create-walle 0.9.11 → 0.9.13

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 (167) hide show
  1. package/README.md +3 -3
  2. package/package.json +2 -2
  3. package/template/bin/dev.sh +7 -1
  4. package/template/bin/setup.js +53 -9
  5. package/template/bin/sync-images.js +53 -0
  6. package/template/builder-journal.md +17 -0
  7. package/template/claude-task-manager/api-prompts.js +98 -13
  8. package/template/claude-task-manager/api-reviews.js +82 -5
  9. package/template/claude-task-manager/db.js +32 -5
  10. package/template/claude-task-manager/docs/session-capture-foundation-design.md +1273 -0
  11. package/template/claude-task-manager/lib/claude-desktop-sessions.js +696 -0
  12. package/template/claude-task-manager/lib/coding-agent-models.js +49 -1
  13. package/template/claude-task-manager/lib/session-capture.js +421 -0
  14. package/template/claude-task-manager/lib/session-history.js +135 -15
  15. package/template/claude-task-manager/lib/session-jobs.js +10 -5
  16. package/template/claude-task-manager/lib/session-stream.js +87 -19
  17. package/template/claude-task-manager/lib/setup-provider-config.js +115 -0
  18. package/template/claude-task-manager/lib/walle-ctm-history.js +72 -0
  19. package/template/claude-task-manager/lib/walle-session-context.js +61 -0
  20. package/template/claude-task-manager/lib/walle-transcript.js +176 -0
  21. package/template/claude-task-manager/public/css/setup.css +35 -8
  22. package/template/claude-task-manager/public/css/walle-session.css +56 -0
  23. package/template/claude-task-manager/public/css/walle.css +120 -0
  24. package/template/claude-task-manager/public/index.html +814 -181
  25. package/template/claude-task-manager/public/js/message-renderer.js +148 -19
  26. package/template/claude-task-manager/public/js/reviews.js +120 -62
  27. package/template/claude-task-manager/public/js/setup.js +75 -31
  28. package/template/claude-task-manager/public/js/stream-view.js +115 -55
  29. package/template/claude-task-manager/public/js/walle-session.js +84 -2
  30. package/template/claude-task-manager/public/js/walle.js +308 -54
  31. package/template/claude-task-manager/server.js +1092 -146
  32. package/template/claude-task-manager/session-integrity.js +181 -54
  33. package/template/claude-task-manager/session-utils.js +123 -41
  34. package/template/claude-task-manager/workers/state-detectors/codex.js +5 -2
  35. package/template/package.json +1 -1
  36. package/template/wall-e/adapters/ctm.js +39 -18
  37. package/template/wall-e/agent-runners/contract.js +17 -0
  38. package/template/wall-e/agent-runners/index.js +22 -0
  39. package/template/wall-e/agent-runtime/harness.js +212 -0
  40. package/template/wall-e/agent-runtime/index.js +8 -0
  41. package/template/wall-e/agent-runtime/registry.js +67 -0
  42. package/template/wall-e/agent-runtime/session-store.js +179 -0
  43. package/template/wall-e/agent-runtime/spawn.js +208 -0
  44. package/template/wall-e/api-walle.js +174 -7
  45. package/template/wall-e/brain.js +266 -28
  46. package/template/wall-e/channels/policy.js +88 -0
  47. package/template/wall-e/channels/registry.js +15 -1
  48. package/template/wall-e/channels/reply-dispatcher.js +70 -0
  49. package/template/wall-e/channels/session-bindings.js +51 -0
  50. package/template/wall-e/chat/code-review-context.js +29 -0
  51. package/template/wall-e/chat.js +188 -42
  52. package/template/wall-e/coding/acp-adapter.js +188 -0
  53. package/template/wall-e/coding/agent-catalog.js +129 -0
  54. package/template/wall-e/coding/compaction-service.js +247 -0
  55. package/template/wall-e/coding/execution-trace.js +3 -0
  56. package/template/wall-e/coding/instruction-service.js +224 -0
  57. package/template/wall-e/coding/model-message.js +67 -0
  58. package/template/wall-e/coding/permission-rules-store.js +111 -0
  59. package/template/wall-e/coding/permission-service.js +266 -0
  60. package/template/wall-e/coding/prompt-bundle.js +67 -0
  61. package/template/wall-e/coding/prompt-runtime.js +243 -0
  62. package/template/wall-e/coding/provider-transform.js +188 -0
  63. package/template/wall-e/coding/runtime-mode.js +132 -0
  64. package/template/wall-e/coding/snapshot-service.js +155 -0
  65. package/template/wall-e/coding/stream-processor.js +268 -0
  66. package/template/wall-e/coding/task-tool.js +255 -0
  67. package/template/wall-e/coding/tool-registry.js +361 -0
  68. package/template/wall-e/coding/transcript-writer.js +143 -0
  69. package/template/wall-e/coding/workspace-replay.js +324 -0
  70. package/template/wall-e/coding-context.js +4 -22
  71. package/template/wall-e/coding-orchestrator.js +307 -18
  72. package/template/wall-e/coding-prompts.js +44 -3
  73. package/template/wall-e/context/context-builder.js +43 -1
  74. package/template/wall-e/context/topic-matcher.js +1 -1
  75. package/template/wall-e/eval/agent-runner.js +59 -13
  76. package/template/wall-e/eval/benchmarks/memory-retrieval.json +155 -57
  77. package/template/wall-e/eval/benchmarks.js +100 -16
  78. package/template/wall-e/eval/eval-orchestrator.js +218 -8
  79. package/template/wall-e/eval/harvester.js +62 -5
  80. package/template/wall-e/eval/head-to-head.js +23 -2
  81. package/template/wall-e/eval/humaneval-adapter.js +30 -5
  82. package/template/wall-e/eval/livecodebench-adapter.js +29 -5
  83. package/template/wall-e/eval/manifest.js +186 -0
  84. package/template/wall-e/eval/run-agent-benchmarks.js +66 -2
  85. package/template/wall-e/eval/session-retrieval-benchmark.js +150 -0
  86. package/template/wall-e/eval/session-transcripts.js +57 -4
  87. package/template/wall-e/eval/swebench-adapter.js +109 -3
  88. package/template/wall-e/evaluation/agent-router.js +53 -1
  89. package/template/wall-e/evaluation/coding-quorum.js +48 -1
  90. package/template/wall-e/evaluation/router.js +4 -2
  91. package/template/wall-e/evaluation/tier-selector.js +11 -1
  92. package/template/wall-e/extraction/contradiction.js +2 -2
  93. package/template/wall-e/extraction/indexer.js +2 -1
  94. package/template/wall-e/extraction/knowledge-extractor.js +2 -2
  95. package/template/wall-e/hooks/cli.js +92 -0
  96. package/template/wall-e/hooks/discovery.js +119 -0
  97. package/template/wall-e/hooks/index.js +7 -0
  98. package/template/wall-e/hooks/manifest.js +55 -0
  99. package/template/wall-e/hooks/runtime.js +84 -0
  100. package/template/wall-e/hooks/session-memory.js +225 -0
  101. package/template/wall-e/http/auth.js +6 -2
  102. package/template/wall-e/http/chat-api.js +54 -8
  103. package/template/wall-e/integrations/claude-plugin/hooks/hooks.json +27 -0
  104. package/template/wall-e/integrations/claude-plugin/hooks/walle-precompact-hook.sh +5 -0
  105. package/template/wall-e/integrations/claude-plugin/hooks/walle-stop-hook.sh +5 -0
  106. package/template/wall-e/integrations/codex-plugin/hooks/walle-hook.sh +7 -0
  107. package/template/wall-e/integrations/codex-plugin/hooks.json +37 -0
  108. package/template/wall-e/listening/calendar.js +3 -1
  109. package/template/wall-e/llm/client.js +64 -10
  110. package/template/wall-e/llm/google.js +39 -5
  111. package/template/wall-e/llm/ollama.js +1 -1
  112. package/template/wall-e/llm/ollama.plugin.json +1 -1
  113. package/template/wall-e/llm/provider-availability.js +10 -0
  114. package/template/wall-e/llm/provider-error.js +269 -0
  115. package/template/wall-e/llm/tool-adapter.js +48 -12
  116. package/template/wall-e/loops/boot.js +2 -1
  117. package/template/wall-e/loops/initiative.js +2 -2
  118. package/template/wall-e/loops/tasks.js +8 -47
  119. package/template/wall-e/loops/workspace-prompts.js +20 -0
  120. package/template/wall-e/mcp-server.js +442 -1
  121. package/template/wall-e/memory/session-ingest-service.js +159 -0
  122. package/template/wall-e/memory/source-indexer.js +289 -0
  123. package/template/wall-e/plugins/discovery.js +83 -0
  124. package/template/wall-e/plugins/manifest-loader.js +50 -10
  125. package/template/wall-e/plugins/manifest-schema.js +69 -0
  126. package/template/wall-e/plugins/model-catalog.js +55 -0
  127. package/template/wall-e/prompts/coding/base.txt +2 -0
  128. package/template/wall-e/prompts/coding/deepseek.txt +1 -0
  129. package/template/wall-e/prompts/coding/memory-protocol.md +9 -0
  130. package/template/wall-e/prompts/coding/plan.txt +1 -0
  131. package/template/wall-e/runtime/execution-trace.js +220 -0
  132. package/template/wall-e/security/audit.js +266 -0
  133. package/template/wall-e/security/ssrf.js +236 -0
  134. package/template/wall-e/session-files.js +303 -0
  135. package/template/wall-e/skills/_bundled/slack-backfill/SKILL.md +3 -0
  136. package/template/wall-e/skills/_bundled/slack-sync/SKILL.md +3 -0
  137. package/template/wall-e/skills/internal-skill-registry.js +2 -2
  138. package/template/wall-e/skills/script-skill-runner.js +143 -0
  139. package/template/wall-e/skills/skill-executor.js +5 -6
  140. package/template/wall-e/skills/skill-fallback.js +3 -1
  141. package/template/wall-e/skills/skill-harness-registry.js +7 -8
  142. package/template/wall-e/skills/skill-planner.js +52 -4
  143. package/template/wall-e/skills/slack-ingest.js +11 -3
  144. package/template/wall-e/sources/base.js +90 -0
  145. package/template/wall-e/sources/builtin.js +33 -0
  146. package/template/wall-e/sources/claude-code-jsonl.js +78 -0
  147. package/template/wall-e/sources/codex-jsonl.js +125 -0
  148. package/template/wall-e/sources/coding-session-utils.js +117 -0
  149. package/template/wall-e/sources/contract-suite.js +59 -0
  150. package/template/wall-e/sources/gemini-jsonl.js +85 -0
  151. package/template/wall-e/sources/index.js +9 -0
  152. package/template/wall-e/sources/jsonl-utils.js +181 -0
  153. package/template/wall-e/sources/record-types.js +252 -0
  154. package/template/wall-e/sources/registry.js +92 -0
  155. package/template/wall-e/sources/transforms.js +100 -0
  156. package/template/wall-e/sources/walle-jsonl.js +108 -0
  157. package/template/wall-e/tools/coding-middleware.js +31 -1
  158. package/template/wall-e/tools/file-tracker.js +25 -1
  159. package/template/wall-e/tools/local-tools.js +75 -47
  160. package/template/wall-e/tools/session-sharing.js +68 -1
  161. package/template/wall-e/tools/shell-analyzer.js +1 -1
  162. package/template/wall-e/tools/shell-policy.js +47 -0
  163. package/template/wall-e/tools/snapshot.js +42 -0
  164. package/template/wall-e/training/harvester.js +62 -5
  165. package/template/wall-e/utils/repair.js +253 -1
  166. package/template/website/index.html +3 -3
  167. package/template/wall-e/skills/_bundled/slack-mentions/.watched-threads.json +0 -18
@@ -0,0 +1,696 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const os = require('os');
6
+ const zlib = require('zlib');
7
+
8
+ const DESKTOP_PROJECT_ENTRY = 'claude-desktop';
9
+ const DESKTOP_PROJECT_PATH = 'Claude Desktop';
10
+ const DESKTOP_AGENT = 'claude-desktop';
11
+ const VIRTUAL_MARKER = '#ctm-claude-desktop=';
12
+ const CACHE_TTL_MS = 10000;
13
+ const MAX_CACHE_FILE_SIZE = 25 * 1024 * 1024;
14
+ const SSTABLE_MAGIC_HEX = '57fb808b247547db';
15
+
16
+ let _cache = { key: '', ts: 0, sessions: [] };
17
+
18
+ function isDisabled(env = process.env) {
19
+ return env.CTM_DISABLE_CLAUDE_DESKTOP_SESSIONS === '1' ||
20
+ env.CLAUDE_DESKTOP_SESSIONS === '0';
21
+ }
22
+
23
+ function getAppSupportDirs(env = process.env) {
24
+ if (isDisabled(env)) return [];
25
+ const explicit = env.CLAUDE_DESKTOP_APP_SUPPORT_DIRS || env.CLAUDE_DESKTOP_APP_SUPPORT_DIR;
26
+ if (explicit) {
27
+ return explicit.split(path.delimiter).filter(Boolean).map(p => expandHome(p, env));
28
+ }
29
+
30
+ const home = env.HOME || os.homedir();
31
+ if (!home) return [];
32
+ if (process.platform === 'darwin') {
33
+ return [
34
+ path.join(home, 'Library', 'Application Support', 'Claude'),
35
+ path.join(home, 'Library', 'Application Support', 'Claude-3p'),
36
+ ];
37
+ }
38
+ if (process.platform === 'win32') {
39
+ const appData = env.APPDATA || path.join(home, 'AppData', 'Roaming');
40
+ return [path.join(appData, 'Claude')];
41
+ }
42
+ const configHome = env.XDG_CONFIG_HOME || path.join(home, '.config');
43
+ return [path.join(configHome, 'Claude')];
44
+ }
45
+
46
+ function expandHome(p, env) {
47
+ if (!p || p[0] !== '~') return p;
48
+ const home = env.HOME || os.homedir();
49
+ return home ? path.join(home, p.slice(1)) : p;
50
+ }
51
+
52
+ function virtualSessionPath(sourcePath, sessionId) {
53
+ return `${sourcePath || DESKTOP_PROJECT_PATH}${VIRTUAL_MARKER}${encodeURIComponent(sessionId)}`;
54
+ }
55
+
56
+ function parseVirtualSessionPath(filePath) {
57
+ const idx = String(filePath || '').indexOf(VIRTUAL_MARKER);
58
+ if (idx === -1) return null;
59
+ let sessionId;
60
+ try {
61
+ sessionId = decodeURIComponent(filePath.slice(idx + VIRTUAL_MARKER.length));
62
+ } catch {
63
+ return null;
64
+ }
65
+ return {
66
+ sourcePath: filePath.slice(0, idx),
67
+ sessionId,
68
+ };
69
+ }
70
+
71
+ function isVirtualSessionPath(filePath) {
72
+ return !!parseVirtualSessionPath(filePath);
73
+ }
74
+
75
+ function sourcePathForStat(filePath) {
76
+ const parsed = parseVirtualSessionPath(filePath);
77
+ return parsed ? parsed.sourcePath : filePath;
78
+ }
79
+
80
+ function listSessionFileEntries(options = {}) {
81
+ return listSessions(options).map(session => ({
82
+ filePath: virtualSessionPath(session.sourcePath || session.appDir || DESKTOP_PROJECT_PATH, session.uuid),
83
+ projectPath: DESKTOP_PROJECT_PATH,
84
+ projectEntry: DESKTOP_PROJECT_ENTRY,
85
+ sessionId: session.uuid,
86
+ agent: DESKTOP_AGENT,
87
+ }));
88
+ }
89
+
90
+ function listSessions(options = {}) {
91
+ const env = options.env || process.env;
92
+ const appDirs = (options.appSupportDirs || getAppSupportDirs(env)).filter(Boolean);
93
+ const key = appDirs.join('\0');
94
+ const now = Date.now();
95
+ if (!options.force && _cache.key === key && (now - _cache.ts) < CACHE_TTL_MS) {
96
+ return _cache.sessions.map(cloneSession);
97
+ }
98
+
99
+ const byId = new Map();
100
+ for (const appDir of appDirs) {
101
+ try {
102
+ if (!fs.existsSync(appDir) || !fs.statSync(appDir).isDirectory()) continue;
103
+ } catch {
104
+ continue;
105
+ }
106
+
107
+ for (const textInfo of readReactQueryCacheTexts(appDir)) {
108
+ for (const session of parseConversationListsFromReactQueryText(textInfo.text)) {
109
+ mergeSession(byId, { ...session, appDir, sourcePath: textInfo.sourcePath });
110
+ }
111
+ }
112
+
113
+ for (const session of readCacheConversations(appDir)) {
114
+ mergeSession(byId, session);
115
+ }
116
+ }
117
+
118
+ const sessions = [...byId.values()]
119
+ .filter(s => s.uuid)
120
+ .sort((a, b) => new Date(b.updatedAt || b.modifiedAt || 0) - new Date(a.updatedAt || a.modifiedAt || 0));
121
+ _cache = { key, ts: now, sessions: sessions.map(cloneSession) };
122
+ return sessions.map(cloneSession);
123
+ }
124
+
125
+ function getSession(sessionId, options = {}) {
126
+ if (!sessionId) return null;
127
+ return listSessions(options).find(s => s.uuid === sessionId) || null;
128
+ }
129
+
130
+ function getSessionFromVirtualPath(filePath, options = {}) {
131
+ const parsed = parseVirtualSessionPath(filePath);
132
+ return parsed ? getSession(parsed.sessionId, options) : null;
133
+ }
134
+
135
+ function parseSessionFile(filePath, projectPath = DESKTOP_PROJECT_PATH, projectEntry = DESKTOP_PROJECT_ENTRY, options = {}) {
136
+ const parsed = parseVirtualSessionPath(filePath);
137
+ if (!parsed) throw new Error('not a Claude Desktop virtual session path');
138
+ const session = getSession(parsed.sessionId, options) || {
139
+ uuid: parsed.sessionId,
140
+ sourcePath: parsed.sourcePath,
141
+ title: '',
142
+ summary: '',
143
+ messages: [],
144
+ };
145
+ const stat = statSource(session.sourcePath || parsed.sourcePath);
146
+ const messages = Array.isArray(session.messages) ? session.messages : [];
147
+ const firstUser = messages.find(m => m.role === 'user');
148
+ const firstAssistant = messages.find(m => m.role === 'assistant');
149
+ const lastUser = [...messages].reverse().find(m => m.role === 'user');
150
+ const firstMessage = cleanText(firstUser?.text || session.firstUserText || session.summary || session.title || '');
151
+ const title = makeTitle(session.title || session.name || firstMessage || session.summary || session.uuid);
152
+ const updated = session.updatedAt || session.modifiedAt || stat.modifiedAt || '';
153
+ const created = session.createdAt || updated;
154
+
155
+ return {
156
+ sessionId: session.uuid,
157
+ project: projectPath || DESKTOP_PROJECT_PATH,
158
+ projectEntry: projectEntry || DESKTOP_PROJECT_ENTRY,
159
+ cwd: DESKTOP_PROJECT_PATH,
160
+ firstMessage,
161
+ lastUserContent: cleanText(lastUser?.text || firstMessage || ''),
162
+ firstAssistantText: cleanText(firstAssistant?.text || ''),
163
+ renameName: '',
164
+ title,
165
+ isEmpty: !firstMessage && messages.length === 0,
166
+ userMsgCount: messages.filter(m => m.role === 'user').length,
167
+ modifiedAt: updated,
168
+ fileModifiedAt: stat.modifiedAt || updated,
169
+ timestamp: created,
170
+ version: '',
171
+ gitBranch: '',
172
+ slug: '',
173
+ fileSize: stat.fileSize,
174
+ hostname: os.hostname(),
175
+ modelId: session.model || '',
176
+ modelProvider: session.model ? 'anthropic' : '',
177
+ model: session.model || '',
178
+ agent: DESKTOP_AGENT,
179
+ jsonlPath: '',
180
+ sourcePath: session.sourcePath || parsed.sourcePath,
181
+ desktopCacheOnly: messages.length === 0,
182
+ };
183
+ }
184
+
185
+ function getMessages(sessionId, options = {}) {
186
+ const session = getSession(sessionId, options);
187
+ if (!session) return null;
188
+ const messages = Array.isArray(session.messages) ? session.messages : [];
189
+ if (messages.length > 0) return messages.map(m => ({ ...m }));
190
+ if (!options.includeMetadataNote) return [];
191
+ return [{
192
+ role: 'system',
193
+ text: 'Claude Desktop has cached this conversation in the recent list, but the full message transcript is not present in the local Desktop HTTP cache yet. Open the conversation in Claude Desktop once, then rescan to populate the transcript.',
194
+ timestamp: session.updatedAt || session.modifiedAt || session.createdAt || new Date().toISOString(),
195
+ }];
196
+ }
197
+
198
+ function toClaudeCodeEntries(sessionOrId, options = {}) {
199
+ const session = typeof sessionOrId === 'string' ? getSession(sessionOrId, options) : sessionOrId;
200
+ if (!session || !Array.isArray(session.messages)) return [];
201
+ return session.messages.map((msg, index) => ({
202
+ type: msg.role === 'assistant' ? 'assistant' : 'user',
203
+ provider: DESKTOP_AGENT,
204
+ timestamp: msg.timestamp,
205
+ uuid: msg.uuid || `${session.uuid}:${index}`,
206
+ message: {
207
+ role: msg.role === 'assistant' ? 'assistant' : 'user',
208
+ content: msg.text || '',
209
+ model: msg.role === 'assistant' ? (session.model || '') : undefined,
210
+ },
211
+ _source: DESKTOP_AGENT,
212
+ _sessionId: session.uuid,
213
+ }));
214
+ }
215
+
216
+ function readReactQueryCacheTexts(appDir) {
217
+ const levelDir = path.join(appDir, 'Local Storage', 'leveldb');
218
+ let files;
219
+ try { files = fs.readdirSync(levelDir); } catch { return []; }
220
+ const out = [];
221
+ for (const file of files) {
222
+ if (!file.endsWith('.ldb')) continue;
223
+ const filePath = path.join(levelDir, file);
224
+ let stat;
225
+ try { stat = fs.statSync(filePath); } catch { continue; }
226
+ if (!stat.isFile() || stat.size > MAX_CACHE_FILE_SIZE) continue;
227
+ for (const { key, value } of readSstableEntries(filePath)) {
228
+ const keyText = key.toString('utf8');
229
+ if (!keyText.includes('react-query-cache-ls')) continue;
230
+ const text = decodeDomStorageValue(value);
231
+ if (text && text.includes('chat_conversation_list')) out.push({ sourcePath: filePath, text });
232
+ }
233
+ }
234
+ return out;
235
+ }
236
+
237
+ function readCacheConversations(appDir) {
238
+ const cacheDir = path.join(appDir, 'Cache', 'Cache_Data');
239
+ let files;
240
+ try { files = fs.readdirSync(cacheDir); } catch { return []; }
241
+ const sessions = [];
242
+ for (const file of files) {
243
+ const filePath = path.join(cacheDir, file);
244
+ let stat;
245
+ try { stat = fs.statSync(filePath); } catch { continue; }
246
+ if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_CACHE_FILE_SIZE) continue;
247
+
248
+ let buf;
249
+ try { buf = fs.readFileSync(filePath); } catch { continue; }
250
+ const head = buf.subarray(0, Math.min(buf.length, 4096)).toString('latin1');
251
+ if (!/chat_conversations/i.test(head)) continue;
252
+
253
+ const json = extractJsonFromCacheBuffer(buf);
254
+ if (!json) continue;
255
+ for (const session of normalizeCacheJson(json, filePath, appDir, stat)) {
256
+ sessions.push(session);
257
+ }
258
+ }
259
+ return sessions;
260
+ }
261
+
262
+ function extractJsonFromCacheBuffer(buf) {
263
+ const candidates = [];
264
+ const zstdIdx = buf.indexOf(Buffer.from([0x28, 0xb5, 0x2f, 0xfd]));
265
+ if (zstdIdx >= 0 && typeof zlib.zstdDecompressSync === 'function') {
266
+ candidates.push(() => zlib.zstdDecompressSync(buf.subarray(zstdIdx)).toString('utf8'));
267
+ }
268
+ const gzipIdx = buf.indexOf(Buffer.from([0x1f, 0x8b, 0x08]));
269
+ if (gzipIdx >= 0) {
270
+ candidates.push(() => zlib.gunzipSync(buf.subarray(gzipIdx)).toString('utf8'));
271
+ }
272
+ const brMarker = Buffer.from('content-encoding:brotli', 'latin1');
273
+ if (buf.indexOf(brMarker) >= 0 && typeof zlib.brotliDecompressSync === 'function') {
274
+ const start = findFirstJsonishByte(buf);
275
+ if (start >= 0) candidates.push(() => zlib.brotliDecompressSync(buf.subarray(start)).toString('utf8'));
276
+ }
277
+ candidates.push(() => {
278
+ const text = buf.toString('utf8');
279
+ const idx = text.search(/[\[{]/);
280
+ return idx >= 0 ? text.slice(idx) : '';
281
+ });
282
+
283
+ for (const makeText of candidates) {
284
+ let text = '';
285
+ try { text = makeText(); } catch { continue; }
286
+ const parsed = parseFirstJsonValue(text);
287
+ if (parsed) return parsed;
288
+ }
289
+ return null;
290
+ }
291
+
292
+ function findFirstJsonishByte(buf) {
293
+ const brace = buf.indexOf(0x7b);
294
+ const bracket = buf.indexOf(0x5b);
295
+ if (brace === -1) return bracket;
296
+ if (bracket === -1) return brace;
297
+ return Math.min(brace, bracket);
298
+ }
299
+
300
+ function parseFirstJsonValue(text) {
301
+ const start = text.search(/[\[{]/);
302
+ if (start < 0) return null;
303
+ const end = findJsonValueEnd(text, start);
304
+ const raw = (end > start ? text.slice(start, end) : text.slice(start)).replace(/[\u0000-\u001f]/g, ' ');
305
+ try { return JSON.parse(raw); } catch { return null; }
306
+ }
307
+
308
+ function normalizeCacheJson(json, sourcePath, appDir, stat) {
309
+ const out = [];
310
+ if (Array.isArray(json)) {
311
+ for (const item of json) {
312
+ const session = normalizeConversationObject(item, sourcePath, appDir, stat);
313
+ if (session) out.push(session);
314
+ }
315
+ return out;
316
+ }
317
+ if (Array.isArray(json.data)) {
318
+ for (const item of json.data) {
319
+ const session = normalizeConversationObject(item, sourcePath, appDir, stat);
320
+ if (session) out.push(session);
321
+ }
322
+ }
323
+ const direct = normalizeConversationObject(json, sourcePath, appDir, stat);
324
+ if (direct) out.push(direct);
325
+ return out;
326
+ }
327
+
328
+ function parseConversationListsFromReactQueryText(text) {
329
+ const sessions = [];
330
+ let pos = 0;
331
+ while ((pos = text.indexOf('"queryKey":["chat_conversation_list"', pos + 1)) !== -1) {
332
+ const start = text.lastIndexOf('{"state":', pos);
333
+ if (start < 0) continue;
334
+ const end = findJsonValueEnd(text, start);
335
+ if (end <= start) continue;
336
+ const raw = text.slice(start, end).replace(/[\u0000-\u001f]/g, ' ');
337
+ let query;
338
+ try { query = JSON.parse(raw); } catch { continue; }
339
+ const data = query.state && query.state.data;
340
+ const items = Array.isArray(data) ? data
341
+ : Array.isArray(data && data.data) ? data.data
342
+ : Array.isArray(data && data.conversations) ? data.conversations
343
+ : [];
344
+ for (const item of items) {
345
+ const session = normalizeConversationObject(item);
346
+ if (session) sessions.push({ ...session, sourceKind: 'recent-list', metadataOnly: session.messages.length === 0 });
347
+ }
348
+ }
349
+ return sessions;
350
+ }
351
+
352
+ function normalizeConversationObject(obj, sourcePath = '', appDir = '', stat = null) {
353
+ if (!obj || typeof obj !== 'object') return null;
354
+ const uuid = cleanText(obj.uuid || obj.conversation_uuid || obj.id || '');
355
+ if (!isUuid(uuid)) return null;
356
+ const messages = normalizeMessages(obj.chat_messages || obj.messages || []);
357
+ const firstUser = messages.find(m => m.role === 'user');
358
+ const firstAssistant = messages.find(m => m.role === 'assistant');
359
+ const modifiedAt = stat && stat.mtime ? stat.mtime.toISOString() : '';
360
+ return {
361
+ uuid,
362
+ name: cleanText(obj.name || obj.title || ''),
363
+ title: cleanText(obj.name || obj.title || ''),
364
+ summary: cleanText(obj.summary || ''),
365
+ model: cleanText(obj.model || obj.settings?.model || ''),
366
+ createdAt: cleanText(obj.created_at || obj.createdAt || ''),
367
+ updatedAt: cleanText(obj.updated_at || obj.updatedAt || modifiedAt),
368
+ modifiedAt,
369
+ sourcePath,
370
+ appDir,
371
+ isStarred: !!obj.is_starred,
372
+ platform: cleanText(obj.platform || ''),
373
+ desktopSessionId: cleanText(obj.session_id || ''),
374
+ messages,
375
+ firstUserText: cleanText(firstUser?.text || ''),
376
+ firstAssistantText: cleanText(firstAssistant?.text || ''),
377
+ userMsgCount: messages.filter(m => m.role === 'user').length,
378
+ assistantMsgCount: messages.filter(m => m.role === 'assistant').length,
379
+ sourceKind: sourcePath ? 'http-cache' : '',
380
+ metadataOnly: messages.length === 0,
381
+ };
382
+ }
383
+
384
+ function normalizeMessages(messages) {
385
+ if (!Array.isArray(messages)) return [];
386
+ return messages.map((msg, index) => {
387
+ const role = normalizeRole(msg.sender || msg.role || msg.author);
388
+ const text = cleanText(extractMessageText(msg));
389
+ if (!role || !text) return null;
390
+ return {
391
+ role,
392
+ text,
393
+ timestamp: cleanText(msg.created_at || msg.updated_at || msg.timestamp || ''),
394
+ uuid: cleanText(msg.uuid || msg.id || ''),
395
+ index: Number.isFinite(msg.index) ? msg.index : index,
396
+ };
397
+ }).filter(Boolean).sort((a, b) => {
398
+ if (a.index !== b.index) return a.index - b.index;
399
+ return (a.timestamp || '').localeCompare(b.timestamp || '');
400
+ });
401
+ }
402
+
403
+ function normalizeRole(role) {
404
+ const v = String(role || '').toLowerCase();
405
+ if (v === 'human' || v === 'user') return 'user';
406
+ if (v === 'assistant' || v === 'claude') return 'assistant';
407
+ if (v === 'system') return 'system';
408
+ return '';
409
+ }
410
+
411
+ function extractMessageText(msg) {
412
+ if (!msg) return '';
413
+ if (typeof msg.text === 'string' && msg.text) return msg.text;
414
+ return extractContentText(msg.content);
415
+ }
416
+
417
+ function extractContentText(value) {
418
+ if (!value) return '';
419
+ if (typeof value === 'string') return value;
420
+ if (Array.isArray(value)) {
421
+ return value.map(extractContentText).filter(Boolean).join('\n');
422
+ }
423
+ if (typeof value !== 'object') return '';
424
+ if (typeof value.text === 'string') return value.text;
425
+ if (typeof value.content === 'string') return value.content;
426
+ if (Array.isArray(value.content)) return extractContentText(value.content);
427
+ if (value.type === 'tool_use') return `[Tool: ${cleanText(value.name || 'tool')}]`;
428
+ return '';
429
+ }
430
+
431
+ function mergeSession(map, incoming) {
432
+ if (!incoming || !incoming.uuid) return;
433
+ const existing = map.get(incoming.uuid);
434
+ if (!existing) {
435
+ map.set(incoming.uuid, incoming);
436
+ return;
437
+ }
438
+ const existingMsgCount = Array.isArray(existing.messages) ? existing.messages.length : 0;
439
+ const incomingMsgCount = Array.isArray(incoming.messages) ? incoming.messages.length : 0;
440
+ const preferMessages = incomingMsgCount >= existingMsgCount;
441
+ const newerIncoming = new Date(incoming.updatedAt || incoming.modifiedAt || 0) >= new Date(existing.updatedAt || existing.modifiedAt || 0);
442
+ const messages = preferMessages ? (incoming.messages || []) : (existing.messages || []);
443
+ map.set(incoming.uuid, {
444
+ ...existing,
445
+ ...(newerIncoming ? incoming : {}),
446
+ title: incoming.title || existing.title || incoming.summary || existing.summary || '',
447
+ name: incoming.name || existing.name || '',
448
+ summary: incoming.summary || existing.summary || '',
449
+ model: incoming.model || existing.model || '',
450
+ createdAt: existing.createdAt || incoming.createdAt || '',
451
+ updatedAt: newerIncoming ? (incoming.updatedAt || existing.updatedAt || '') : (existing.updatedAt || incoming.updatedAt || ''),
452
+ modifiedAt: newerIncoming ? (incoming.modifiedAt || existing.modifiedAt || '') : (existing.modifiedAt || incoming.modifiedAt || ''),
453
+ sourcePath: preferMessages ? (incoming.sourcePath || existing.sourcePath || '') : (existing.sourcePath || incoming.sourcePath || ''),
454
+ appDir: incoming.appDir || existing.appDir || '',
455
+ messages,
456
+ firstUserText: incoming.firstUserText || existing.firstUserText || '',
457
+ firstAssistantText: incoming.firstAssistantText || existing.firstAssistantText || '',
458
+ userMsgCount: preferMessages ? (incoming.userMsgCount || 0) : (existing.userMsgCount || 0),
459
+ assistantMsgCount: preferMessages ? (incoming.assistantMsgCount || 0) : (existing.assistantMsgCount || 0),
460
+ sourceKind: preferMessages ? (incoming.sourceKind || existing.sourceKind || '') : (existing.sourceKind || incoming.sourceKind || ''),
461
+ metadataOnly: messages.length === 0,
462
+ });
463
+ }
464
+
465
+ function readSstableEntries(filePath) {
466
+ let db;
467
+ try { db = fs.readFileSync(filePath); } catch { return []; }
468
+ if (db.length < 53 || db.subarray(db.length - 8).toString('hex') !== SSTABLE_MAGIC_HEX) return [];
469
+ const footer = db.subarray(db.length - 48);
470
+ const meta = decodeBlockHandle(footer, 0);
471
+ if (!meta) return [];
472
+ const index = decodeBlockHandle(footer, meta.bytes);
473
+ if (!index) return [];
474
+
475
+ let indexEntries;
476
+ try { indexEntries = parseBlockEntries(readBlock(db, index.offset, index.size)); } catch { return []; }
477
+ const out = [];
478
+ for (const entry of indexEntries) {
479
+ const handle = decodeBlockHandle(entry.value, 0);
480
+ if (!handle) continue;
481
+ let block;
482
+ try { block = readBlock(db, handle.offset, handle.size); } catch { continue; }
483
+ out.push(...parseBlockEntries(block));
484
+ }
485
+ return out;
486
+ }
487
+
488
+ function decodeBlockHandle(buf, off) {
489
+ const a = readVarint(buf, off);
490
+ if (!a) return null;
491
+ const b = readVarint(buf, off + a.bytes);
492
+ if (!b) return null;
493
+ return { offset: a.value, size: b.value, bytes: a.bytes + b.bytes };
494
+ }
495
+
496
+ function readBlock(db, offset, size) {
497
+ if (offset < 0 || size < 0 || offset + size + 5 > db.length) throw new Error('bad block');
498
+ const raw = db.subarray(offset, offset + size);
499
+ const compression = db[offset + size];
500
+ if (compression === 0) return raw;
501
+ if (compression === 1) return snappyDecompress(raw);
502
+ throw new Error(`unsupported leveldb compression ${compression}`);
503
+ }
504
+
505
+ function parseBlockEntries(block) {
506
+ if (!block || block.length < 4) return [];
507
+ const restartCount = block.readUInt32LE(block.length - 4);
508
+ const restartsStart = block.length - 4 - restartCount * 4;
509
+ if (restartCount < 0 || restartsStart < 0 || restartsStart > block.length) return [];
510
+ let off = 0;
511
+ let prev = Buffer.alloc(0);
512
+ const entries = [];
513
+ while (off < restartsStart) {
514
+ const shared = readVarint(block, off); if (!shared) break; off += shared.bytes;
515
+ const nonShared = readVarint(block, off); if (!nonShared) break; off += nonShared.bytes;
516
+ const valueLen = readVarint(block, off); if (!valueLen) break; off += valueLen.bytes;
517
+ if (shared.value > prev.length || off + nonShared.value + valueLen.value > restartsStart) break;
518
+ const key = Buffer.concat([prev.subarray(0, shared.value), block.subarray(off, off + nonShared.value)]);
519
+ off += nonShared.value;
520
+ const value = block.subarray(off, off + valueLen.value);
521
+ off += valueLen.value;
522
+ prev = key;
523
+ entries.push({ key, value });
524
+ }
525
+ return entries;
526
+ }
527
+
528
+ function readVarint(buf, off) {
529
+ let result = 0;
530
+ let shift = 0;
531
+ for (let i = 0; i < 10 && off + i < buf.length; i++) {
532
+ const byte = buf[off + i];
533
+ result += (byte & 0x7f) * Math.pow(2, shift);
534
+ if ((byte & 0x80) === 0) return { value: result, bytes: i + 1 };
535
+ shift += 7;
536
+ }
537
+ return null;
538
+ }
539
+
540
+ function snappyDecompress(buf) {
541
+ const lenInfo = readVarint(buf, 0);
542
+ if (!lenInfo) throw new Error('bad snappy length');
543
+ const out = Buffer.alloc(lenInfo.value);
544
+ let ip = lenInfo.bytes;
545
+ let op = 0;
546
+ while (ip < buf.length && op < out.length) {
547
+ const tag = buf[ip++];
548
+ const type = tag & 3;
549
+ if (type === 0) {
550
+ let len = tag >>> 2;
551
+ if (len < 60) {
552
+ len += 1;
553
+ } else {
554
+ const n = len - 59;
555
+ len = 0;
556
+ for (let i = 0; i < n; i++) len |= buf[ip++] << (8 * i);
557
+ len += 1;
558
+ }
559
+ if (ip + len > buf.length || op + len > out.length) throw new Error('bad snappy literal');
560
+ buf.copy(out, op, ip, ip + len);
561
+ ip += len;
562
+ op += len;
563
+ } else {
564
+ let len;
565
+ let offset;
566
+ if (type === 1) {
567
+ len = ((tag >>> 2) & 7) + 4;
568
+ offset = ((tag & 0xe0) << 3) | buf[ip++];
569
+ } else if (type === 2) {
570
+ len = (tag >>> 2) + 1;
571
+ offset = buf[ip] | (buf[ip + 1] << 8);
572
+ ip += 2;
573
+ } else {
574
+ len = (tag >>> 2) + 1;
575
+ offset = (buf[ip] | (buf[ip + 1] << 8) | (buf[ip + 2] << 16) | (buf[ip + 3] << 24)) >>> 0;
576
+ ip += 4;
577
+ }
578
+ if (offset <= 0 || offset > op || op + len > out.length) throw new Error('bad snappy copy');
579
+ for (let i = 0; i < len; i++) out[op + i] = out[op - offset + i];
580
+ op += len;
581
+ }
582
+ }
583
+ if (op !== out.length) throw new Error('truncated snappy block');
584
+ return out;
585
+ }
586
+
587
+ function decodeDomStorageValue(buf) {
588
+ if (!buf || buf.length === 0) return '';
589
+ if (buf.length > 2 && buf[0] === 0 && buf[1] !== 0) return decodeUtf16be(buf);
590
+ if (buf.length > 2 && buf[1] === 0) return buf.toString('utf16le');
591
+ return buf.toString('utf8');
592
+ }
593
+
594
+ function decodeUtf16be(buf) {
595
+ const chunks = [];
596
+ const maxBytes = 16384;
597
+ for (let i = 0; i + 1 < buf.length; i += maxBytes) {
598
+ let s = '';
599
+ const end = Math.min(buf.length - 1, i + maxBytes);
600
+ for (let j = i; j < end; j += 2) s += String.fromCharCode((buf[j] << 8) | buf[j + 1]);
601
+ chunks.push(s);
602
+ }
603
+ return chunks.join('');
604
+ }
605
+
606
+ function findJsonValueEnd(text, start) {
607
+ const first = text[start];
608
+ if (first !== '{' && first !== '[') return -1;
609
+ const stack = [first === '{' ? '}' : ']'];
610
+ let inString = false;
611
+ let escaped = false;
612
+ for (let i = start + 1; i < text.length; i++) {
613
+ const ch = text[i];
614
+ if (inString) {
615
+ if (escaped) escaped = false;
616
+ else if (ch === '\\') escaped = true;
617
+ else if (ch === '"') inString = false;
618
+ continue;
619
+ }
620
+ if (ch === '"') {
621
+ inString = true;
622
+ } else if (ch === '{') {
623
+ stack.push('}');
624
+ } else if (ch === '[') {
625
+ stack.push(']');
626
+ } else if (ch === stack[stack.length - 1]) {
627
+ stack.pop();
628
+ if (stack.length === 0) return i + 1;
629
+ }
630
+ }
631
+ return -1;
632
+ }
633
+
634
+ function cleanText(value) {
635
+ if (value == null) return '';
636
+ return String(value).replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, ' ').trim();
637
+ }
638
+
639
+ function makeTitle(value) {
640
+ let title = cleanText(value).split('\n')[0].replace(/^#+\s*/, '').replace(/[*_`]/g, '').trim();
641
+ if (title.length > 80) title = title.slice(0, 77) + '...';
642
+ return title;
643
+ }
644
+
645
+ function isUuid(value) {
646
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value || '');
647
+ }
648
+
649
+ function statSource(filePath) {
650
+ try {
651
+ const st = fs.statSync(sourcePathForStat(filePath));
652
+ return {
653
+ fileSize: st.size || 0,
654
+ modifiedAt: st.mtime ? st.mtime.toISOString() : '',
655
+ };
656
+ } catch {
657
+ return { fileSize: 0, modifiedAt: '' };
658
+ }
659
+ }
660
+
661
+ function cloneSession(session) {
662
+ return {
663
+ ...session,
664
+ messages: Array.isArray(session.messages) ? session.messages.map(m => ({ ...m })) : [],
665
+ };
666
+ }
667
+
668
+ module.exports = {
669
+ DESKTOP_PROJECT_ENTRY,
670
+ DESKTOP_PROJECT_PATH,
671
+ DESKTOP_AGENT,
672
+ VIRTUAL_MARKER,
673
+ getAppSupportDirs,
674
+ listSessions,
675
+ getSession,
676
+ getSessionFromVirtualPath,
677
+ listSessionFileEntries,
678
+ parseSessionFile,
679
+ getMessages,
680
+ toClaudeCodeEntries,
681
+ virtualSessionPath,
682
+ parseVirtualSessionPath,
683
+ isVirtualSessionPath,
684
+ sourcePathForStat,
685
+ _private: {
686
+ parseConversationListsFromReactQueryText,
687
+ normalizeConversationObject,
688
+ normalizeMessages,
689
+ extractJsonFromCacheBuffer,
690
+ decodeDomStorageValue,
691
+ decodeUtf16be,
692
+ findJsonValueEnd,
693
+ snappyDecompress,
694
+ readSstableEntries,
695
+ },
696
+ };