@xuda.io/ai_module 1.1.5419 → 1.1.5421

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 (2) hide show
  1. package/index.mjs +108 -34
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6919,54 +6919,128 @@ const get_image_blob_from_downloaded_image = async function (url) {
6919
6919
  return images[0];
6920
6920
  };
6921
6921
 
6922
- const get_chat_picture_cache = async function (uid, docType, key) {
6923
- if (!key || typeof key !== 'string') return null;
6922
+ // const get_chat_picture_cache = async function (uid, docType, key) {
6923
+ // if (!key || typeof key !== 'string') return null;
6924
+
6925
+ // const words = key
6926
+ // .trim()
6927
+ // .toLowerCase()
6928
+ // .split(/\s+/)
6929
+ // .filter((w) => w.length > 1); // ignore very short words
6930
+
6931
+ // if (words.length < 3) {
6932
+ // // Direct exact match for short names
6933
+ // const ret = await db_module.find_couch_query('xuda_cache', {
6934
+ // selector: { docType, key: key.toLowerCase().trim() },
6935
+ // limit: 1,
6936
+ // });
6937
+
6938
+ // return ret.docs[0]?.file ?? null;
6939
+ // }
6924
6940
 
6925
- const words = key
6926
- .trim()
6927
- .toLowerCase()
6928
- .split(/\s+/)
6929
- .filter((w) => w.length > 1); // ignore very short words
6941
+ // // Try combinations of 3 words but **limit** the number of attempts
6942
+ // const MAX_ATTEMPTS = 15; // prevent explosion on long names
6943
+ // const combinations = [];
6930
6944
 
6931
- if (words.length < 3) {
6932
- // Direct exact match for short names
6933
- const ret = await db_module.find_couch_query('xuda_cache', {
6934
- selector: { docType, key: key.toLowerCase().trim() },
6935
- limit: 1,
6936
- });
6945
+ // for (let i = 0; i < words.length - 2 && combinations.length < MAX_ATTEMPTS; i++) {
6946
+ // for (let j = i + 1; j < words.length - 1 && combinations.length < MAX_ATTEMPTS; j++) {
6947
+ // for (let k = j + 1; k < words.length && combinations.length < MAX_ATTEMPTS; k++) {
6948
+ // combinations.push([words[i], words[j], words[k]]);
6949
+ // }
6950
+ // }
6951
+ // }
6937
6952
 
6938
- return ret.docs[0]?.file ?? null;
6939
- }
6953
+ // for (const threeWords of combinations) {
6954
+ // const query = {
6955
+ // selector: {
6956
+ // docType,
6957
+ // $and: threeWords.map((word) => ({
6958
+ // key: { $regex: `(?i)\\b${word}\\b` },
6959
+ // })),
6960
+ // },
6961
+ // limit: 1,
6962
+ // };
6940
6963
 
6941
- // Try combinations of 3 words — but **limit** the number of attempts
6942
- const MAX_ATTEMPTS = 15; // prevent explosion on long names
6943
- const combinations = [];
6964
+ // const ret = await db_module.find_couch_query('xuda_cache', query);
6944
6965
 
6945
- for (let i = 0; i < words.length - 2 && combinations.length < MAX_ATTEMPTS; i++) {
6946
- for (let j = i + 1; j < words.length - 1 && combinations.length < MAX_ATTEMPTS; j++) {
6947
- for (let k = j + 1; k < words.length && combinations.length < MAX_ATTEMPTS; k++) {
6948
- combinations.push([words[i], words[j], words[k]]);
6949
- }
6950
- }
6951
- }
6966
+ // if (ret.docs.length > 0) {
6967
+ // account_msa.save_cache_hit(ret.docs[0]._id);
6968
+ // return ret.docs[0].file;
6969
+ // }
6970
+ // }
6971
+
6972
+ // return null;
6973
+ // };
6952
6974
 
6953
- for (const threeWords of combinations) {
6954
- const query = {
6975
+ const get_chat_picture_cache = async function (uid, docType, key) {
6976
+ // 1. Validation & Normalization
6977
+ if (!key || typeof key !== 'string') return null;
6978
+
6979
+ const normalizedKey = key.trim().toLowerCase();
6980
+ if (!normalizedKey) return null;
6981
+
6982
+ const words = normalizedKey
6983
+ .split(/\s+/)
6984
+ .filter((w) => w.length > 1)
6985
+ .filter((w, i, arr) => arr.indexOf(w) === i);
6986
+
6987
+ // ────────────────────────────────────────────────
6988
+ // Case 1: Exact Match (High Confidence)
6989
+ // ────────────────────────────────────────────────
6990
+ if (words.length <= 2) {
6991
+ const exactQuery = {
6955
6992
  selector: {
6956
6993
  docType,
6957
- $and: threeWords.map((word) => ({
6958
- key: { $regex: `(?i)\\b${word}\\b` },
6959
- })),
6994
+ key: normalizedKey,
6960
6995
  },
6961
6996
  limit: 1,
6962
6997
  };
6963
6998
 
6964
- const ret = await db_module.find_couch_query('xuda_cache', query);
6999
+ try {
7000
+ const exactRet = await db_module.find_couch_query('xuda_cache', exactQuery);
6965
7001
 
6966
- if (ret.docs.length > 0) {
6967
- account_msa.save_cache_hit(ret.docs[0]._id);
6968
- return ret.docs[0].file;
7002
+ // Use optional chaining to prevent "undefined" crashes
7003
+ if (exactRet?.docs?.length > 0) {
7004
+ const doc = exactRet.docs[0];
7005
+ if (doc._id) account_msa.save_cache_hit(doc._id);
7006
+ return doc.file ?? null;
7007
+ }
7008
+ } catch (err) {
7009
+ console.error('Exact match lookup error:', err.message);
6969
7010
  }
7011
+
7012
+ if (words.length === 0) return null;
7013
+ }
7014
+
7015
+ // ────────────────────────────────────────────────
7016
+ // Case 2: Fuzzy match using key_words array
7017
+ // ────────────────────────────────────────────────
7018
+ const searchWords = words.slice(0, 3);
7019
+ if (searchWords.length === 0) return null;
7020
+
7021
+ const fuzzyQuery = {
7022
+ selector: {
7023
+ docType,
7024
+ // Ensure 'ts' is in the selector if you plan to sort by it
7025
+ ts: { $gt: 0 },
7026
+ key_words: { $all: searchWords },
7027
+ },
7028
+ sort: [{ ts: 'desc' }],
7029
+ limit: 1,
7030
+ };
7031
+
7032
+ try {
7033
+ const fuzzyRet = await db_module.find_couch_query('xuda_cache', fuzzyQuery);
7034
+
7035
+ // Defensive check against null/undefined response
7036
+ if (fuzzyRet?.docs?.length > 0) {
7037
+ const doc = fuzzyRet.docs[0];
7038
+ if (doc._id) account_msa.save_cache_hit(doc._id);
7039
+ return doc.file ?? null;
7040
+ }
7041
+ } catch (err) {
7042
+ // This catches the 'no_usable_index' error without crashing the app
7043
+ console.warn(`Fuzzy lookup failed for [${searchWords}]:`, err.message);
6970
7044
  }
6971
7045
 
6972
7046
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5419",
3
+ "version": "1.1.5421",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",