@xuda.io/ai_module 1.1.5420 → 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.
- package/index.mjs +28 -55
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -6973,22 +6973,19 @@ const get_image_blob_from_downloaded_image = async function (url) {
|
|
|
6973
6973
|
// };
|
|
6974
6974
|
|
|
6975
6975
|
const get_chat_picture_cache = async function (uid, docType, key) {
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
}
|
|
6976
|
+
// 1. Validation & Normalization
|
|
6977
|
+
if (!key || typeof key !== 'string') return null;
|
|
6979
6978
|
|
|
6980
6979
|
const normalizedKey = key.trim().toLowerCase();
|
|
6981
|
-
if (!normalizedKey)
|
|
6982
|
-
return null;
|
|
6983
|
-
}
|
|
6980
|
+
if (!normalizedKey) return null;
|
|
6984
6981
|
|
|
6985
6982
|
const words = normalizedKey
|
|
6986
6983
|
.split(/\s+/)
|
|
6987
|
-
.filter((w) => w.length > 1)
|
|
6988
|
-
.filter((w, i, arr) => arr.indexOf(w) === i);
|
|
6984
|
+
.filter((w) => w.length > 1)
|
|
6985
|
+
.filter((w, i, arr) => arr.indexOf(w) === i);
|
|
6989
6986
|
|
|
6990
6987
|
// ────────────────────────────────────────────────
|
|
6991
|
-
// Case 1:
|
|
6988
|
+
// Case 1: Exact Match (High Confidence)
|
|
6992
6989
|
// ────────────────────────────────────────────────
|
|
6993
6990
|
if (words.length <= 2) {
|
|
6994
6991
|
const exactQuery = {
|
|
@@ -6999,77 +6996,53 @@ const get_chat_picture_cache = async function (uid, docType, key) {
|
|
|
6999
6996
|
limit: 1,
|
|
7000
6997
|
};
|
|
7001
6998
|
|
|
7002
|
-
|
|
6999
|
+
try {
|
|
7000
|
+
const exactRet = await db_module.find_couch_query('xuda_cache', exactQuery);
|
|
7003
7001
|
|
|
7004
|
-
|
|
7005
|
-
|
|
7006
|
-
|
|
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);
|
|
7007
7010
|
}
|
|
7008
7011
|
|
|
7009
|
-
|
|
7010
|
-
if (words.length === 0) {
|
|
7011
|
-
return null;
|
|
7012
|
-
}
|
|
7012
|
+
if (words.length === 0) return null;
|
|
7013
7013
|
}
|
|
7014
7014
|
|
|
7015
7015
|
// ────────────────────────────────────────────────
|
|
7016
|
-
// Case 2: Fuzzy match using key_words array
|
|
7016
|
+
// Case 2: Fuzzy match using key_words array
|
|
7017
7017
|
// ────────────────────────────────────────────────
|
|
7018
|
-
// We take up to 3 most meaningful words (you can adjust logic)
|
|
7019
7018
|
const searchWords = words.slice(0, 3);
|
|
7020
|
-
|
|
7021
|
-
if (searchWords.length === 0) {
|
|
7022
|
-
return null;
|
|
7023
|
-
}
|
|
7019
|
+
if (searchWords.length === 0) return null;
|
|
7024
7020
|
|
|
7025
7021
|
const fuzzyQuery = {
|
|
7026
7022
|
selector: {
|
|
7027
7023
|
docType,
|
|
7028
|
-
|
|
7029
|
-
|
|
7030
|
-
},
|
|
7024
|
+
// Ensure 'ts' is in the selector if you plan to sort by it
|
|
7025
|
+
ts: { $gt: 0 },
|
|
7026
|
+
key_words: { $all: searchWords },
|
|
7031
7027
|
},
|
|
7032
|
-
sort: [{ ts: 'desc' }],
|
|
7028
|
+
sort: [{ ts: 'desc' }],
|
|
7033
7029
|
limit: 1,
|
|
7034
7030
|
};
|
|
7035
7031
|
|
|
7036
7032
|
try {
|
|
7037
7033
|
const fuzzyRet = await db_module.find_couch_query('xuda_cache', fuzzyQuery);
|
|
7038
7034
|
|
|
7039
|
-
|
|
7035
|
+
// Defensive check against null/undefined response
|
|
7036
|
+
if (fuzzyRet?.docs?.length > 0) {
|
|
7040
7037
|
const doc = fuzzyRet.docs[0];
|
|
7041
|
-
account_msa.save_cache_hit(doc._id);
|
|
7038
|
+
if (doc._id) account_msa.save_cache_hit(doc._id);
|
|
7042
7039
|
return doc.file ?? null;
|
|
7043
7040
|
}
|
|
7044
7041
|
} catch (err) {
|
|
7045
|
-
|
|
7046
|
-
|
|
7042
|
+
// This catches the 'no_usable_index' error without crashing the app
|
|
7043
|
+
console.warn(`Fuzzy lookup failed for [${searchWords}]:`, err.message);
|
|
7047
7044
|
}
|
|
7048
7045
|
|
|
7049
|
-
// ────────────────────────────────────────────────
|
|
7050
|
-
// Optional fallback strategies (uncomment if desired)
|
|
7051
|
-
// ────────────────────────────────────────────────
|
|
7052
|
-
// 1. Try with 2 words if 3-word search failed
|
|
7053
|
-
/*
|
|
7054
|
-
if (searchWords.length === 3) {
|
|
7055
|
-
const twoWordsQuery = {
|
|
7056
|
-
selector: {
|
|
7057
|
-
docType,
|
|
7058
|
-
key_words: { $all: searchWords.slice(0, 2) }
|
|
7059
|
-
},
|
|
7060
|
-
sort: [{ ts: "desc" }],
|
|
7061
|
-
limit: 1
|
|
7062
|
-
};
|
|
7063
|
-
const ret2 = await db_module.find_couch_query('xuda_cache', twoWordsQuery);
|
|
7064
|
-
if (ret2.docs.length > 0) {
|
|
7065
|
-
account_msa.save_cache_hit(ret2.docs[0]._id);
|
|
7066
|
-
return ret2.docs[0].file ?? null;
|
|
7067
|
-
}
|
|
7068
|
-
}
|
|
7069
|
-
*/
|
|
7070
|
-
|
|
7071
|
-
// 2. Last resort: exact match on original key (already done above, but can retry variations)
|
|
7072
|
-
|
|
7073
7046
|
return null;
|
|
7074
7047
|
};
|
|
7075
7048
|
|