@xuda.io/ai_module 1.1.4961 → 1.1.4963

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 +129 -128
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -63,6 +63,7 @@ const { get_xuda_couch } = _common;
63
63
 
64
64
  const db_module = await import(`${module_path}/db_module/index.mjs`);
65
65
 
66
+ const db_ms = await import(`${module_path}/db_module/index_ms.mjs`);
66
67
  const account_ms = await import(`${module_path}/account_module/index_ms.mjs`);
67
68
  const drive_ms = await import(`${module_path}/drive_module/index_ms.mjs`);
68
69
  const jobs_ms = await import(`${module_path}/jobs_module/index_ms.mjs`);
@@ -149,7 +150,7 @@ const get_studio_table_info_tool = tool({
149
150
  try {
150
151
  const project_db = await get_project_db(app_id);
151
152
  // let tables = await project_db.view('xuda', 'studio_docs_by_type', { key: 'table' });
152
- let tables = await db_module.get_app_couch_view(app_id, 'studio_docs_by_type', { key: 'table' });
153
+ let tables = await db_ms.get_app_couch_view(app_id, 'studio_docs_by_type', { key: 'table' });
153
154
  let response = '';
154
155
 
155
156
  for (const doc of tables.rows) {
@@ -1085,19 +1086,19 @@ export const get_chat_conversation = async function (req) {
1085
1086
  const account_profile_info = await get_active_account_profile_info(uid);
1086
1087
 
1087
1088
  if (_id) {
1088
- let data = await db_module.get_app_couch_doc(account_profile_info.app_id, _id);
1089
+ let data = await db_ms.get_app_couch_doc(account_profile_info.app_id, _id);
1089
1090
  return data;
1090
1091
  }
1091
1092
 
1092
1093
  try {
1093
- let conversation_doc = await db_module.get_app_couch_doc(account_profile_info.app_id, conversation_id);
1094
+ let conversation_doc = await db_ms.get_app_couch_doc(account_profile_info.app_id, conversation_id);
1094
1095
  let reference_doc;
1095
1096
  if (conversation_doc.shared_from_uid) {
1096
1097
  reference_doc = _.cloneDeep(conversation_doc);
1097
- conversation_doc = await db_module.get_app_couch_doc_native(reference_doc.shared_from_app_id, reference_doc.share_item_id);
1098
+ conversation_doc = await db_ms.get_app_couch_doc_native(reference_doc.shared_from_app_id, reference_doc.share_item_id);
1098
1099
  conversation_doc.reference_doc = reference_doc;
1099
1100
  }
1100
- let conversation_items = await db_module.find_app_couch_query(account_profile_info.app_id, {
1101
+ let conversation_items = await db_ms.find_app_couch_query(account_profile_info.app_id, {
1101
1102
  selector: {
1102
1103
  docType: 'chat_conversation_item',
1103
1104
  conversation_id,
@@ -1113,7 +1114,7 @@ export const get_chat_conversation = async function (req) {
1113
1114
  conversation_item_doc.read = {};
1114
1115
  }
1115
1116
  conversation_item_doc.read[uid] = Date.now();
1116
- await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_item_doc);
1117
+ await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_item_doc);
1117
1118
  }
1118
1119
 
1119
1120
  return { code: 15, data: { conversation: conversation_items, ...conversation_doc } };
@@ -1162,7 +1163,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1162
1163
 
1163
1164
  try {
1164
1165
  if (_id) {
1165
- let data = await db_module.get_app_couch_doc(account_profile_info.app_id, _id);
1166
+ let data = await db_ms.get_app_couch_doc(account_profile_info.app_id, _id);
1166
1167
  return data;
1167
1168
  }
1168
1169
 
@@ -1240,7 +1241,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1240
1241
  }
1241
1242
 
1242
1243
  const chat_conversation_count = async function (contact_id, conversation_id) {
1243
- const counts_ret = await db_module.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1244
+ const counts_ret = await db_ms.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1244
1245
  reduce: true,
1245
1246
  group_level: 1,
1246
1247
 
@@ -1253,7 +1254,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1253
1254
  return counts_ret?.rows?.[0]?.value || 0;
1254
1255
  };
1255
1256
  const chat_conversation_read = async function (contact_id, conversation_id) {
1256
- const counts_ret = await db_module.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_read', {
1257
+ const counts_ret = await db_ms.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_read', {
1257
1258
  reduce: true,
1258
1259
  group_level: 2,
1259
1260
  key: [uid, contact_id, conversation_id],
@@ -1271,7 +1272,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1271
1272
  if (!reference_id) {
1272
1273
  if (filter_type === 'pending') {
1273
1274
  const from_opt = { selector: { docType: 'team_request', access_type: 'chat', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid', 'share_item_id'] };
1274
- const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1275
+ const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
1275
1276
 
1276
1277
  for await (let e of requests_from_res.docs) {
1277
1278
  let doc = { date_created: e.team_req_date, pending: true, uid: e.team_req_from_uid, team_req_id: e._id };
@@ -1282,7 +1283,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1282
1283
 
1283
1284
  //share_item_id
1284
1285
  const app_id = await get_account_default_project_id(e.team_req_from_uid);
1285
- const chat_ret = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
1286
+ const chat_ret = await db_ms.get_app_couch_doc_native(app_id, e.share_item_id);
1286
1287
  doc.title = chat_ret.title;
1287
1288
  doc.chat_image = chat_ret.chat_image;
1288
1289
 
@@ -1295,7 +1296,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1295
1296
  let ai_chats = { docs: [], total_docs: 0 };
1296
1297
 
1297
1298
  if (filter_type !== 'pending') {
1298
- ai_chats = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
1299
+ ai_chats = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
1299
1300
  if (!limit || conversation_id) {
1300
1301
  ai_chats.total_docs = ai_chats.docs.length;
1301
1302
  } else {
@@ -1304,7 +1305,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1304
1305
  opt.limit = 9999;
1305
1306
  opt.fields = ['_id'];
1306
1307
 
1307
- const counter = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
1308
+ const counter = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
1308
1309
  ai_chats.total_docs = counter.docs.length;
1309
1310
  }
1310
1311
  }
@@ -1316,7 +1317,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
1316
1317
 
1317
1318
  if (doc.shared_from_uid) {
1318
1319
  reference_doc = _.cloneDeep(doc);
1319
- doc = await db_module.get_app_couch_doc_native(reference_doc.shared_from_app_id, reference_doc.share_item_id);
1320
+ doc = await db_ms.get_app_couch_doc_native(reference_doc.shared_from_app_id, reference_doc.share_item_id);
1320
1321
  doc.reference_doc = reference_doc;
1321
1322
  }
1322
1323
 
@@ -1362,7 +1363,7 @@ export const delete_ai_chat = async function (req) {
1362
1363
  const account_profile_info = await get_active_account_profile_info(uid);
1363
1364
 
1364
1365
  const app_id = await get_account_default_project_id(uid);
1365
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1366
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1366
1367
 
1367
1368
  let save_ret;
1368
1369
  try {
@@ -1377,7 +1378,7 @@ export const delete_ai_chat = async function (req) {
1377
1378
  } else {
1378
1379
  conversation_doc.stat = 4;
1379
1380
  conversation_doc.ts = Date.now();
1380
- save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1381
+ save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1381
1382
 
1382
1383
  client.conversations.delete(conversation_id);
1383
1384
 
@@ -1407,7 +1408,7 @@ export const archive_ai_chat = async function (req) {
1407
1408
  try {
1408
1409
  await team_ms.validate_share_exist(uid, 'chat', conversation_id);
1409
1410
 
1410
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1411
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1411
1412
 
1412
1413
  if (conversation_doc.uid !== uid && conversation_doc?.account_profile_info?.uid !== uid) {
1413
1414
  throw new Error('Operation not allowed');
@@ -1415,7 +1416,7 @@ export const archive_ai_chat = async function (req) {
1415
1416
 
1416
1417
  conversation_doc.stat = 5;
1417
1418
  conversation_doc.ts = Date.now();
1418
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1419
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1419
1420
 
1420
1421
  return save_ret;
1421
1422
  } catch (err) {
@@ -1428,7 +1429,7 @@ export const unarchive_ai_chat = async function (req) {
1428
1429
  const account_profile_info = await get_active_account_profile_info(uid);
1429
1430
 
1430
1431
  try {
1431
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1432
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
1432
1433
 
1433
1434
  if (conversation_doc.stat !== 5) {
1434
1435
  throw new Error('chat is not archived');
@@ -1436,7 +1437,7 @@ export const unarchive_ai_chat = async function (req) {
1436
1437
 
1437
1438
  conversation_doc.stat = 3;
1438
1439
  conversation_doc.ts = Date.now();
1439
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1440
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1440
1441
 
1441
1442
  return save_ret;
1442
1443
  } catch (err) {
@@ -1526,7 +1527,7 @@ const get_studio_doc = async function (req) {
1526
1527
 
1527
1528
  const app_id = await get_account_default_project_id(uid);
1528
1529
  try {
1529
- let data = await db_module.get_app_couch_doc_native(account_profile_info.app_id, _id);
1530
+ let data = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, _id);
1530
1531
 
1531
1532
  if (data?.studio_meta?.createdByUid !== uid) delete data.agentConfig; //throw new Error('doctype not supported');
1532
1533
  return { code: 7, data };
@@ -1797,7 +1798,7 @@ export const get_ai_agent_info = async function (uid, job_id, headers, doc, from
1797
1798
  };
1798
1799
 
1799
1800
  const agent_conversation_count = async function (agent_id) {
1800
- const counts_ret = await db_module.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1801
+ const counts_ret = await db_ms.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1801
1802
  reduce: true,
1802
1803
  group_level: 1,
1803
1804
 
@@ -1851,7 +1852,7 @@ const get_user_ai_agents = async function (uid) {
1851
1852
  'properties.menuType': 'ai_agent',
1852
1853
  },
1853
1854
  };
1854
- return await db_module.find_app_couch_query(account_profile_info.app_id, opt);
1855
+ return await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
1855
1856
  };
1856
1857
 
1857
1858
  export const get_ai_agents = async function (req, job_id, headers) {
@@ -1942,7 +1943,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
1942
1943
 
1943
1944
  let user_agents = { docs: [], total_docs: 0 };
1944
1945
  if (filter_type !== 'pending') {
1945
- user_agents = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
1946
+ user_agents = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
1946
1947
  if (!limit || agent_id) {
1947
1948
  user_agents.total_docs = user_agents.docs.length;
1948
1949
  } else {
@@ -1951,7 +1952,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
1951
1952
  opt.limit = 9999;
1952
1953
  opt.fields = ['_id'];
1953
1954
 
1954
- const counter = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
1955
+ const counter = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
1955
1956
  user_agents.total_docs = counter.docs.length;
1956
1957
  }
1957
1958
  }
@@ -1960,7 +1961,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
1960
1961
  let requests_from = { docs: [], total_docs: 0 };
1961
1962
  if (filter_type === 'pending') {
1962
1963
  const from_opt = { selector: { docType: 'team_request', access_type: 'ai_agent', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid', 'share_item_id'] };
1963
- const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1964
+ const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
1964
1965
 
1965
1966
  for await (let e of requests_from_res.docs) {
1966
1967
  let doc = { ts: e.team_req_date, pending: true, properties: {}, studio_meta: { createdByUid: e.team_req_from_uid }, team_req_id: e._id };
@@ -1972,7 +1973,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
1972
1973
 
1973
1974
  //share_item_id
1974
1975
  const app_id = await get_account_default_project_id(e.team_req_from_uid);
1975
- const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
1976
+ const ai_agent_doc = await db_ms.get_app_couch_doc_native(app_id, e.share_item_id);
1976
1977
  doc.properties.menuName = ai_agent_doc.properties.menuName;
1977
1978
  doc.studio_meta.agent_image = ai_agent_doc.studio_meta.agent_image;
1978
1979
 
@@ -1990,7 +1991,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
1990
1991
  if (doc.studio_meta.shared_from_uid && doc.studio_meta.shared_from_app_id) {
1991
1992
  reference_doc = _.cloneDeep(doc);
1992
1993
  try {
1993
- doc = await db_module.get_app_couch_doc_native(reference_doc.studio_meta.shared_from_app_id, reference_doc.studio_meta.share_item_id);
1994
+ doc = await db_ms.get_app_couch_doc_native(reference_doc.studio_meta.shared_from_app_id, reference_doc.studio_meta.share_item_id);
1994
1995
  } catch (error) {
1995
1996
  continue;
1996
1997
  }
@@ -2001,7 +2002,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
2001
2002
  // purchased
2002
2003
  if (doc.studio_meta.installed_from_uid && doc.studio_meta.installed_from_app_id) {
2003
2004
  reference_doc = _.cloneDeep(doc);
2004
- doc = await db_module.get_app_couch_doc_native(reference_doc.studio_meta.installed_from_app_id, doc._id);
2005
+ doc = await db_ms.get_app_couch_doc_native(reference_doc.studio_meta.installed_from_app_id, doc._id);
2005
2006
  doc.reference_doc = reference_doc;
2006
2007
  doc.stat = reference_doc.stat;
2007
2008
  }
@@ -2026,7 +2027,7 @@ export const delete_ai_agent = async function (req) {
2026
2027
  // const project_db = await get_account_project_db(uid);
2027
2028
 
2028
2029
  try {
2029
- let prog_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
2030
+ let prog_doc = await db_ms.get_app_couch_doc_native(app_id, agent_id);
2030
2031
 
2031
2032
  if (prog_doc.stat !== 5) {
2032
2033
  throw new Error('A document can only be deleted after it has been archived');
@@ -2038,10 +2039,10 @@ export const delete_ai_agent = async function (req) {
2038
2039
 
2039
2040
  save_ret = await team_ms.remove_team_member({ team_req_id: prog_doc.studio_meta.team_req_id });
2040
2041
  } else {
2041
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2042
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2042
2043
  agent_doc.stat = 4;
2043
2044
  agent_doc.ts = Date.now();
2044
- const save_ret = await db_module.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2045
+ const save_ret = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2045
2046
 
2046
2047
  for (const [key, val] of Object.entries(agent_doc?.agentConfig?.agent_tools || [])) {
2047
2048
  const file_id = val?.youtube?.file_id || val?.file?.file_id;
@@ -2066,7 +2067,7 @@ export const uninstall_ai_agent = async function (req) {
2066
2067
  const account_profile_info = await get_active_account_profile_info(uid);
2067
2068
 
2068
2069
  try {
2069
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2070
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2070
2071
 
2071
2072
  if (!agent_doc.studio_meta.installed_from_app_id) {
2072
2073
  throw new Error('agent not installed');
@@ -2075,7 +2076,7 @@ export const uninstall_ai_agent = async function (req) {
2075
2076
  agent_doc.stat = 4;
2076
2077
  agent_doc.ts = Date.now();
2077
2078
 
2078
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2079
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2079
2080
 
2080
2081
  return save_ret;
2081
2082
  } catch (err) {
@@ -2088,7 +2089,7 @@ export const unarchive_ai_agent = async function (req) {
2088
2089
  const account_profile_info = await get_active_account_profile_info(uid);
2089
2090
 
2090
2091
  try {
2091
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2092
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2092
2093
 
2093
2094
  if (agent_doc.stat !== 5) {
2094
2095
  throw new Error('agent is not archived');
@@ -2097,7 +2098,7 @@ export const unarchive_ai_agent = async function (req) {
2097
2098
  agent_doc.stat = 3;
2098
2099
  agent_doc.ts = Date.now();
2099
2100
 
2100
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2101
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2101
2102
 
2102
2103
  return save_ret;
2103
2104
  } catch (err) {
@@ -2111,7 +2112,7 @@ export const archive_ai_agent = async function (req) {
2111
2112
  try {
2112
2113
  await team_ms.validate_share_exist(uid, 'ai_agent', agent_id);
2113
2114
 
2114
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2115
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2115
2116
 
2116
2117
  if (agent_doc.studio_meta.createdByUid !== uid && agent_doc?.studio_meta?.account_profile_info?.uid !== uid) {
2117
2118
  throw new Error('Operation not allowed');
@@ -2119,7 +2120,7 @@ export const archive_ai_agent = async function (req) {
2119
2120
 
2120
2121
  agent_doc.stat = 5;
2121
2122
  agent_doc.ts = Date.now();
2122
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2123
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2123
2124
 
2124
2125
  return save_ret;
2125
2126
  } catch (err) {
@@ -2135,7 +2136,7 @@ export const archive_mini_app = async function (req) {
2135
2136
  try {
2136
2137
  await team_ms.validate_share_exist(uid, 'miniapp', prog_id);
2137
2138
 
2138
- let prog_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2139
+ let prog_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2139
2140
 
2140
2141
  if (agent_doc.studio_meta.createdByUid !== uid && agent_doc?.studio_meta?.account_profile_info?.uid !== uid) {
2141
2142
  throw new Error('Operation not allowed');
@@ -2143,7 +2144,7 @@ export const archive_mini_app = async function (req) {
2143
2144
 
2144
2145
  prog_doc.stat = 5;
2145
2146
  prog_doc.ts = Date.now();
2146
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2147
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2147
2148
 
2148
2149
  return save_ret;
2149
2150
  } catch (err) {
@@ -2157,7 +2158,7 @@ export const unarchive_mini_app = async function (req) {
2157
2158
  const account_profile_info = await get_active_account_profile_info(uid);
2158
2159
 
2159
2160
  try {
2160
- let prog_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2161
+ let prog_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2161
2162
 
2162
2163
  if (prog_doc.stat !== 5) {
2163
2164
  throw new Error('mini app is not archived');
@@ -2165,7 +2166,7 @@ export const unarchive_mini_app = async function (req) {
2165
2166
 
2166
2167
  prog_doc.stat = 3;
2167
2168
  prog_doc.ts = Date.now();
2168
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2169
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2169
2170
 
2170
2171
  return save_ret;
2171
2172
  } catch (err) {
@@ -2179,7 +2180,7 @@ export const delete_mini_app = async function (req) {
2179
2180
  const account_profile_info = await get_active_account_profile_info(uid);
2180
2181
 
2181
2182
  try {
2182
- let prog_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2183
+ let prog_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
2183
2184
 
2184
2185
  if (prog_doc.stat !== 5) {
2185
2186
  throw new Error('A document can only be deleted after it has been archived');
@@ -2193,7 +2194,7 @@ export const delete_mini_app = async function (req) {
2193
2194
  } else {
2194
2195
  prog_doc.stat = 4;
2195
2196
  prog_doc.ts = Date.now();
2196
- save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2197
+ save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, prog_doc);
2197
2198
  delete_depended_chats(uid, prog_id);
2198
2199
  }
2199
2200
  return save_ret;
@@ -2208,7 +2209,7 @@ export const update_ai_agent = async function (req, job_id, headers) {
2208
2209
  const account_profile_info = await get_active_account_profile_info(uid);
2209
2210
 
2210
2211
  try {
2211
- let agent_doc = await await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2212
+ let agent_doc = await await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2212
2213
 
2213
2214
  agent_doc.ts = Date.now();
2214
2215
  agent_doc.agentConfig = agentConfig;
@@ -2228,7 +2229,7 @@ export const update_ai_agent = async function (req, job_id, headers) {
2228
2229
  agent_doc.studio_meta.agent_tags = agentConfig.agent_tags;
2229
2230
  agent_doc.studio_meta.agent_user_guide = agentConfig.agent_user_guide;
2230
2231
 
2231
- const data = await db_module.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2232
+ const data = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2232
2233
 
2233
2234
  setTimeout(async () => {
2234
2235
  await update_ai_agent_properties(agent_doc, account_profile_info.app_id, uid, { name_changed, agent_user_guide_changed, agent_category_changed, agent_subcategory_changed, agent_instructions_changed });
@@ -2316,7 +2317,7 @@ export const get_apps = async function (req, job_id, headers) {
2316
2317
 
2317
2318
  let user_apps = { docs: [], total_docs: 0 };
2318
2319
  if (filter_type !== 'pending') {
2319
- user_apps = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
2320
+ user_apps = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
2320
2321
  if (!limit || mini_app_id) {
2321
2322
  user_apps.total_docs = user_apps.docs.length;
2322
2323
  } else {
@@ -2325,7 +2326,7 @@ export const get_apps = async function (req, job_id, headers) {
2325
2326
  opt.limit = 9999;
2326
2327
  opt.fields = ['_id'];
2327
2328
 
2328
- const counter = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
2329
+ const counter = await db_ms.find_app_couch_query(account_profile_info.app_id, opt);
2329
2330
  user_apps.total_docs = counter.docs.length;
2330
2331
  }
2331
2332
  }
@@ -2337,7 +2338,7 @@ export const get_apps = async function (req, job_id, headers) {
2337
2338
  if (!mini_app_id) {
2338
2339
  if (filter_type === 'pending') {
2339
2340
  const from_opt = { selector: { docType: 'team_request', access_type: 'miniapp', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid', 'share_item_id'] };
2340
- const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
2341
+ const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
2341
2342
 
2342
2343
  for await (let e of requests_from_res.docs) {
2343
2344
  //account_info
@@ -2349,7 +2350,7 @@ export const get_apps = async function (req, job_id, headers) {
2349
2350
 
2350
2351
  //share_item_id
2351
2352
  const app_id = await get_account_default_project_id(e.team_req_from_uid);
2352
- const miniapp_ret = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
2353
+ const miniapp_ret = await db_ms.get_app_couch_doc_native(app_id, e.share_item_id);
2353
2354
  doc.properties.menuName = miniapp_ret.properties.menuName;
2354
2355
  doc.studio_meta.agent_image = miniapp_ret.studio_meta.agent_image;
2355
2356
 
@@ -2365,7 +2366,7 @@ export const get_apps = async function (req, job_id, headers) {
2365
2366
  let reference_doc;
2366
2367
  if (doc.studio_meta.shared_from_uid) {
2367
2368
  reference_doc = _.cloneDeep(doc);
2368
- doc = await db_module.get_app_couch_doc_native(reference_doc.studio_meta.shared_from_app_id, reference_doc.studio_meta.share_item_id);
2369
+ doc = await db_ms.get_app_couch_doc_native(reference_doc.studio_meta.shared_from_app_id, reference_doc.studio_meta.share_item_id);
2369
2370
  doc.reference_doc = reference_doc;
2370
2371
  }
2371
2372
 
@@ -2575,11 +2576,11 @@ const save_agent_status = async function (uid, agent_id, stat, agentConfig) {
2575
2576
  const account_profile_info = await get_active_account_profile_info(uid);
2576
2577
 
2577
2578
  try {
2578
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2579
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
2579
2580
  agent_doc.ts = Date.now();
2580
2581
  agent_doc.stat = stat;
2581
2582
  agent_doc.agentConfig = agentConfig;
2582
- const data = await db_module.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2583
+ const data = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2583
2584
 
2584
2585
  return { code: 12, data };
2585
2586
  } catch (err) {
@@ -2609,7 +2610,7 @@ export const create_ai_agent = async function (req, job_id, headers) {
2609
2610
 
2610
2611
  // const data = await db.insert(agent_doc);
2611
2612
 
2612
- const data = await db_module.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2613
+ const data = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
2613
2614
  setTimeout(async () => {
2614
2615
  await update_ai_agent_properties(agent_doc, account_profile_info.app_id, uid);
2615
2616
  await update_thumbnail('ai_agent', agent_doc, account_profile_info.app_id, uid, job_id, headers, null, null, account_profile_info);
@@ -2624,7 +2625,7 @@ export const create_ai_agent = async function (req, job_id, headers) {
2624
2625
 
2625
2626
  export const update_ai_agent_properties = async function (doc, app_id, uid, fields_changed = {}) {
2626
2627
  debugger;
2627
- const db_doc = await db_module.get_app_couch_doc_native(app_id, doc._id);
2628
+ const db_doc = await db_ms.get_app_couch_doc_native(app_id, doc._id);
2628
2629
  // const { account_profile_info } = doc.studio_meta;
2629
2630
  const account_profile_info = await get_active_account_profile_info(uid);
2630
2631
 
@@ -2836,7 +2837,7 @@ export const update_ai_agent_properties = async function (doc, app_id, uid, fiel
2836
2837
  db_doc.studio_meta.agent_user_guide_steps = db_doc.agentConfig.agent_user_guide_steps;
2837
2838
  }
2838
2839
 
2839
- const save_ret = await db_module.save_app_couch_doc_native(app_id, db_doc);
2840
+ const save_ret = await db_ms.save_app_couch_doc_native(app_id, db_doc);
2840
2841
  return save_ret;
2841
2842
  };
2842
2843
 
@@ -2847,9 +2848,9 @@ export const update_thumbnail = async function (type, doc, app_id, uid, job_id,
2847
2848
  try {
2848
2849
  const get_db_doc = async function () {
2849
2850
  if (xuda_db) {
2850
- return await db_module.get_couch_doc_native(xuda_db, doc._id);
2851
+ return await db_ms.get_couch_doc_native(xuda_db, doc._id);
2851
2852
  } else {
2852
- return await db_module.get_app_couch_doc_native(account_profile_info.app_id, doc._id);
2853
+ return await db_ms.get_app_couch_doc_native(account_profile_info.app_id, doc._id);
2853
2854
  }
2854
2855
  };
2855
2856
 
@@ -2952,9 +2953,9 @@ export const update_thumbnail = async function (type, doc, app_id, uid, job_id,
2952
2953
  let save_ret;
2953
2954
 
2954
2955
  if (xuda_db) {
2955
- save_ret = await db_module.save_couch_doc_native(xuda_db, db_doc);
2956
+ save_ret = await db_ms.save_couch_doc_native(xuda_db, db_doc);
2956
2957
  } else {
2957
- save_ret = await db_module.save_app_couch_doc_native(account_profile_info.app_id, db_doc);
2958
+ save_ret = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, db_doc);
2958
2959
  }
2959
2960
  return save_ret;
2960
2961
  } catch (error) {
@@ -3417,7 +3418,7 @@ export const create_conversation = async function (req, job_id, headers) {
3417
3418
  from_mailbox,
3418
3419
  email_recipient_type,
3419
3420
  };
3420
- const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
3421
+ const save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
3421
3422
 
3422
3423
  let contact_id, contact_doc, recipient_uid, recipient_contact_id;
3423
3424
 
@@ -3438,7 +3439,7 @@ export const create_conversation = async function (req, job_id, headers) {
3438
3439
  recipient_uid = contact_doc.connection_uid;
3439
3440
 
3440
3441
  const app_id = await get_account_default_project_id(recipient_uid);
3441
- const save_ret = await db_module.save_app_couch_doc(app_id, { ...conversation_doc, ...{ uid: recipient_uid, reference_id: recipient_contact_id } });
3442
+ const save_ret = await db_ms.save_app_couch_doc(app_id, { ...conversation_doc, ...{ uid: recipient_uid, reference_id: recipient_contact_id } });
3442
3443
  }
3443
3444
 
3444
3445
  setTimeout(async () => {
@@ -3452,15 +3453,15 @@ export const create_conversation = async function (req, job_id, headers) {
3452
3453
  // write conversation to host after thumbnail update
3453
3454
  // get sender conversation
3454
3455
 
3455
- const conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3456
+ const conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3456
3457
 
3457
3458
  const recipient_account_profile_info = await get_active_account_profile_info(recipient_uid);
3458
- let recipient_conversation_doc = await db_module.get_app_couch_doc_native(recipient_account_profile_info.app_id, conversation_doc._id);
3459
+ let recipient_conversation_doc = await db_ms.get_app_couch_doc_native(recipient_account_profile_info.app_id, conversation_doc._id);
3459
3460
  recipient_conversation_doc.title = conversation_doc.title;
3460
3461
 
3461
3462
  recipient_conversation_doc.thumbnail_request_ts = conversation_doc.thumbnail_request_ts;
3462
3463
  recipient_conversation_doc.chat_image = conversation_doc.chat_image;
3463
- await db_module.save_app_couch_doc_native(recipient_account_profile_info.app_id, recipient_conversation_doc);
3464
+ await db_ms.save_app_couch_doc_native(recipient_account_profile_info.app_id, recipient_conversation_doc);
3464
3465
 
3465
3466
  await update_conversation_mood_level(
3466
3467
  uid,
@@ -3493,7 +3494,7 @@ export const create_conversation = async function (req, job_id, headers) {
3493
3494
  };
3494
3495
 
3495
3496
  const process_conversation = async function (uid, conversation_id, account_profile_info, job_id, headers) {
3496
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3497
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3497
3498
  const { conversation_type, prompt, reference_id } = conversation_doc;
3498
3499
 
3499
3500
  // Ask GPT for title
@@ -3501,10 +3502,10 @@ const process_conversation = async function (uid, conversation_id, account_profi
3501
3502
  if (prompt.split(' ').length > 10) {
3502
3503
  const title = await submit_chat_gpt_prompt({ uid, prompt: `create title for the prompt return 5 words result maximum text only without options : ${prompt}`, model: 'gpt-5-nano', metadata: { conversation_id: conversation_doc._id, func: 'create_conversation' }, account_profile_info });
3503
3504
 
3504
- conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3505
+ conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3505
3506
  conversation_doc.title = title.data;
3506
3507
 
3507
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3508
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3508
3509
  }
3509
3510
  }
3510
3511
  /// categorize prompt
@@ -3537,10 +3538,10 @@ const process_conversation = async function (uid, conversation_id, account_profi
3537
3538
  }
3538
3539
  }
3539
3540
 
3540
- conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3541
+ conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
3541
3542
  conversation_doc.category_info = category_info;
3542
3543
  conversation_doc.process_stat = 'full';
3543
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3544
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3544
3545
 
3545
3546
  if (category_info.category) {
3546
3547
  await update_thumbnail('conversation_category', conversation_doc, account_profile_info.app_id, uid, job_id, headers, null, null, account_profile_info);
@@ -3581,7 +3582,7 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
3581
3582
  if (!conversation_id) throw new Error('missing conversation_id');
3582
3583
  const account_profile_info = await get_active_account_profile_info(uid, profile_id);
3583
3584
 
3584
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3585
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3585
3586
  req.conversation_doc = conversation_doc;
3586
3587
  let ret;
3587
3588
  if (conversation_doc.reference_type === 'contacts') {
@@ -3646,9 +3647,9 @@ const update_conversation_mood_level = async function (uid, target_contacts = []
3646
3647
 
3647
3648
  for await (const target of target_contacts) {
3648
3649
  const account_profile_info = await get_active_account_profile_info(target.uid);
3649
- let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3650
+ let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3650
3651
  conversation_doc.mood_level = mood_level_obj.mood_level;
3651
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3652
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
3652
3653
 
3653
3654
  if (update_contact) {
3654
3655
  const account_profile_info = await get_active_account_profile_info(target.uid);
@@ -3672,11 +3673,11 @@ const contact_chat_conversation = async function (req, job_id, headers) {
3672
3673
 
3673
3674
  const receiver_contact_doc = await get_contact_info(uid, null, conversation_doc.reference_id);
3674
3675
  const receiver_app_id = await get_account_default_project_id(receiver_contact_doc.contact_uid);
3675
- let receiver_conversation_doc = await db_module.get_app_couch_doc_native(receiver_app_id, conversation_id);
3676
+ let receiver_conversation_doc = await db_ms.get_app_couch_doc_native(receiver_app_id, conversation_id);
3676
3677
 
3677
3678
  const sender_contact_doc = await get_contact_info(uid, null, conversation_doc.reference_id);
3678
3679
  const sender_app_id = account_profile_info.app_id;
3679
- let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
3680
+ let sender_conversation_doc = await db_ms.get_app_couch_doc_native(sender_app_id, conversation_id);
3680
3681
 
3681
3682
  let item;
3682
3683
  try {
@@ -3698,10 +3699,10 @@ const contact_chat_conversation = async function (req, job_id, headers) {
3698
3699
  // const obj = { id: item.data[0].id, ts: Date.now(), uid, read: true, conversation_type: 'chat' };
3699
3700
  // receiver_conversation_doc.messages.push(obj);
3700
3701
 
3701
- await db_module.save_app_couch_doc_native(receiver_app_id, receiver_conversation_doc);
3702
+ await db_ms.save_app_couch_doc_native(receiver_app_id, receiver_conversation_doc);
3702
3703
  // sender_conversation_doc.messages.push({ ...obj, ...{ read: false } });
3703
3704
 
3704
- await db_module.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3705
+ await db_ms.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3705
3706
 
3706
3707
  let out_conversation_item_obj = {
3707
3708
  _id: await _common.xuda_get_uuid('chat_conversation_item'),
@@ -3720,7 +3721,7 @@ const contact_chat_conversation = async function (req, job_id, headers) {
3720
3721
  role: 'user',
3721
3722
  };
3722
3723
 
3723
- const save_ret = await db_module.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3724
+ const save_ret = await db_ms.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3724
3725
 
3725
3726
  update_conversation_mood_level(uid, conversation_id, prompt, uid, receiver_contact_doc.contact_uid, conversation_doc.reference_type === 'contacts', account_profile_info);
3726
3727
 
@@ -3735,7 +3736,7 @@ const chat_note = async function (req, job_id, headers) {
3735
3736
  const conversation_id = conversation_doc._id;
3736
3737
 
3737
3738
  const sender_app_id = account_profile_info.app_id; //await get_account_default_project_id(uid);
3738
- let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
3739
+ let sender_conversation_doc = await db_ms.get_app_couch_doc_native(sender_app_id, conversation_id);
3739
3740
 
3740
3741
  await attachment_handler(uid, sender_app_id, conversation_id, attachments, account_profile_info);
3741
3742
  let item;
@@ -3758,7 +3759,7 @@ const chat_note = async function (req, job_id, headers) {
3758
3759
  // let obj = { id: item.data[0].id, ts: Date.now(), uid, read: true, conversation_type: 'note' };
3759
3760
 
3760
3761
  // sender_conversation_doc.messages.push(obj);
3761
- // await db_module.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3762
+ // await db_ms.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3762
3763
 
3763
3764
  let out_conversation_item_obj = {
3764
3765
  _id: await _common.xuda_get_uuid('chat_conversation_item'),
@@ -3779,9 +3780,9 @@ const chat_note = async function (req, job_id, headers) {
3779
3780
  rtl: _common.detectRTL(body),
3780
3781
  };
3781
3782
 
3782
- const save_ret = await db_module.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3783
+ const save_ret = await db_ms.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3783
3784
 
3784
- // await db_module.save_app_couch_doc_native(sender_app_id, { _id: obj.id, stat: 3, docType: 'chat_conversation_item', ts: obj.ts, uid, conversation_type: obj.conversation_type, date_created_ts: obj.ts, conversation_id, body, reference_id: conversation_doc.reference_id });
3785
+ // await db_ms.save_app_couch_doc_native(sender_app_id, { _id: obj.id, stat: 3, docType: 'chat_conversation_item', ts: obj.ts, uid, conversation_type: obj.conversation_type, date_created_ts: obj.ts, conversation_id, body, reference_id: conversation_doc.reference_id });
3785
3786
 
3786
3787
  return save_ret; //{ code: 15, data: item };
3787
3788
  } catch (err) {
@@ -3801,9 +3802,9 @@ const chat_email = async function (req, job_id, headers) {
3801
3802
  const conversation_id = conversation_doc._id;
3802
3803
  const sender_app_id = account_profile_info.app_id;
3803
3804
 
3804
- let email_account_doc = await db_module.get_app_couch_doc_native(sender_app_id, account_profile_info.account_profile_obj?.email_account_id);
3805
+ let email_account_doc = await db_ms.get_app_couch_doc_native(sender_app_id, account_profile_info.account_profile_obj?.email_account_id);
3805
3806
 
3806
- let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
3807
+ let sender_conversation_doc = await db_ms.get_app_couch_doc_native(sender_app_id, conversation_id);
3807
3808
 
3808
3809
  if (sender_conversation_doc.reference_type !== 'contacts') {
3809
3810
  throw new Error('not an contact conversation');
@@ -3820,7 +3821,7 @@ const chat_email = async function (req, job_id, headers) {
3820
3821
  if (!from_mailbox) {
3821
3822
  // get last email
3822
3823
 
3823
- const chat_conversation_item = await db_module.find_app_couch_query(account_profile_info.app_id, {
3824
+ const chat_conversation_item = await db_ms.find_app_couch_query(account_profile_info.app_id, {
3824
3825
  selector: {
3825
3826
  docType: 'chat_conversation_item',
3826
3827
  conversation_type: 'email',
@@ -3883,7 +3884,7 @@ const chat_email = async function (req, job_id, headers) {
3883
3884
  // let obj = { id: item.data[0].id, ts: Date.now(), uid, read: true, conversation_type: 'email' };
3884
3885
 
3885
3886
  // sender_conversation_doc.messages.push(obj);
3886
- await db_module.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3887
+ await db_ms.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
3887
3888
 
3888
3889
  let out_conversation_item_obj = {
3889
3890
  _id: await _common.xuda_get_uuid('chat_conversation_item'),
@@ -3907,8 +3908,8 @@ const chat_email = async function (req, job_id, headers) {
3907
3908
  process_stat: perform_ai_execution ? 'full' : 'partial',
3908
3909
  };
3909
3910
 
3910
- const save_ret = await db_module.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3911
- // const save_ret = await db_module.save_app_couch_doc_native(sender_app_id, {
3911
+ const save_ret = await db_ms.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
3912
+ // const save_ret = await db_ms.save_app_couch_doc_native(sender_app_id, {
3912
3913
  // _id: obj.id,
3913
3914
  // stat: 3,
3914
3915
  // docType: 'chat_conversation_item',
@@ -3956,7 +3957,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
3956
3957
  direction: 'out',
3957
3958
  role: 'user',
3958
3959
  };
3959
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj);
3960
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj);
3960
3961
 
3961
3962
  // const { account_profile_info } = conversation_doc;
3962
3963
  const emitToDashboard = (type, content) => {
@@ -4031,13 +4032,13 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4031
4032
  await _utils.delay(500);
4032
4033
  await update_job('initiating conversation');
4033
4034
 
4034
- // let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
4035
+ // let conversation_doc = await db_ms.get_app_couch_doc_native(app_id, conversation_id);
4035
4036
  const reference_type = conversation_doc.reference_type;
4036
4037
  const reference_id = conversation_doc.reference_id;
4037
4038
 
4038
4039
  conversation_doc.stat = 2;
4039
4040
  conversation_doc.ts = Date.now();
4040
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
4041
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
4041
4042
 
4042
4043
  await update_job('analyzing request');
4043
4044
 
@@ -4150,16 +4151,16 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4150
4151
  try {
4151
4152
  let ai_agent_doc;
4152
4153
  try {
4153
- ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
4154
+ ai_agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
4154
4155
  if (ai_agent_doc.studio_meta.shared_from_uid) {
4155
4156
  const reference_doc = _.cloneDeep(ai_agent_doc);
4156
4157
  const app_id = await get_account_default_project_id(ai_agent_doc.studio_meta.shared_from_uid);
4157
- ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
4158
+ ai_agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
4158
4159
  ai_agent_doc.reference_doc = reference_doc;
4159
4160
  } else if (ai_agent_doc.studio_meta.installed_from_uid) {
4160
4161
  const reference_doc = _.cloneDeep(ai_agent_doc);
4161
4162
  const app_id = await get_account_default_project_id(ai_agent_doc.studio_meta.installed_from_uid);
4162
- ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
4163
+ ai_agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
4163
4164
  ai_agent_doc.reference_doc = reference_doc;
4164
4165
  }
4165
4166
  } catch (err) {
@@ -4408,7 +4409,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4408
4409
  opt.selector._id = val.ai_agent_id;
4409
4410
  }
4410
4411
 
4411
- const user_agents = await db_module.find_app_couch_query(ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id, opt);
4412
+ const user_agents = await db_ms.find_app_couch_query(ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id, opt);
4412
4413
  for (const agent_doc of user_agents.docs) {
4413
4414
  const agent = new Agent({
4414
4415
  name: agent_doc.agent_name.substring(0, 60),
@@ -4497,9 +4498,9 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4497
4498
 
4498
4499
  const set_ts_to_agent = async function () {
4499
4500
  try {
4500
- let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, reference_id);
4501
+ let agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, reference_id);
4501
4502
  agent_doc.ts = Date.now();
4502
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
4503
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, agent_doc);
4503
4504
  } catch (err) {}
4504
4505
  };
4505
4506
 
@@ -4545,7 +4546,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4545
4546
  const done = async function (output) {
4546
4547
  try {
4547
4548
  // const obj = { id: output.state._lastTurnResponse.responseId, ts: Date.now(), ai_agent_id: output.state._currentAgent.name, attachments, conversation_type: 'ai_chat' };
4548
- conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
4549
+ conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
4549
4550
 
4550
4551
  conversation_doc.ts = Date.now();
4551
4552
  conversation_doc.stat = 3;
@@ -4559,7 +4560,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4559
4560
  outputTokens += item.usage.outputTokens;
4560
4561
  }
4561
4562
 
4562
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
4563
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
4563
4564
 
4564
4565
  // let conversation_item_obj = {
4565
4566
  // _id: obj.id,
@@ -4578,7 +4579,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4578
4579
  // direction: 'out',
4579
4580
  // role: 'user',
4580
4581
  // };
4581
- // await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_item_obj);
4582
+ // await db_ms.save_app_couch_doc_native(account_profile_info.app_id, conversation_item_obj);
4582
4583
 
4583
4584
  let in_conversation_item_obj = {
4584
4585
  _id: await _common.xuda_get_uuid('chat_conversation_item'),
@@ -4597,16 +4598,16 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4597
4598
  role: 'assistant',
4598
4599
  ai_agent_id: output.state._currentAgent.name,
4599
4600
  };
4600
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, in_conversation_item_obj);
4601
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, in_conversation_item_obj);
4601
4602
 
4602
4603
  debugger;
4603
4604
 
4604
4605
  let conversation_items = await client.conversations.items.list(conversation_doc.reference_conversation_id, { order: 'asc', after: last_conversation_item });
4605
4606
 
4606
- let out_conversation_item_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj._id);
4607
+ let out_conversation_item_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj._id);
4607
4608
  // out_conversation_item_doc.stat = 3;
4608
4609
  out_conversation_item_doc.conversation_item_reference_id = conversation_items?.data?.[0]?.id;
4609
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_doc);
4610
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_doc);
4610
4611
 
4611
4612
  account_ms.record_ai_usage(uid, inputTokens, outputTokens, 'conversation chat', prompt, model, { conversation_id, ai_agent_id: in_conversation_item_obj.ai_agent_id, item_id: in_conversation_item_obj._id }, account_profile_info);
4612
4613
  // if (!conversation_doc.chat_image && (!conversation_doc.thumbnail_request_ts || Date.now() - conversation_doc.thumbnail_request_ts > 120000)) {
@@ -4804,7 +4805,7 @@ const run_plugin = async function (app_id, uid, plugin_name, method, prop_data,
4804
4805
 
4805
4806
  console.log('Testing plugin with:', { app_id, uid });
4806
4807
 
4807
- const couch_ret = await db_module.get_app_couch_nano(app_id);
4808
+ const couch_ret = await db_ms.get_app_couch_nano(app_id);
4808
4809
 
4809
4810
  const couch = couch_ret.data.couch;
4810
4811
  try {
@@ -4812,7 +4813,7 @@ const run_plugin = async function (app_id, uid, plugin_name, method, prop_data,
4812
4813
 
4813
4814
  let plugin_doc;
4814
4815
  try {
4815
- plugin_doc = await db_module.get_app_couch_doc_native(app_id, plugin_name);
4816
+ plugin_doc = await db_ms.get_app_couch_doc_native(app_id, plugin_name);
4816
4817
  } catch (error) {
4817
4818
  throw new Error(`plugin_doc ${plugin_name} not found`);
4818
4819
  }
@@ -4930,7 +4931,7 @@ const get_plugin_tool = async function (app_id, uid, plugin_name, method, prop_d
4930
4931
  return data_obj;
4931
4932
  };
4932
4933
 
4933
- const couch_ret = await db_module.get_app_couch_nano(app_id);
4934
+ const couch_ret = await db_ms.get_app_couch_nano(app_id);
4934
4935
 
4935
4936
  const couch = couch_ret.data.couch;
4936
4937
  try {
@@ -4938,7 +4939,7 @@ const get_plugin_tool = async function (app_id, uid, plugin_name, method, prop_d
4938
4939
 
4939
4940
  let plugin_doc;
4940
4941
  try {
4941
- plugin_doc = await db_module.get_app_couch_doc_native(app_id, plugin_name);
4942
+ plugin_doc = await db_ms.get_app_couch_doc_native(app_id, plugin_name);
4942
4943
  } catch (error) {
4943
4944
  throw new Error(`plugin_doc ${plugin_name} not found`);
4944
4945
  }
@@ -5110,7 +5111,7 @@ export const create_avatar = async function (req, job_id, headers) {
5110
5111
 
5111
5112
  const account_profile_info = await get_active_account_profile_info(uid);
5112
5113
 
5113
- const account_obj = await db_module.get_couch_doc_native('xuda_accounts', uid);
5114
+ const account_obj = await db_ms.get_couch_doc_native('xuda_accounts', uid);
5114
5115
  const account_info = account_obj.account_info;
5115
5116
  if (!metadata) {
5116
5117
  metadata = account_info;
@@ -5693,9 +5694,9 @@ export const conversation_actions = async function (req, job_id, headers) {
5693
5694
 
5694
5695
  // const update_profile_avatar_status = async function (uid, stat) {
5695
5696
  // try {
5696
- // let doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
5697
+ // let doc = await db_ms.get_couch_doc_native('xuda_accounts', uid);
5697
5698
  // doc.account_info.profile_avatar_stat = stat;
5698
- // const save_ret = await db_module.save_couch_doc_native('xuda_accounts', doc);
5699
+ // const save_ret = await db_ms.save_couch_doc_native('xuda_accounts', doc);
5699
5700
  // } catch (err) {
5700
5701
  // console.error(err);
5701
5702
  // }
@@ -5743,7 +5744,7 @@ export const modify_profile_avatar = async function (req, job_id, headers) {
5743
5744
  }
5744
5745
  }
5745
5746
 
5746
- const account_obj = await db_module.get_couch_doc_native('xuda_accounts', uid);
5747
+ const account_obj = await db_ms.get_couch_doc_native('xuda_accounts', uid);
5747
5748
  const account_info = account_obj.account_info;
5748
5749
  if (!metadata) {
5749
5750
  metadata = account_info;
@@ -5760,7 +5761,7 @@ export const get_ai_workspace_pending_counts = async function (req, job_id, head
5760
5761
  const { uid } = req;
5761
5762
 
5762
5763
  try {
5763
- const workspace_pending_counts_ret = await db_module.get_couch_view('xuda_team', 'pending_workspace_requests_count', {
5764
+ const workspace_pending_counts_ret = await db_ms.get_couch_view('xuda_team', 'pending_workspace_requests_count', {
5764
5765
  startkey: [uid, ''],
5765
5766
  endkey: [uid, 'zzzz'],
5766
5767
  reduce: true,
@@ -5821,8 +5822,8 @@ const create_ai_agent_image = async function (req, job_id, headers) {
5821
5822
 
5822
5823
  const { app_id, uid, ai_agent_id, tags } = req;
5823
5824
  const account_profile_info = await get_active_account_profile_info(uid);
5824
- const ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_id);
5825
- const account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
5825
+ const ai_agent_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_id);
5826
+ const account_doc = await db_ms.get_couch_doc_native('xuda_accounts', uid);
5826
5827
 
5827
5828
  // faceless humanoid for business
5828
5829
  if (account_doc.account_info.account_type === 'business') {
@@ -5964,7 +5965,7 @@ export const create_mini_app = async function (req, job_id, headers) {
5964
5965
  const name = await submit_chat_gpt_prompt({ uid, prompt: `create title for the prompt return 5 words result maximum text only without options : ${prompt}`, model: 'gpt-5-nano', metadata: { prog_id: component_doc._id, func: 'create_mini_app' } }, account_profile_info);
5965
5966
  component_doc.properties.menuName = name.data;
5966
5967
  component_doc.properties.menuTitle = name.data;
5967
- const data = await db_module.save_app_couch_doc_native(account_profile_info.app_id, component_doc);
5968
+ const data = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, component_doc);
5968
5969
  setTimeout(async () => {
5969
5970
  await update_thumbnail('mini_app', component_doc, account_profile_info.app_id, uid, job_id, headers, null, null, account_profile_info);
5970
5971
  }, 500);
@@ -5980,11 +5981,11 @@ export const pin_ai_agent = async function (req, job_id, headers) {
5980
5981
  const { uid, agent_id } = req;
5981
5982
  try {
5982
5983
  const app_id = await get_account_default_project_id(uid);
5983
- const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
5984
+ const ai_agent_doc = await db_ms.get_app_couch_doc_native(app_id, agent_id);
5984
5985
 
5985
5986
  ai_agent_doc.studio_meta.pinned = true;
5986
5987
 
5987
- const save_ret = await db_module.save_app_couch_doc(app_id, ai_agent_doc);
5988
+ const save_ret = await db_ms.save_app_couch_doc(app_id, ai_agent_doc);
5988
5989
 
5989
5990
  ws_dashboard_ms.emit_message_to_dashboard({
5990
5991
  service: 'ai_agent_pinned',
@@ -6004,11 +6005,11 @@ export const unpin_ai_agent = async function (req, job_id, headers) {
6004
6005
  const { uid, agent_id } = req;
6005
6006
  try {
6006
6007
  const app_id = await get_account_default_project_id(uid);
6007
- const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
6008
+ const ai_agent_doc = await db_ms.get_app_couch_doc_native(app_id, agent_id);
6008
6009
 
6009
6010
  ai_agent_doc.studio_meta.pinned = false;
6010
6011
 
6011
- const save_ret = await db_module.save_app_couch_doc(app_id, ai_agent_doc);
6012
+ const save_ret = await db_ms.save_app_couch_doc(app_id, ai_agent_doc);
6012
6013
 
6013
6014
  ws_dashboard_ms.emit_message_to_dashboard({
6014
6015
  service: 'ai_agent_unpinned',
@@ -6151,7 +6152,7 @@ export const add_transcript_conversation_item = async function (reference_conver
6151
6152
 
6152
6153
  const attachment_handler = async function (uid, app_id, conversation_id, attachments, account_profile_info, date_created_ts) {
6153
6154
  if (!attachments?.length) return;
6154
- let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
6155
+ let conversation_doc = await db_ms.get_app_couch_doc_native(app_id, conversation_id);
6155
6156
 
6156
6157
  // const get_transcript = async function (filename) {
6157
6158
  // async function streamToBuffer(fileStream) {
@@ -6313,21 +6314,21 @@ const attachment_handler = async function (uid, app_id, conversation_id, attachm
6313
6314
  read: { [uid]: Date.now() },
6314
6315
  process_stat,
6315
6316
  };
6316
- await db_module.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj);
6317
+ await db_ms.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_obj);
6317
6318
  system_message_items.push(out_conversation_item_obj);
6318
6319
  }
6319
6320
 
6320
- conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
6321
+ conversation_doc = await db_ms.get_app_couch_doc_native(app_id, conversation_id);
6321
6322
 
6322
6323
  conversation_doc.ts = Date.now();
6323
6324
  conversation_doc.stat = 2;
6324
6325
 
6325
- await db_module.save_app_couch_doc_native(app_id, conversation_doc);
6326
+ await db_ms.save_app_couch_doc_native(app_id, conversation_doc);
6326
6327
  return system_message_items;
6327
6328
  };
6328
6329
 
6329
6330
  export const is_spam_email = async function (uid, email, subject, account_profile_info) {
6330
- const spam_whitelists = await db_module.find_couch_query('xuda_accounts', {
6331
+ const spam_whitelists = await db_ms.find_couch_query('xuda_accounts', {
6331
6332
  selector: {
6332
6333
  docType: 'spam_whitelist',
6333
6334
  email,
@@ -6549,7 +6550,7 @@ const get_chat_picture_cache = async function (uid, docType, key) {
6549
6550
 
6550
6551
  if (words.length < 3) {
6551
6552
  // Direct exact match for short names
6552
- const ret = await db_module.find_couch_query('xuda_cache', {
6553
+ const ret = await db_ms.find_couch_query('xuda_cache', {
6553
6554
  selector: { docType, key: key.toLowerCase().trim() },
6554
6555
  limit: 1,
6555
6556
  });
@@ -6580,7 +6581,7 @@ const get_chat_picture_cache = async function (uid, docType, key) {
6580
6581
  limit: 1,
6581
6582
  };
6582
6583
 
6583
- const ret = await db_module.find_couch_query('xuda_cache', query);
6584
+ const ret = await db_ms.find_couch_query('xuda_cache', query);
6584
6585
 
6585
6586
  if (ret.docs.length > 0) {
6586
6587
  account_ms.save_cache_hit(ret.docs[0]._id);
@@ -10713,11 +10714,11 @@ export const open_ai_alive_check = async function () {};
10713
10714
 
10714
10715
  // let account_profile_info;
10715
10716
  // try {
10716
- // account_profile_info = await db_module.get_memcached_doc(key);
10717
+ // account_profile_info = await db_ms.get_memcached_doc(key);
10717
10718
  // } catch (error) {
10718
10719
  // try {
10719
10720
  // account_profile_info = await account_ms.get_active_account_profile_info(uid);
10720
- // db_module.set_memcached_doc(key, account_profile_info);
10721
+ // db_ms.set_memcached_doc(key, account_profile_info);
10721
10722
  // } catch (error) {}
10722
10723
  // }
10723
10724
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4961",
3
+ "version": "1.1.4963",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",