engine7 7.1.152 → 7.1.153

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.
@@ -1807,7 +1807,22 @@ function cjkBigrams(text2) {
1807
1807
  }
1808
1808
  return out;
1809
1809
  }
1810
- var DEFAULT_STOPWORDS, MIN_TOKEN_LENGTH, jieba;
1810
+ function invisibleCjkNeedles(candidates) {
1811
+ return [...new Set(candidates.map((token) => token.trim()))].filter((token) => /^[\u4e00-\u9fff]{2,}$/.test(token) && tokenize(token).length === 0).slice(0, 8);
1812
+ }
1813
+ function literalQueryNeedles(text2) {
1814
+ const chars = shortCjkCharCount(text2);
1815
+ if (!chars || chars > 6) return null;
1816
+ const names = invisibleCjkNeedles(engine().cut(String(text2 ?? ""), true));
1817
+ return names.length ? names : queryBigramNeedles(text2);
1818
+ }
1819
+ function queryBigramNeedles(text2) {
1820
+ const chars = shortCjkCharCount(text2);
1821
+ if (!chars || chars > 6) return null;
1822
+ const grams = [...new Set(cjkBigrams(text2))].slice(0, 8);
1823
+ return grams.length ? grams : null;
1824
+ }
1825
+ var DEFAULT_STOPWORDS, MIN_TOKEN_LENGTH, jieba, shortCjkCharCount;
1811
1826
  var init_tokenizer = __esm({
1812
1827
  "src/mem7/core/tokenizer.ts"() {
1813
1828
  "use strict";
@@ -1880,6 +1895,7 @@ var init_tokenizer = __esm({
1880
1895
  ]);
1881
1896
  MIN_TOKEN_LENGTH = 2;
1882
1897
  jieba = null;
1898
+ shortCjkCharCount = (text2) => (String(text2 ?? "").match(/[\u4e00-\u9fff]/g) ?? []).length;
1883
1899
  }
1884
1900
  });
1885
1901
 
@@ -6947,12 +6963,6 @@ var BM25_TEXT_FIELDS = {
6947
6963
  agent_case: ["task_intent", "approach"],
6948
6964
  agent_skill: ["description", "content"]
6949
6965
  };
6950
- function shortCjkQueryBigrams(text2) {
6951
- const chars = (String(text2 ?? "").match(/[\u4e00-\u9fff]/g) ?? []).length;
6952
- if (!chars || chars > 6) return null;
6953
- const grams = [...new Set(cjkBigrams(text2))].slice(0, 8);
6954
- return grams.length ? grams : null;
6955
- }
6956
6966
  var sqlLiteral = (value2) => value2.replace(/'/g, "''");
6957
6967
  var RRF_K = 60;
6958
6968
  var warnedFts = /* @__PURE__ */ new Set();
@@ -7647,24 +7657,43 @@ var Mem7VectorStore = class {
7647
7657
  if (!cur || score > Number(cur._score ?? 0)) best.set(rid, r);
7648
7658
  }
7649
7659
  }
7650
- const grams = shortCjkQueryBigrams(queryText);
7651
- if (grams) {
7660
+ const textCols = BM25_TEXT_FIELDS[kind];
7661
+ const scan = async (terms, op, limit) => {
7662
+ const expr = op === "AND" ? terms.map((g) => `(${textCols.map((col) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" AND ") : textCols.map((col) => `(${terms.map((g) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" OR ");
7663
+ const where2 = compileFilter(opts.filter);
7664
+ return await tbl.query().where(where2 ? `(${where2}) AND (${expr})` : expr).limit(limit).toArray();
7665
+ };
7666
+ const maxScore = [...best.values()].reduce((max, r) => Math.max(max, Number(r._score ?? 0)), 0);
7667
+ const scoreLiteral = (needles2, rows) => {
7668
+ const terms = [.../* @__PURE__ */ new Set([...needles2, ...tokenizeToField(queryText).split(/\s+/).filter(Boolean)])];
7669
+ const occurrences2 = (text2, term) => text2.split(term).length - 1;
7670
+ for (const r of rows) {
7671
+ const rid = String(r.id ?? "");
7672
+ if (!rid) continue;
7673
+ const text2 = textCols.map((col) => String(r[col] ?? "")).join(" ");
7674
+ const matched = terms.filter((term) => text2.includes(term));
7675
+ if (!matched.length || matched.every((term) => !needles2.includes(term))) continue;
7676
+ const full = matched.length === terms.length;
7677
+ const density = matched.reduce((sum, term) => sum + occurrences2(text2, term), 0) / Math.max(text2.length, 1) * 1e3;
7678
+ const score = full ? maxScore + 1 + Math.min(0.99, density / 10) : maxScore > 0 ? maxScore * (0.5 + 0.5 * matched.length / terms.length) : matched.length / terms.length;
7679
+ const cur = best.get(rid);
7680
+ if (cur) {
7681
+ if (full) cur._score = Math.max(Number(cur._score ?? 0), score);
7682
+ } else best.set(rid, { ...r, _score: score });
7683
+ }
7684
+ };
7685
+ const needles = literalQueryNeedles(queryText);
7686
+ if (needles) {
7652
7687
  try {
7653
- const textCols = BM25_TEXT_FIELDS[kind];
7654
- const expr = textCols.map((col) => `(${grams.map((g) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" OR ");
7655
- const where2 = compileFilter(opts.filter);
7656
- const extra = await tbl.query().where(where2 ? `(${where2}) AND (${expr})` : expr).limit(topK * 3).toArray();
7657
- const maxScore = [...best.values()].reduce((max, r) => Math.max(max, Number(r._score ?? 0)), 0);
7658
- for (const r of extra) {
7659
- const rid = String(r.id ?? "");
7660
- if (!rid) continue;
7661
- const text2 = textCols.map((col) => String(r[col] ?? "")).join(" ");
7662
- const matched = grams.filter((gram) => text2.includes(gram)).length;
7663
- if (!matched) continue;
7664
- const score = maxScore + matched / grams.length;
7665
- const cur = best.get(rid);
7666
- if (cur) cur._score = Math.max(Number(cur._score ?? 0), score);
7667
- else best.set(rid, { ...r, _score: score });
7688
+ const terms = [.../* @__PURE__ */ new Set([...needles, ...tokenizeToField(queryText).split(/\s+/).filter(Boolean)])];
7689
+ const [all, any] = await Promise.all([
7690
+ terms.length > 1 ? scan(terms, "AND", topK * 10) : Promise.resolve([]),
7691
+ scan(needles, "OR", topK * 3)
7692
+ ]);
7693
+ scoreLiteral(needles, [...all, ...any]);
7694
+ if (![...best.values()].some((r) => Number(r._score ?? 0) > maxScore)) {
7695
+ const bigrams = [...new Set(needles.flatMap((name) => queryBigramNeedles(name) ?? []))].slice(0, 8);
7696
+ if (bigrams.length && JSON.stringify(bigrams) !== JSON.stringify(needles)) scoreLiteral(bigrams, await scan(bigrams, "OR", topK * 3));
7668
7697
  }
7669
7698
  } catch {
7670
7699
  }
@@ -1822,7 +1822,22 @@ function cjkBigrams(text2) {
1822
1822
  }
1823
1823
  return out;
1824
1824
  }
1825
- var DEFAULT_STOPWORDS, MIN_TOKEN_LENGTH, jieba;
1825
+ function invisibleCjkNeedles(candidates2) {
1826
+ return [...new Set(candidates2.map((token) => token.trim()))].filter((token) => /^[\u4e00-\u9fff]{2,}$/.test(token) && tokenize(token).length === 0).slice(0, 8);
1827
+ }
1828
+ function literalQueryNeedles(text2) {
1829
+ const chars = shortCjkCharCount(text2);
1830
+ if (!chars || chars > 6) return null;
1831
+ const names = invisibleCjkNeedles(engine().cut(String(text2 ?? ""), true));
1832
+ return names.length ? names : queryBigramNeedles(text2);
1833
+ }
1834
+ function queryBigramNeedles(text2) {
1835
+ const chars = shortCjkCharCount(text2);
1836
+ if (!chars || chars > 6) return null;
1837
+ const grams = [...new Set(cjkBigrams(text2))].slice(0, 8);
1838
+ return grams.length ? grams : null;
1839
+ }
1840
+ var DEFAULT_STOPWORDS, MIN_TOKEN_LENGTH, jieba, shortCjkCharCount;
1826
1841
  var init_tokenizer = __esm({
1827
1842
  "src/mem7/core/tokenizer.ts"() {
1828
1843
  "use strict";
@@ -1895,6 +1910,7 @@ var init_tokenizer = __esm({
1895
1910
  ]);
1896
1911
  MIN_TOKEN_LENGTH = 2;
1897
1912
  jieba = null;
1913
+ shortCjkCharCount = (text2) => (String(text2 ?? "").match(/[\u4e00-\u9fff]/g) ?? []).length;
1898
1914
  }
1899
1915
  });
1900
1916
 
@@ -3468,12 +3484,6 @@ function loadArrow() {
3468
3484
  }
3469
3485
  return arrowPromise;
3470
3486
  }
3471
- function shortCjkQueryBigrams(text2) {
3472
- const chars = (String(text2 ?? "").match(/[\u4e00-\u9fff]/g) ?? []).length;
3473
- if (!chars || chars > 6) return null;
3474
- const grams = [...new Set(cjkBigrams(text2))].slice(0, 8);
3475
- return grams.length ? grams : null;
3476
- }
3477
3487
  function buildSchemas(arrow) {
3478
3488
  const vecField = () => new arrow.Field("vector", new arrow.FixedSizeList(VECTOR_DIM, new arrow.Field("item", new arrow.Float32(), true)), true);
3479
3489
  const commonFields = () => [
@@ -4206,24 +4216,43 @@ var init_vector_store = __esm({
4206
4216
  if (!cur || score > Number(cur._score ?? 0)) best.set(rid, r);
4207
4217
  }
4208
4218
  }
4209
- const grams = shortCjkQueryBigrams(queryText);
4210
- if (grams) {
4219
+ const textCols = BM25_TEXT_FIELDS[kind];
4220
+ const scan = async (terms, op, limit2) => {
4221
+ const expr = op === "AND" ? terms.map((g) => `(${textCols.map((col) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" AND ") : textCols.map((col) => `(${terms.map((g) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" OR ");
4222
+ const where2 = compileFilter(opts.filter);
4223
+ return await tbl.query().where(where2 ? `(${where2}) AND (${expr})` : expr).limit(limit2).toArray();
4224
+ };
4225
+ const maxScore = [...best.values()].reduce((max, r) => Math.max(max, Number(r._score ?? 0)), 0);
4226
+ const scoreLiteral = (needles2, rows) => {
4227
+ const terms = [.../* @__PURE__ */ new Set([...needles2, ...tokenizeToField(queryText).split(/\s+/).filter(Boolean)])];
4228
+ const occurrences2 = (text2, term) => text2.split(term).length - 1;
4229
+ for (const r of rows) {
4230
+ const rid = String(r.id ?? "");
4231
+ if (!rid) continue;
4232
+ const text2 = textCols.map((col) => String(r[col] ?? "")).join(" ");
4233
+ const matched = terms.filter((term) => text2.includes(term));
4234
+ if (!matched.length || matched.every((term) => !needles2.includes(term))) continue;
4235
+ const full = matched.length === terms.length;
4236
+ const density = matched.reduce((sum, term) => sum + occurrences2(text2, term), 0) / Math.max(text2.length, 1) * 1e3;
4237
+ const score = full ? maxScore + 1 + Math.min(0.99, density / 10) : maxScore > 0 ? maxScore * (0.5 + 0.5 * matched.length / terms.length) : matched.length / terms.length;
4238
+ const cur = best.get(rid);
4239
+ if (cur) {
4240
+ if (full) cur._score = Math.max(Number(cur._score ?? 0), score);
4241
+ } else best.set(rid, { ...r, _score: score });
4242
+ }
4243
+ };
4244
+ const needles = literalQueryNeedles(queryText);
4245
+ if (needles) {
4211
4246
  try {
4212
- const textCols = BM25_TEXT_FIELDS[kind];
4213
- const expr = textCols.map((col) => `(${grams.map((g) => `contains(${col}, '${sqlLiteral(g)}')`).join(" OR ")})`).join(" OR ");
4214
- const where2 = compileFilter(opts.filter);
4215
- const extra = await tbl.query().where(where2 ? `(${where2}) AND (${expr})` : expr).limit(topK * 3).toArray();
4216
- const maxScore = [...best.values()].reduce((max, r) => Math.max(max, Number(r._score ?? 0)), 0);
4217
- for (const r of extra) {
4218
- const rid = String(r.id ?? "");
4219
- if (!rid) continue;
4220
- const text2 = textCols.map((col) => String(r[col] ?? "")).join(" ");
4221
- const matched = grams.filter((gram) => text2.includes(gram)).length;
4222
- if (!matched) continue;
4223
- const score = maxScore + matched / grams.length;
4224
- const cur = best.get(rid);
4225
- if (cur) cur._score = Math.max(Number(cur._score ?? 0), score);
4226
- else best.set(rid, { ...r, _score: score });
4247
+ const terms = [.../* @__PURE__ */ new Set([...needles, ...tokenizeToField(queryText).split(/\s+/).filter(Boolean)])];
4248
+ const [all, any] = await Promise.all([
4249
+ terms.length > 1 ? scan(terms, "AND", topK * 10) : Promise.resolve([]),
4250
+ scan(needles, "OR", topK * 3)
4251
+ ]);
4252
+ scoreLiteral(needles, [...all, ...any]);
4253
+ if (![...best.values()].some((r) => Number(r._score ?? 0) > maxScore)) {
4254
+ const bigrams = [...new Set(needles.flatMap((name) => queryBigramNeedles(name) ?? []))].slice(0, 8);
4255
+ if (bigrams.length && JSON.stringify(bigrams) !== JSON.stringify(needles)) scoreLiteral(bigrams, await scan(bigrams, "OR", topK * 3));
4227
4256
  }
4228
4257
  } catch {
4229
4258
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "engine7",
3
3
  "license": "Apache-2.0",
4
- "version": "7.1.152",
4
+ "version": "7.1.153",
5
5
  "type": "module",
6
6
  "description": "Engine7 — 给助手一个家 / A home for your AI assistant · https://engine7.ai",
7
7
  "homepage": "https://engine7.ai",