@xuda.io/ai_module 1.1.5418 → 1.1.5420

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 +134 -33
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -4875,7 +4875,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4875
4875
  };
4876
4876
 
4877
4877
  // await update_job('submitting chat');
4878
- emitToDashboard('stream_phase', 'Submitting chat');
4878
+ emitToDashboard('stream_phase', 'Submitting chat', { update: true });
4879
4879
  init_agent_hooks(_agent);
4880
4880
  // const output = await runner.run(_agent, prompt, opt);
4881
4881
  const output = await run_agent(_agent, prompt, opt);
@@ -6919,55 +6919,156 @@ 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;
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
+ // }
6940
+
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 = [];
6944
+
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
+ // }
6952
+
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
+ // };
6963
+
6964
+ // const ret = await db_module.find_couch_query('xuda_cache', query);
6965
+
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
+ // };
6974
+
6922
6975
  const get_chat_picture_cache = async function (uid, docType, key) {
6923
- if (!key || typeof key !== 'string') return null;
6976
+ if (!key || typeof key !== 'string') {
6977
+ return null;
6978
+ }
6924
6979
 
6925
- const words = key
6926
- .trim()
6927
- .toLowerCase()
6928
- .split(/\s+/)
6929
- .filter((w) => w.length > 1); // ignore very short words
6980
+ const normalizedKey = key.trim().toLowerCase();
6981
+ if (!normalizedKey) {
6982
+ return null;
6983
+ }
6930
6984
 
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() },
6985
+ const words = normalizedKey
6986
+ .split(/\s+/)
6987
+ .filter((w) => w.length > 1) // ignore very short words
6988
+ .filter((w, i, arr) => arr.indexOf(w) === i); // remove duplicates
6989
+
6990
+ // ────────────────────────────────────────────────
6991
+ // Case 1: Very short input → try exact match first
6992
+ // ────────────────────────────────────────────────
6993
+ if (words.length <= 2) {
6994
+ const exactQuery = {
6995
+ selector: {
6996
+ docType,
6997
+ key: normalizedKey,
6998
+ },
6935
6999
  limit: 1,
6936
- });
7000
+ };
7001
+
7002
+ const exactRet = await db_module.find_couch_query('xuda_cache', exactQuery);
6937
7003
 
6938
- return ret.docs[0]?.file ?? null;
7004
+ if (exactRet.docs.length > 0) {
7005
+ account_msa.save_cache_hit(exactRet.docs[0]._id);
7006
+ return exactRet.docs[0].file ?? null;
7007
+ }
7008
+
7009
+ // If no exact match and very few words → give up early
7010
+ if (words.length === 0) {
7011
+ return null;
7012
+ }
6939
7013
  }
6940
7014
 
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 = [];
7015
+ // ────────────────────────────────────────────────
7016
+ // Case 2: Fuzzy match using key_words array + $all
7017
+ // ────────────────────────────────────────────────
7018
+ // We take up to 3 most meaningful words (you can adjust logic)
7019
+ const searchWords = words.slice(0, 3);
6944
7020
 
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
- }
7021
+ if (searchWords.length === 0) {
7022
+ return null;
7023
+ }
7024
+
7025
+ const fuzzyQuery = {
7026
+ selector: {
7027
+ docType,
7028
+ key_words: {
7029
+ $all: searchWords,
7030
+ },
7031
+ },
7032
+ sort: [{ ts: 'desc' }], // newest first
7033
+ limit: 1,
7034
+ };
7035
+
7036
+ try {
7037
+ const fuzzyRet = await db_module.find_couch_query('xuda_cache', fuzzyQuery);
7038
+
7039
+ if (fuzzyRet.docs.length > 0) {
7040
+ const doc = fuzzyRet.docs[0];
7041
+ account_msa.save_cache_hit(doc._id);
7042
+ return doc.file ?? null;
6950
7043
  }
7044
+ } catch (err) {
7045
+ console.warn('Fuzzy thumbnail lookup failed:', err.message);
7046
+ // continue → maybe fall back to something else if needed
6951
7047
  }
6952
7048
 
6953
- for (const threeWords of combinations) {
6954
- const query = {
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 = {
6955
7056
  selector: {
6956
7057
  docType,
6957
- $and: threeWords.map((word) => ({
6958
- key: { $regex: `(?i)\\b${word}\\b` },
6959
- })),
7058
+ key_words: { $all: searchWords.slice(0, 2) }
6960
7059
  },
6961
- limit: 1,
7060
+ sort: [{ ts: "desc" }],
7061
+ limit: 1
6962
7062
  };
6963
-
6964
- const ret = await db_module.find_couch_query('xuda_cache', query);
6965
-
6966
- if (ret.docs.length > 0) {
6967
- account_msa.save_cache_hit(ret.docs[0]._id);
6968
- return ret.docs[0].file;
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;
6969
7067
  }
6970
7068
  }
7069
+ */
7070
+
7071
+ // 2. Last resort: exact match on original key (already done above, but can retry variations)
6971
7072
 
6972
7073
  return null;
6973
7074
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5418",
3
+ "version": "1.1.5420",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",