@xuda.io/ai_module 1.1.5587 → 1.1.5589

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 +35 -7
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1112,6 +1112,7 @@ export const get_chat_conversation = async function (req) {
1112
1112
  });
1113
1113
 
1114
1114
  for (let conversation_item_doc of conversation_items.docs || []) {
1115
+ conversation_item_doc.metadata = conversation_item_doc.metadata || {};
1115
1116
  if (!conversation_item_doc.read) {
1116
1117
  conversation_item_doc.read = {};
1117
1118
  }
@@ -1539,6 +1540,7 @@ export const archive_ai_chat = async function (req) {
1539
1540
  conversation_doc.stat = 5;
1540
1541
  conversation_doc.ts = Date.now();
1541
1542
  const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_doc);
1543
+ await update_conversation_items_stat(account_profile_info.app_id, 5, conversation_id);
1542
1544
 
1543
1545
  return save_ret;
1544
1546
  } catch (err) {
@@ -2282,10 +2284,8 @@ export const unarchive_ai_agent = async function (req) {
2282
2284
  }
2283
2285
  };
2284
2286
 
2285
- const update_agent_conversation_items_stat = async function (app_id, agent_id, stat, conversation_id) {
2286
- const target_conversation_id = conversation_id || agent_id;
2287
+ const update_conversation_items_stat = async function (app_id, stat, target_conversation_id) {
2287
2288
  const ts = Date.now();
2288
- debugger;
2289
2289
  const items_ret = await db_module.find_app_couch_query(app_id, {
2290
2290
  selector: {
2291
2291
  docType: 'chat_conversation_item',
@@ -2308,6 +2308,32 @@ const update_agent_conversation_items_stat = async function (app_id, agent_id, s
2308
2308
  return docs.length;
2309
2309
  };
2310
2310
 
2311
+ const update_conversation_stat = async function (app_id, agent_id, stat, conversation_id) {
2312
+ const conversation_ids = [];
2313
+
2314
+ if (conversation_id) {
2315
+ conversation_ids.push(conversation_id);
2316
+ } else {
2317
+ const conversations_ret = await db_module.find_app_couch_query(app_id, {
2318
+ selector: {
2319
+ docType: 'chat_conversation',
2320
+ reference_id: agent_id,
2321
+ },
2322
+ fields: ['_id'],
2323
+ limit: 9999,
2324
+ });
2325
+
2326
+ conversation_ids.push(...(conversations_ret.docs || []).map((doc) => doc._id).filter(Boolean));
2327
+ }
2328
+
2329
+ let updated = 0;
2330
+ for (const target_conversation_id of conversation_ids) {
2331
+ updated += await update_conversation_items_stat(app_id, stat, target_conversation_id);
2332
+ }
2333
+
2334
+ return updated;
2335
+ };
2336
+
2311
2337
  export const archive_ai_agent = async function (req) {
2312
2338
  let { agent_id, uid, conversation_id } = req;
2313
2339
  const account_profile_info = await get_active_account_profile_info(uid);
@@ -2320,11 +2346,10 @@ export const archive_ai_agent = async function (req) {
2320
2346
  if (agent_doc.studio_meta.createdByUid !== uid && agent_doc?.studio_meta?.account_profile_info?.uid !== uid) {
2321
2347
  throw new Error('Operation not allowed');
2322
2348
  }
2323
- debugger;
2324
2349
  agent_doc.stat = 5;
2325
2350
  agent_doc.ts = Date.now();
2326
2351
  const save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, agent_doc);
2327
- const updated_conversation_items = await update_agent_conversation_items_stat(account_profile_info.app_id, agent_id, 5, conversation_id);
2352
+ const updated_conversation_items = await update_conversation_stat(account_profile_info.app_id, agent_id, 5, conversation_id);
2328
2353
 
2329
2354
  return { code: 1, data: { save_ret, updated_conversation_items } };
2330
2355
  } catch (err) {
@@ -3893,7 +3918,7 @@ const contactGuardrailAgent = new Agent({
3893
3918
  // };
3894
3919
 
3895
3920
  export const submit_chat_conversation = async function (req, job_id, headers) {
3896
- const { profile_id, uid } = req;
3921
+ const { profile_id, uid, metadata = {} } = req;
3897
3922
  let { conversation_id } = req;
3898
3923
  try {
3899
3924
  if (!conversation_id) throw new Error('missing conversation_id');
@@ -3901,6 +3926,7 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
3901
3926
 
3902
3927
  let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
3903
3928
  req.conversation_doc = conversation_doc;
3929
+ req.metadata = metadata || {};
3904
3930
  let ret;
3905
3931
  if (conversation_doc.conversation_type === 'studio') {
3906
3932
  ret = await chat_studio(req, job_id, headers);
@@ -4880,7 +4906,8 @@ const load_ai_agent_doc = async function (app_id, agent_id) {
4880
4906
  const ai_chat_conversation = async function (req, job_id, headers) {
4881
4907
  const { uid, gtp_token, profile_id, conversation_item_id } = req;
4882
4908
  const account_profile_info = await get_active_account_profile_info(uid, profile_id);
4883
- let { prompt, conversation_doc, attachments = [], ai_agents, output_format = 'md', stream = true, with_context = true, ai_model = _conf.default_ai_model } = req;
4909
+ let { prompt, conversation_doc, attachments = [], ai_agents, output_format = 'md', stream = true, with_context = true, ai_model = _conf.default_ai_model, metadata = {} } = req;
4910
+ metadata = metadata || {};
4884
4911
 
4885
4912
  let local_ai_agents = _.cloneDeep(ai_agents);
4886
4913
 
@@ -4919,6 +4946,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4919
4946
  direction: 'out',
4920
4947
  role: 'user',
4921
4948
  job_id,
4949
+ metadata,
4922
4950
  };
4923
4951
  let out_conversation_item_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, out_conversation_item_obj);
4924
4952
  if (!conversation_item_id) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5587",
3
+ "version": "1.1.5589",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",