@clagentic/console 1.6.0 → 1.7.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/bin/cli.js +20 -102
  2. package/deploy/clagentic-console.service +10 -1
  3. package/lib/agents-favorites.js +0 -1
  4. package/lib/agents.js +0 -10
  5. package/lib/cli-sessions.js +0 -12
  6. package/lib/codex-defaults.js +4 -1
  7. package/lib/config.js +0 -8
  8. package/lib/daemon.js +88 -9
  9. package/lib/dm.js +0 -1
  10. package/lib/ipc.js +129 -59
  11. package/lib/loop-handoff.js +5 -1
  12. package/lib/memory-limits.js +214 -36
  13. package/lib/memory-shed.js +207 -0
  14. package/lib/notes.js +0 -20
  15. package/lib/os-users.js +0 -3
  16. package/lib/project-connection.js +31 -5
  17. package/lib/project-external-trigger.js +2 -1
  18. package/lib/project-filesystem.js +18 -11
  19. package/lib/project-knowledge.js +0 -8
  20. package/lib/project-loop.js +162 -9
  21. package/lib/project-sessions.js +87 -21
  22. package/lib/project-user-mention.js +2 -1
  23. package/lib/project-user-message.js +16 -5
  24. package/lib/project.js +14 -25
  25. package/lib/public/app.js +1 -2
  26. package/lib/public/css/channel-layout.css +25 -158
  27. package/lib/public/css/diagnostics.css +17 -1
  28. package/lib/public/css/icon-strip.css +1 -32
  29. package/lib/public/css/input.css +0 -56
  30. package/lib/public/css/loop.css +94 -0
  31. package/lib/public/css/messages.css +23 -4
  32. package/lib/public/css/mobile-nav.css +0 -124
  33. package/lib/public/index.html +11 -11
  34. package/lib/public/modules/app-connection.js +2 -1
  35. package/lib/public/modules/app-favicon.js +1 -1
  36. package/lib/public/modules/app-loop-ui.js +140 -4
  37. package/lib/public/modules/app-messages.js +17 -11
  38. package/lib/public/modules/app-projects.js +1 -1
  39. package/lib/public/modules/custom-icons.js +15 -1
  40. package/lib/public/modules/dom-refs.js +1 -2
  41. package/lib/public/modules/emoji-picker.js +6 -1
  42. package/lib/public/modules/filebrowser.js +33 -9
  43. package/lib/public/modules/input.js +39 -0
  44. package/lib/public/modules/scheduler.js +20 -6
  45. package/lib/public/modules/sidebar-mobile.js +0 -64
  46. package/lib/public/modules/sidebar-projects.js +90 -25
  47. package/lib/public/modules/sidebar-sessions.js +80 -13
  48. package/lib/public/modules/stt.js +2 -6
  49. package/lib/public/modules/tool-palette.js +0 -1
  50. package/lib/public/modules/tools.js +2 -6
  51. package/lib/public/style.css +0 -1
  52. package/lib/scheduler.js +0 -1
  53. package/lib/sdk-bridge.js +126 -194
  54. package/lib/sdk-message-processor.js +24 -9
  55. package/lib/sdk-permission-ownership.js +51 -0
  56. package/lib/server-auth.js +25 -18
  57. package/lib/server-dm.js +1 -1
  58. package/lib/server-skills.js +9 -1
  59. package/lib/server.js +18 -3
  60. package/lib/session-search.js +4 -299
  61. package/lib/sessions.js +467 -82
  62. package/lib/smtp.js +0 -15
  63. package/lib/user-presence.js +0 -12
  64. package/lib/users-preferences.js +2 -2
  65. package/lib/users.js +0 -5
  66. package/lib/utils.js +56 -0
  67. package/lib/ws-schema.js +3 -14
  68. package/lib/yoke/adapters/codex.js +96 -10
  69. package/lib/yoke/codex-local-image.js +79 -0
  70. package/package.json +1 -1
  71. package/scripts/postinstall.js +16 -11
  72. package/lib/ask-user-mcp-server.js +0 -121
  73. package/lib/project-memory.js +0 -39
  74. package/lib/public/console-icon.png +0 -0
  75. package/lib/public/css/debate.css +0 -1610
  76. package/lib/public/favicon-banded-32.png +0 -0
  77. package/lib/public/favicon-dark.svg +0 -32
  78. package/lib/public/favicon.svg +0 -32
  79. package/lib/public/icon-banded-96.png +0 -0
  80. package/lib/public/icons/email/gmail.svg +0 -7
  81. package/lib/public/icons/email/outlook.svg +0 -35
  82. package/lib/public/icons/email/yahoo.svg +0 -1
  83. package/lib/public/modules/events.js +0 -21
  84. package/lib/public/modules/state.js +0 -3
  85. package/lib/sdk-worker.js +0 -730
@@ -5,6 +5,7 @@ var { CONFIG_DIR } = require("./config");
5
5
  var _isDevMode = require("./config").isDevMode;
6
6
  var store = require("./store");
7
7
  var audit = require("./audit");
8
+ var { readCappedBody } = require("./utils");
8
9
 
9
10
  // Default token TTL: 30 days. Tokens issued before expiry support was added are
10
11
  // treated as valid for one additional TTL cycle to avoid forced logouts on upgrade.
@@ -334,9 +335,7 @@ function attachAuth(ctx) {
334
335
  res.end(JSON.stringify({ ok: false, locked: true, retryAfter: remaining }));
335
336
  return true;
336
337
  }
337
- var body = "";
338
- req.on("data", function (chunk) { body += chunk; });
339
- req.on("end", function () {
338
+ readCappedBody(req).then(function (body) {
340
339
  try {
341
340
  var data = JSON.parse(body);
342
341
  if (data.step === "verify") {
@@ -378,6 +377,9 @@ function attachAuth(ctx) {
378
377
  res.writeHead(400, { "Content-Type": "application/json" });
379
378
  res.end('{"error":"Invalid request"}');
380
379
  }
380
+ }).catch(function () {
381
+ res.writeHead(413, { "Content-Type": "application/json" });
382
+ res.end('{"error":"Request body too large"}');
381
383
  });
382
384
  return true;
383
385
  }
@@ -390,9 +392,7 @@ function attachAuth(ctx) {
390
392
  res.end('{"error":"Admin already exists"}');
391
393
  return true;
392
394
  }
393
- var body = "";
394
- req.on("data", function (chunk) { body += chunk; });
395
- req.on("end", function () {
395
+ readCappedBody(req).then(function (body) {
396
396
  try {
397
397
  var data = JSON.parse(body);
398
398
  if (!users.validateSetupCode(data.setupCode)) {
@@ -452,6 +452,9 @@ function attachAuth(ctx) {
452
452
  res.writeHead(400, { "Content-Type": "application/json" });
453
453
  res.end('{"error":"Invalid request"}');
454
454
  }
455
+ }).catch(function () {
456
+ res.writeHead(413, { "Content-Type": "application/json" });
457
+ res.end('{"error":"Request body too large"}');
455
458
  });
456
459
  return true;
457
460
  }
@@ -469,9 +472,7 @@ function attachAuth(ctx) {
469
472
  res.end(JSON.stringify({ ok: false, locked: true, retryAfter: remaining }));
470
473
  return true;
471
474
  }
472
- var body = "";
473
- req.on("data", function (chunk) { body += chunk; });
474
- req.on("end", function () {
475
+ readCappedBody(req).then(function (body) {
475
476
  try {
476
477
  var data = JSON.parse(body);
477
478
  var username = data.username || null;
@@ -503,6 +504,9 @@ function attachAuth(ctx) {
503
504
  res.writeHead(400, { "Content-Type": "application/json" });
504
505
  res.end('{"error":"Invalid request"}');
505
506
  }
507
+ }).catch(function () {
508
+ res.writeHead(413, { "Content-Type": "application/json" });
509
+ res.end('{"error":"Request body too large"}');
506
510
  });
507
511
  return true;
508
512
  }
@@ -521,9 +525,7 @@ function attachAuth(ctx) {
521
525
  res.end(JSON.stringify({ ok: false, locked: true, retryAfter: remaining }));
522
526
  return true;
523
527
  }
524
- var body = "";
525
- req.on("data", function (chunk) { body += chunk; });
526
- req.on("end", function () {
528
+ readCappedBody(req).then(function (body) {
527
529
  try {
528
530
  var data = JSON.parse(body);
529
531
  if (!data.email) {
@@ -555,6 +557,9 @@ function attachAuth(ctx) {
555
557
  res.writeHead(400, { "Content-Type": "application/json" });
556
558
  res.end('{"error":"Invalid request"}');
557
559
  }
560
+ }).catch(function () {
561
+ res.writeHead(413, { "Content-Type": "application/json" });
562
+ res.end('{"error":"Request body too large"}');
558
563
  });
559
564
  return true;
560
565
  }
@@ -573,9 +578,7 @@ function attachAuth(ctx) {
573
578
  res.end(JSON.stringify({ ok: false, locked: true, retryAfter: remaining }));
574
579
  return true;
575
580
  }
576
- var body = "";
577
- req.on("data", function (chunk) { body += chunk; });
578
- req.on("end", function () {
581
+ readCappedBody(req).then(function (body) {
579
582
  try {
580
583
  var data = JSON.parse(body);
581
584
  if (!data.email || !data.code) {
@@ -607,15 +610,16 @@ function attachAuth(ctx) {
607
610
  res.writeHead(400, { "Content-Type": "application/json" });
608
611
  res.end('{"error":"Invalid request"}');
609
612
  }
613
+ }).catch(function () {
614
+ res.writeHead(413, { "Content-Type": "application/json" });
615
+ res.end('{"error":"Request body too large"}');
610
616
  });
611
617
  return true;
612
618
  }
613
619
 
614
620
  // Invite registration
615
621
  if (req.method === "POST" && fullUrl === "/auth/register") {
616
- var body = "";
617
- req.on("data", function (chunk) { body += chunk; });
618
- req.on("end", function () {
622
+ readCappedBody(req).then(function (body) {
619
623
  try {
620
624
  var data = JSON.parse(body);
621
625
  var validation = users.validateInvite(data.inviteCode);
@@ -682,6 +686,9 @@ function attachAuth(ctx) {
682
686
  res.writeHead(400, { "Content-Type": "application/json" });
683
687
  res.end('{"error":"Invalid request"}');
684
688
  }
689
+ }).catch(function () {
690
+ res.writeHead(413, { "Content-Type": "application/json" });
691
+ res.end('{"error":"Request body too large"}');
685
692
  });
686
693
  return true;
687
694
  }
package/lib/server-dm.js CHANGED
@@ -36,7 +36,7 @@ function attachDm(ctx) {
36
36
  };
37
37
  }
38
38
  }
39
- ws.send(JSON.stringify({ type: "dm_list", dms: dmList, mates: [] }));
39
+ ws.send(JSON.stringify({ type: "dm_list", dms: dmList }));
40
40
  return true;
41
41
  }
42
42
 
@@ -5,6 +5,14 @@ var https = require("https");
5
5
 
6
6
  var skillsCache = {};
7
7
 
8
+ // lr-5e70: rebuildable-cache drop hook consumed by lib/memory-shed.js on a
9
+ // MemoryHigh watermark crossing. Every entry is a TTL-cached (5 min) fetch
10
+ // of a skills.sh proxy response — safe to drop unconditionally, since the
11
+ // next request for any key just re-fetches and re-populates it.
12
+ function dropSkillsCache() {
13
+ skillsCache = {};
14
+ }
15
+
8
16
  function httpGet(url) {
9
17
  return new Promise(function (resolve, reject) {
10
18
  var mod = url.startsWith("https") ? https : http;
@@ -278,4 +286,4 @@ function attachSkills(ctx) {
278
286
  };
279
287
  }
280
288
 
281
- module.exports = { attachSkills: attachSkills };
289
+ module.exports = { attachSkills: attachSkills, dropSkillsCache: dropSkillsCache };
package/lib/server.js CHANGED
@@ -17,6 +17,7 @@ var serverPalette = require("./server-palette");
17
17
  var { CONFIG_DIR } = require("./config");
18
18
  var { provisionLinuxUser } = require("./os-users");
19
19
  var { detectLite } = require("./lite-detect");
20
+ var { readCappedBody } = require("./utils");
20
21
 
21
22
  var https = require("https");
22
23
  var pkg = require("../package.json");
@@ -142,6 +143,8 @@ function createServer(opts) {
142
143
  var dangerouslySkipPermissions = opts.dangerouslySkipPermissions || false;
143
144
  var osUsers = opts.osUsers || false;
144
145
  var lanHost = opts.lanHost || null;
146
+ // lr-2ea2a7: daemon.json knob overriding the in-heap session-history tail cap.
147
+ var historyInMemMax = opts.historyInMemMax;
145
148
  var onAddProject = opts.onAddProject || null;
146
149
  var onCreateProject = opts.onCreateProject || null;
147
150
  var onCloneProject = opts.onCloneProject || null;
@@ -387,9 +390,7 @@ function createServer(opts) {
387
390
  }
388
391
 
389
392
  if (req.method === "POST" && fullUrl === "/api/push-subscribe" && pushModule) {
390
- var body = "";
391
- req.on("data", function (chunk) { body += chunk; });
392
- req.on("end", function () {
393
+ readCappedBody(req).then(function (body) {
393
394
  try {
394
395
  var parsed = JSON.parse(body);
395
396
  var sub = parsed.subscription || parsed;
@@ -401,6 +402,9 @@ function createServer(opts) {
401
402
  res.writeHead(400);
402
403
  res.end("Bad request");
403
404
  }
405
+ }).catch(function () {
406
+ res.writeHead(413);
407
+ res.end("Request body too large");
404
408
  });
405
409
  return;
406
410
  }
@@ -830,6 +834,16 @@ function createServer(opts) {
830
834
  }
831
835
 
832
836
  wss.handleUpgrade(req, socket, head, function (ws) {
837
+ // Per-client socket faults (e.g. ECONNRESET) emit 'error' on the ws
838
+ // instance. Registered here, before any other listener, so a socket
839
+ // fault during setup (rate-limit wrapper, ctx.handleConnection) can
840
+ // never reach the daemon's uncaughtException handler and trigger
841
+ // gracefulShutdown for every project. Log and drop only this socket.
842
+ ws.on("error", function (err) {
843
+ console.error("[server] WS upgrade socket error for " + wsSlug + ":", err && err.message ? err.message : err);
844
+ try { ws.terminate(); } catch (e) {}
845
+ });
846
+
833
847
  // Apply rate limiting to WS messages
834
848
  var msgCount = 0;
835
849
  var msgWindowStart = Date.now();
@@ -958,6 +972,7 @@ function createServer(opts) {
958
972
  lanHost: lanHost,
959
973
  port: portNum,
960
974
  tls: !!tlsOptions,
975
+ historyInMemMax: historyInMemMax,
961
976
  getProjectCount: function () { return projects.size; },
962
977
  getProjectList: function (userId) {
963
978
  var list = [];
@@ -1,11 +1,8 @@
1
- var fs = require("fs");
2
- var path = require("path");
3
-
4
1
  // ============================================================================
5
- // session-search.js - Unified BM25 search for Clay
2
+ // session-search.js - BM25 search for the command palette (Cmd+K)
6
3
  //
7
- // Single module for ALL search: digests, session history, palette, mate context.
8
- // Callers pass data in, get results out. No BM25 logic leaks outside this file.
4
+ // Was previously a shared engine for Mates digest/knowledge search too
5
+ // (removed lr-316f, pruned lr-439f/lr-509f); only the palette path survives.
9
6
  // ============================================================================
10
7
 
11
8
  // --- BM25 Parameters ---
@@ -219,38 +216,6 @@ function searchIndex(index, query, maxResults) {
219
216
  // 2. DATA SOURCES - convert various data into generic {id, text, meta} docs
220
217
  // ==========================================================================
221
218
 
222
- /**
223
- * Convert a digest entry into a searchable document.
224
- * Important fields get repeated for higher BM25 weight.
225
- * @param {object} digest - parsed digest entry
226
- * @param {number} lineIdx - line index in JSONL file
227
- * @param {string} mateName - optional mate name (for cross-mate search)
228
- */
229
- function digestToDoc(digest, lineIdx, mateName) {
230
- var parts = [];
231
- // High weight
232
- if (digest.topic) { parts.push(digest.topic); parts.push(digest.topic); }
233
- if (digest.tags && digest.tags.length) {
234
- var tagStr = digest.tags.join(" ");
235
- parts.push(tagStr); parts.push(tagStr);
236
- }
237
- // Medium weight
238
- if (digest.summary) parts.push(digest.summary);
239
- if (digest.key_quotes && digest.key_quotes.length) parts.push(digest.key_quotes.join(" "));
240
- if (digest.user_intent) parts.push(digest.user_intent);
241
- if (digest.user_context) parts.push(digest.user_context);
242
- // Normal weight
243
- if (digest.my_position) parts.push(digest.my_position);
244
- if (digest.decisions) parts.push(digest.decisions);
245
- if (digest.open_items) parts.push(digest.open_items);
246
- if (digest.user_sentiment) parts.push(digest.user_sentiment);
247
- if (digest.other_perspectives) parts.push(digest.other_perspectives);
248
- if (digest.outcome) parts.push(digest.outcome);
249
- if (digest.my_role) parts.push(digest.my_role);
250
-
251
- return { id: "digest:" + (mateName || "self") + ":" + lineIdx, text: parts.join(" "), meta: { source: "digest", lineIdx: lineIdx, digest: digest, mateName: mateName || null } };
252
- }
253
-
254
219
  /**
255
220
  * Convert a session's history into searchable documents.
256
221
  * Groups consecutive deltas into turns for better context.
@@ -315,193 +280,10 @@ function sessionHistoryToDocs(session) {
315
280
  return docs;
316
281
  }
317
282
 
318
- /**
319
- * Load digests from JSONL file into doc array.
320
- * @param {string} digestFilePath
321
- * @param {object} opts - { dateFrom, dateTo, mateName }
322
- */
323
- function loadDigestDocs(digestFilePath, opts) {
324
- opts = opts || {};
325
- var docs = [];
326
- try {
327
- if (!fs.existsSync(digestFilePath)) return docs;
328
- var lines = fs.readFileSync(digestFilePath, "utf8").trim().split("\n");
329
- for (var i = 0; i < lines.length; i++) {
330
- if (!lines[i].trim()) continue;
331
- try {
332
- var entry = JSON.parse(lines[i]);
333
- if (opts.dateFrom && entry.date && entry.date < opts.dateFrom) continue;
334
- if (opts.dateTo && entry.date && entry.date > opts.dateTo) continue;
335
- docs.push(digestToDoc(entry, i, opts.mateName || null));
336
- } catch (e) {}
337
- }
338
- } catch (e) {}
339
- return docs;
340
- }
341
-
342
283
  // ==========================================================================
343
- // 3. HIGH-LEVEL SEARCH APIs - the public interface
284
+ // 3. HIGH-LEVEL SEARCH API - the public interface
344
285
  // ==========================================================================
345
286
 
346
- /**
347
- * Search a mate's digests.
348
- * @param {string} digestFilePath
349
- * @param {string} query
350
- * @param {object} opts - { maxResults, minScore, dateFrom, dateTo }
351
- * @returns {Array} [{ digest, score, lineIdx }]
352
- */
353
- function searchDigests(digestFilePath, query, opts) {
354
- opts = opts || {};
355
- if (!query || !query.trim()) return [];
356
-
357
- var docs = loadDigestDocs(digestFilePath, opts);
358
- if (docs.length === 0) return [];
359
-
360
- var index = buildIndex(docs);
361
- var results = searchIndex(index, query, opts.maxResults || MAX_RESULTS);
362
-
363
- if (opts.minScore) {
364
- results = results.filter(function (r) { return r.score >= opts.minScore; });
365
- }
366
-
367
- // Map back to caller-friendly format
368
- return results.map(function (r) {
369
- return { digest: r.meta.digest, score: r.score, lineIdx: r.meta.lineIdx };
370
- });
371
- }
372
-
373
- /**
374
- * Search a mate's session history (full conversation turns).
375
- * @param {Array} sessions - Array of session objects with .history, .localId, .title
376
- * @param {string} query
377
- * @param {object} opts - { maxResults, minScore }
378
- * @returns {Array} [{ sessionId, sessionTitle, turnIdx, turnType, snippet, score }]
379
- */
380
- function searchSessions(sessions, query, opts) {
381
- opts = opts || {};
382
- if (!query || !query.trim() || !sessions || sessions.length === 0) return [];
383
-
384
- var docs = [];
385
- for (var s = 0; s < sessions.length; s++) {
386
- var sessionDocs = sessionHistoryToDocs(sessions[s]);
387
- for (var d = 0; d < sessionDocs.length; d++) {
388
- docs.push(sessionDocs[d]);
389
- }
390
- }
391
-
392
- if (docs.length === 0) return [];
393
-
394
- var index = buildIndex(docs);
395
- var results = searchIndex(index, query, opts.maxResults || MAX_RESULTS);
396
-
397
- if (opts.minScore) {
398
- results = results.filter(function (r) { return r.score >= opts.minScore; });
399
- }
400
-
401
- return results.map(function (r) {
402
- return {
403
- sessionId: r.meta.sessionId,
404
- sessionTitle: r.meta.sessionTitle,
405
- turnIdx: r.meta.turnIdx,
406
- turnType: r.meta.turnType,
407
- snippet: r.meta.snippet,
408
- score: r.score
409
- };
410
- });
411
- }
412
-
413
- /**
414
- * Unified mate search: digests + session history + knowledge files combined.
415
- * Returns merged, re-ranked results from all sources.
416
- * For global search (e.g. Ally), pass otherDigests array with paths and mate names.
417
- * @param {object} opts - { digestFilePath, otherDigests, sessions, knowledgeFiles, query, maxResults, minScore }
418
- * otherDigests: [{ path: string, mateName: string }] - other mates' digest files
419
- * knowledgeFiles: [{ filePath: string, name: string, mateName: string }] - knowledge files to search
420
- * @returns {Array} [{ source: "digest"|"session"|"knowledge", score, mateName, ... }]
421
- */
422
- function searchMate(opts) {
423
- if (!opts || !opts.query || !opts.query.trim()) return [];
424
- var maxResults = opts.maxResults || MAX_RESULTS;
425
- var allDocs = [];
426
-
427
- // Collect own digest docs
428
- if (opts.digestFilePath) {
429
- var ownDocs = loadDigestDocs(opts.digestFilePath);
430
- for (var od = 0; od < ownDocs.length; od++) allDocs.push(ownDocs[od]);
431
- }
432
-
433
- // Collect other mates' digest docs (global search)
434
- if (opts.otherDigests && opts.otherDigests.length > 0) {
435
- for (var oi = 0; oi < opts.otherDigests.length; oi++) {
436
- var other = opts.otherDigests[oi];
437
- var otherDocs = loadDigestDocs(other.path, { mateName: other.mateName });
438
- for (var oid = 0; oid < otherDocs.length; oid++) allDocs.push(otherDocs[oid]);
439
- }
440
- }
441
-
442
- // Collect session history docs
443
- if (opts.sessions && opts.sessions.length > 0) {
444
- for (var si = 0; si < opts.sessions.length; si++) {
445
- var sDocs = sessionHistoryToDocs(opts.sessions[si]);
446
- for (var sd = 0; sd < sDocs.length; sd++) allDocs.push(sDocs[sd]);
447
- }
448
- }
449
-
450
- // Collect knowledge file docs
451
- if (opts.knowledgeFiles && opts.knowledgeFiles.length > 0) {
452
- for (var ki = 0; ki < opts.knowledgeFiles.length; ki++) {
453
- var kf = opts.knowledgeFiles[ki];
454
- try {
455
- if (!fs.existsSync(kf.filePath)) continue;
456
- var content = fs.readFileSync(kf.filePath, "utf8").trim();
457
- if (!content || content.length < 20) continue;
458
- // Cap at 4000 chars to avoid oversized docs dominating the index
459
- var capped = content.length > 4000 ? content.substring(0, 4000) : content;
460
- allDocs.push({
461
- id: "knowledge:" + (kf.mateName || "self") + ":" + kf.name,
462
- text: kf.name + " " + kf.name + " " + capped,
463
- meta: {
464
- source: "knowledge",
465
- fileName: kf.name,
466
- mateName: kf.mateName || null,
467
- snippet: content.length > 200 ? content.substring(0, 200) + "..." : content
468
- }
469
- });
470
- } catch (e) {}
471
- }
472
- }
473
-
474
- if (allDocs.length === 0) return [];
475
-
476
- // Build unified index across all sources and search
477
- var index = buildIndex(allDocs);
478
- var results = searchIndex(index, opts.query, maxResults);
479
-
480
- if (opts.minScore) {
481
- results = results.filter(function (r) { return r.score >= opts.minScore; });
482
- }
483
-
484
- return results.map(function (r) {
485
- var out = { source: r.meta.source, score: r.score };
486
- if (r.meta.source === "digest") {
487
- out.digest = r.meta.digest;
488
- out.lineIdx = r.meta.lineIdx;
489
- out.mateName = r.meta.mateName || null;
490
- } else if (r.meta.source === "knowledge") {
491
- out.fileName = r.meta.fileName;
492
- out.mateName = r.meta.mateName || null;
493
- out.snippet = r.meta.snippet;
494
- } else {
495
- out.sessionId = r.meta.sessionId;
496
- out.sessionTitle = r.meta.sessionTitle;
497
- out.turnIdx = r.meta.turnIdx;
498
- out.turnType = r.meta.turnType;
499
- out.snippet = r.meta.snippet;
500
- }
501
- return out;
502
- });
503
- }
504
-
505
287
  /**
506
288
  * Search for command palette (Cmd+K). Replaces substring search in server.js.
507
289
  * Accepts pre-collected session data from all projects.
@@ -611,84 +393,7 @@ function searchPalette(projectSessions, query, opts) {
611
393
  return deduped.slice(0, maxResults);
612
394
  }
613
395
 
614
- // ==========================================================================
615
- // 4. FORMATTERS - convert search results to context strings
616
- // ==========================================================================
617
-
618
- /**
619
- * Format search results for injection into mate mention/DM context.
620
- * Includes both digest and session history results.
621
- * @param {Array} results - output from searchMate()
622
- * @returns {string} Formatted context string, or "" if empty
623
- */
624
- function formatForContext(results) {
625
- if (!results || results.length === 0) return "";
626
-
627
- var lines = ["\n\nRelevant past context (searched by current topic):"];
628
-
629
- for (var i = 0; i < results.length; i++) {
630
- var r = results[i];
631
- var scorePct = Math.round(r.score * 10) / 10;
632
-
633
- if (r.source === "digest") {
634
- var d = r.digest;
635
- var line = "- [" + (d.date || "?") + "] ";
636
- if (r.mateName) {
637
- line += "(@" + r.mateName + ") ";
638
- }
639
- line += (d.topic || "unknown");
640
- if (d.summary) line += " -- " + d.summary;
641
- if (d.my_position) line += " | Position: " + d.my_position;
642
- if (d.decisions) line += " | Decisions: " + d.decisions;
643
- if (d.key_quotes && d.key_quotes.length > 0) {
644
- line += " | Quotes: " + d.key_quotes.slice(0, 2).join("; ");
645
- }
646
- line += " (relevance: " + scorePct + ")";
647
- lines.push(line);
648
- } else if (r.source === "knowledge") {
649
- var line = "- [knowledge: " + (r.fileName || "?") + "] ";
650
- if (r.mateName) line += "(@" + r.mateName + ") ";
651
- line += (r.snippet || "(no preview)");
652
- line += " (relevance: " + scorePct + ")";
653
- lines.push(line);
654
- } else if (r.source === "session") {
655
- var line = "- [session: " + (r.sessionTitle || "?") + "] ";
656
- line += (r.snippet || "(no preview)");
657
- line += " (relevance: " + scorePct + ")";
658
- lines.push(line);
659
- }
660
- }
661
-
662
- return lines.join("\n");
663
- }
664
-
665
- /**
666
- * Format digest search results for the memory UI panel.
667
- * @param {Array} results - output from searchDigests()
668
- * @returns {Array} [{ entry, score }]
669
- */
670
- function formatForMemoryUI(results) {
671
- return results.map(function (r) {
672
- var entry = r.digest;
673
- entry.index = r.lineIdx;
674
- return { entry: entry, score: Math.round(r.score * 100) / 100 };
675
- });
676
- }
677
-
678
396
  module.exports = {
679
- // Core (exposed for testing / advanced use)
680
- tokenize: tokenize,
681
- buildIndex: buildIndex,
682
- searchIndex: searchIndex,
683
-
684
- // High-level search APIs
685
- searchDigests: searchDigests,
686
- searchSessions: searchSessions,
687
- searchMate: searchMate,
688
397
  searchPalette: searchPalette,
689
-
690
- // Formatters
691
- formatForContext: formatForContext,
692
- formatForMemoryUI: formatForMemoryUI,
693
398
  };
694
399