@equationalapplications/core-llm-wiki 4.3.0 → 4.4.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.
package/dist/index.js CHANGED
@@ -1064,14 +1064,14 @@ After running the migration SQL, restart your application.`
1064
1064
  );
1065
1065
  }
1066
1066
  }
1067
- const entityScope = this._entityInClause(scoredEntityIds);
1067
+ const mismatchScope = this._entityInClause(entityIds);
1068
1068
  const mismatchedCount = await this.db.getFirstAsync(
1069
1069
  `SELECT COUNT(*) AS cnt FROM ${this.prefix}entries
1070
- WHERE ${entityScope.clause} AND deleted_at IS NULL
1070
+ WHERE ${mismatchScope.clause} AND deleted_at IS NULL
1071
1071
  AND embedding_blob IS NOT NULL
1072
1072
  AND (CAST(length(embedding_blob) AS INTEGER) % 4 = 0)
1073
1073
  AND (CAST(length(embedding_blob) AS INTEGER) / 4) != ?`,
1074
- [...entityScope.params, queryVec.length]
1074
+ [...mismatchScope.params, queryVec.length]
1075
1075
  );
1076
1076
  if (mismatchedCount && mismatchedCount.cnt > 0) {
1077
1077
  throw new Error(
@@ -1131,16 +1131,16 @@ After running the migration SQL, restart your application.`
1131
1131
  }
1132
1132
  } else {
1133
1133
  if (useRanker) {
1134
- const entityScope2 = this._entityInClause(scoredEntityIds);
1134
+ const entityScope = this._entityInClause(scoredEntityIds);
1135
1135
  candidateRows = await this.db.getAllAsync(
1136
- `SELECT id, entity_id, updated_at, access_count FROM ${this.prefix}entries WHERE ${entityScope2.clause} AND deleted_at IS NULL`,
1137
- entityScope2.params
1136
+ `SELECT id, entity_id, updated_at, access_count FROM ${this.prefix}entries WHERE ${entityScope.clause} AND deleted_at IS NULL`,
1137
+ entityScope.params
1138
1138
  );
1139
1139
  } else {
1140
- const entityScope2 = this._entityInClause(scoredEntityIds);
1140
+ const entityScope = this._entityInClause(scoredEntityIds);
1141
1141
  candidateRows = await this.db.getAllAsync(
1142
- `SELECT id, entity_id, embedding_blob, embedding, updated_at, access_count FROM ${this.prefix}entries WHERE ${entityScope2.clause} AND deleted_at IS NULL`,
1143
- entityScope2.params
1142
+ `SELECT id, entity_id, embedding_blob, embedding, updated_at, access_count FROM ${this.prefix}entries WHERE ${entityScope.clause} AND deleted_at IS NULL`,
1143
+ entityScope.params
1144
1144
  );
1145
1145
  }
1146
1146
  if (weight !== void 0 && weight < 1) {
@@ -1159,32 +1159,42 @@ After running the migration SQL, restart your application.`
1159
1159
  const entityCacheKey = entityIds.length === 1 ? entityIds[0] : entityIds.join("\0");
1160
1160
  let scored;
1161
1161
  if (useRanker) {
1162
- const candidateIds = entityIds.length > 1 || effectivePreFilterLimit !== void 0 ? candidateRows.map((r) => r.id) : void 0;
1162
+ const candidateRowsByEntity = /* @__PURE__ */ new Map();
1163
+ for (const row of candidateRows) {
1164
+ const rows = candidateRowsByEntity.get(row.entity_id) ?? [];
1165
+ rows.push(row);
1166
+ candidateRowsByEntity.set(row.entity_id, rows);
1167
+ }
1163
1168
  try {
1164
- const oversampledLimit = Math.max(maxResults * 2, maxResults + 50);
1165
- scored = await this._rankWithVectorRanker({
1166
- entityId: entityCacheKey,
1167
- queryVec,
1168
- candidateIds,
1169
- candidateRows,
1170
- weight,
1171
- miniSearchScores,
1172
- limit: oversampledLimit
1173
- });
1174
- if (scored.length > 0) {
1175
- const scoredIds2 = new Set(scored.map((s) => s.id));
1176
- const metaMap = /* @__PURE__ */ new Map();
1177
- for (const r of candidateRows) {
1178
- if (scoredIds2.has(r.id)) {
1179
- metaMap.set(r.id, { updated_at: r.updated_at, access_count: r.access_count });
1180
- }
1181
- }
1182
- scored = scored.map((s) => {
1183
- const meta = metaMap.get(s.id);
1184
- return { ...s, updated_at: meta?.updated_at ?? null, access_count: meta?.access_count ?? null };
1185
- });
1186
- }
1169
+ const rankerResultsByEntity = await Promise.all(
1170
+ scoredEntityIds.filter((id) => (candidateRowsByEntity.get(id)?.length ?? 0) > 0).map(async (scopedEntityId) => {
1171
+ const rowsForEntity = candidateRowsByEntity.get(scopedEntityId) ?? [];
1172
+ const candidateIds = effectivePreFilterLimit !== void 0 ? rowsForEntity.map((row) => row.id) : void 0;
1173
+ const ranked = await this._rankWithVectorRanker({
1174
+ entityId: scopedEntityId,
1175
+ queryVec,
1176
+ candidateIds,
1177
+ candidateRows: rowsForEntity,
1178
+ weight,
1179
+ miniSearchScores,
1180
+ limit: Math.max(maxResults * 2, maxResults + 50)
1181
+ });
1182
+ return ranked.map((row) => ({ ...row, entity_id: scopedEntityId }));
1183
+ })
1184
+ );
1185
+ scored = rankerResultsByEntity.flat();
1187
1186
  const scoredIds = new Set(scored.map((s) => s.id));
1187
+ const metadataById = new Map(
1188
+ candidateRows.filter((row) => scoredIds.has(row.id)).map((row) => [row.id, row])
1189
+ );
1190
+ scored = scored.map((row) => {
1191
+ const metadata = metadataById.get(row.id);
1192
+ return {
1193
+ ...row,
1194
+ updated_at: metadata?.updated_at ?? null,
1195
+ access_count: metadata?.access_count ?? null
1196
+ };
1197
+ });
1188
1198
  const isHybrid = weight !== void 0 && weight < 1;
1189
1199
  const maxBackfill = isHybrid ? maxResults : Math.max(0, maxResults - scored.length);
1190
1200
  if (maxBackfill > 0) {
@@ -1332,21 +1342,17 @@ After running the migration SQL, restart your application.`
1332
1342
  });
1333
1343
  const keywordOversampledLimit = Math.max(maxResults * 2, maxResults + 50);
1334
1344
  const topResults = msResults.slice(0, keywordOversampledLimit);
1335
- const resultIds = new Set(topResults.map((r) => r.id));
1336
- const candidateMap = /* @__PURE__ */ new Map();
1337
- for (const r of candidateRows) {
1338
- if (resultIds.has(r.id)) {
1339
- candidateMap.set(r.id, { entity_id: r.entity_id, updated_at: r.updated_at, access_count: r.access_count });
1340
- }
1341
- }
1342
- scored = topResults.map((r) => {
1343
- const meta = candidateMap.get(r.id);
1345
+ const topResultIds = new Set(topResults.map((r) => r.id));
1346
+ const candidateMap = new Map(candidateRows.filter((r) => topResultIds.has(r.id)).map((row) => [row.id, row]));
1347
+ scored = topResults.map((result) => {
1348
+ const metadata = candidateMap.get(result.id);
1349
+ const entityForScore = metadata?.entity_id ?? result.entity_id ?? "";
1344
1350
  return {
1345
- id: r.id,
1346
- entity_id: meta?.entity_id ?? r.entity_id,
1347
- score: r.score ?? 0,
1348
- access_count: meta?.access_count ?? null,
1349
- updated_at: meta?.updated_at ?? null
1351
+ id: result.id,
1352
+ entity_id: entityForScore,
1353
+ score: result.score ?? 0,
1354
+ access_count: metadata?.access_count ?? null,
1355
+ updated_at: metadata?.updated_at ?? null
1350
1356
  };
1351
1357
  });
1352
1358
  } else {