gm-plugkit 2.0.1306 → 2.0.1307

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gm-plugkit",
3
- "version": "2.0.1306",
3
+ "version": "2.0.1307",
4
4
  "description": "Bootstrap and daemon-spawn tool for gm plugkit binary. Downloads the correct platform binary, verifies SHA256, and starts the spool watcher daemon. Includes plugkit-wasm-wrapper for WASM-based spool watching.",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -1078,6 +1078,32 @@ function kvNamespaceDirs(ns) {
1078
1078
  return out;
1079
1079
  }
1080
1080
 
1081
+ function enabledDisciplineNamespaces(baseNs) {
1082
+ const projectRoot = process.env.CLAUDE_PROJECT_DIR || process.cwd();
1083
+ const set = new Set([baseNs]);
1084
+ try {
1085
+ const enabledPath = path.join(projectRoot, '.gm', 'disciplines', 'enabled.txt');
1086
+ if (fs.existsSync(enabledPath)) {
1087
+ const lines = fs.readFileSync(enabledPath, 'utf-8').split(/\r?\n/);
1088
+ for (const ln of lines) {
1089
+ const name = ln.trim();
1090
+ if (name && !name.startsWith('#')) set.add(name);
1091
+ }
1092
+ }
1093
+ } catch (_) {}
1094
+ return Array.from(set);
1095
+ }
1096
+
1097
+ function jaccardOverlap(a, b) {
1098
+ if (!a || !b) return 0;
1099
+ const tokenize = (s) => new Set(String(s).toLowerCase().split(/[^a-z0-9]+/).filter(t => t.length >= 3));
1100
+ const A = tokenize(a), B = tokenize(b);
1101
+ if (A.size === 0 || B.size === 0) return 0;
1102
+ let inter = 0;
1103
+ for (const t of A) if (B.has(t)) inter++;
1104
+ return inter / (A.size + B.size - inter);
1105
+ }
1106
+
1081
1107
  const __tasks = new Map();
1082
1108
 
1083
1109
  function tasksDir(cwd) {
@@ -1409,8 +1435,8 @@ function makeHostFunctions(instanceRef) {
1409
1435
  if (!raw) return writeWasmJson(instanceRef.value, []);
1410
1436
  let parsedQ;
1411
1437
  try { parsedQ = JSON.parse(raw); } catch (_) { parsedQ = { query: raw }; }
1412
- const q = parsedQ.query || raw;
1413
1438
  const namespace = parsedQ.namespace || 'default';
1439
+ const sigil = parsedQ.sigil || parsedQ.discipline_sigil || null;
1414
1440
  const extractVec = (e) => {
1415
1441
  if (Array.isArray(e)) return e;
1416
1442
  if (Array.isArray(e?.data?.[0]?.embedding)) return e.data[0].embedding;
@@ -1423,37 +1449,58 @@ function makeHostFunctions(instanceRef) {
1423
1449
  if (process.env.PLUGKIT_DEBUG) console.error('[plugkit-wasm] host_vec_search: no embedding in query, raw=', raw.slice(0, 200));
1424
1450
  return writeWasmJson(instanceRef.value, []);
1425
1451
  }
1426
- const vecDirs = kvNamespaceDirs(`${namespace}-vec`);
1427
- const dataDirs = kvNamespaceDirs(namespace);
1428
- if (vecDirs.length === 0 || dataDirs.length === 0) {
1429
- return writeWasmJson(instanceRef.value, []);
1430
- }
1452
+ const namespaces = sigil ? [namespace] : enabledDisciplineNamespaces(namespace);
1453
+ const HALF_LIFE_MS = 30 * 24 * 60 * 60 * 1000;
1454
+ const DEDUP_JACCARD = 0.7;
1455
+ const RECENCY_FLOOR = 0.4;
1456
+ const nowMs = Date.now();
1431
1457
  const scored = [];
1432
1458
  const seen = new Set();
1433
- for (const vecDir of vecDirs) {
1434
- for (const f of fs.readdirSync(vecDir)) {
1435
- if (!f.endsWith('.json')) continue;
1436
- const key = f.replace(/\.json$/, '');
1437
- if (seen.has(key)) continue;
1438
- seen.add(key);
1439
- let emb;
1440
- try { emb = JSON.parse(fs.readFileSync(path.join(vecDir, f), 'utf-8')); }
1441
- catch (_) { continue; }
1442
- const vector = Array.isArray(emb?.data?.[0]?.embedding) ? emb.data[0].embedding
1443
- : Array.isArray(emb?.embedding) ? emb.embedding
1444
- : Array.isArray(emb) ? emb : null;
1445
- if (!vector) continue;
1446
- const score = cosineSim(queryEmbedding, vector);
1447
- let text = '';
1448
- for (const dataDir of dataDirs) {
1449
- const valuePath = path.join(dataDir, `${key}.json`);
1450
- if (fs.existsSync(valuePath)) { text = fs.readFileSync(valuePath, 'utf-8'); break; }
1459
+ for (const ns of namespaces) {
1460
+ const vecDirs = kvNamespaceDirs(`${ns}-vec`);
1461
+ const dataDirs = kvNamespaceDirs(ns);
1462
+ if (vecDirs.length === 0 || dataDirs.length === 0) continue;
1463
+ for (const vecDir of vecDirs) {
1464
+ for (const f of fs.readdirSync(vecDir)) {
1465
+ if (!f.endsWith('.json')) continue;
1466
+ const key = f.replace(/\.json$/, '');
1467
+ const seenKey = `${ns}::${key}`;
1468
+ if (seen.has(seenKey)) continue;
1469
+ seen.add(seenKey);
1470
+ const vecPath = path.join(vecDir, f);
1471
+ let emb, mtimeMs;
1472
+ try {
1473
+ emb = JSON.parse(fs.readFileSync(vecPath, 'utf-8'));
1474
+ mtimeMs = fs.statSync(vecPath).mtimeMs;
1475
+ } catch (_) { continue; }
1476
+ const vector = Array.isArray(emb?.data?.[0]?.embedding) ? emb.data[0].embedding
1477
+ : Array.isArray(emb?.embedding) ? emb.embedding
1478
+ : Array.isArray(emb) ? emb : null;
1479
+ if (!vector) continue;
1480
+ const cos = cosineSim(queryEmbedding, vector);
1481
+ const ageMs = Math.max(0, nowMs - mtimeMs);
1482
+ const recency = RECENCY_FLOOR + (1 - RECENCY_FLOOR) * Math.exp(-ageMs / HALF_LIFE_MS);
1483
+ const score = cos * recency;
1484
+ let text = '';
1485
+ for (const dataDir of dataDirs) {
1486
+ const valuePath = path.join(dataDir, `${key}.json`);
1487
+ if (fs.existsSync(valuePath)) { text = fs.readFileSync(valuePath, 'utf-8'); break; }
1488
+ }
1489
+ scored.push({ key, text, score, cos, recency, namespace: ns });
1451
1490
  }
1452
- scored.push({ key, text, score });
1453
1491
  }
1454
1492
  }
1455
1493
  scored.sort((a, b) => b.score - a.score);
1456
- return writeWasmJson(instanceRef.value, scored.slice(0, k_));
1494
+ const out = [];
1495
+ for (const hit of scored) {
1496
+ let dup = false;
1497
+ for (const kept of out) {
1498
+ if (jaccardOverlap(hit.text, kept.text) >= DEDUP_JACCARD) { dup = true; break; }
1499
+ }
1500
+ if (!dup) out.push(hit);
1501
+ if (out.length >= k_) break;
1502
+ }
1503
+ return writeWasmJson(instanceRef.value, out);
1457
1504
  } catch (e) {
1458
1505
  console.error('[plugkit-wasm] host_vec_search error:', e.message);
1459
1506
  return writeWasmJson(instanceRef.value, []);