@xuda.io/ai_module 1.1.5559 → 1.1.5561

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 +68 -27
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -2434,6 +2434,7 @@ export const get_apps = async function (req, job_id, headers) {
2434
2434
  if (!account_profile_info.is_main) {
2435
2435
  opt.selector['studio_meta.account_profiles'] = { $in: [account_profile_info.account_profile_id] };
2436
2436
  } else {
2437
+ debugger;
2437
2438
  const active_profiles = await db_module.find_app_couch_query(account_profile_info.app_id, {
2438
2439
  selector: {
2439
2440
  docType: 'account_profile',
@@ -3882,7 +3883,7 @@ const update_conversation_mood_level = async function (uid, target_contacts = []
3882
3883
  const contact_chat_conversation = async function (req, job_id, headers) {
3883
3884
  const { uid, profile_id } = req;
3884
3885
  const account_profile_info = await get_active_account_profile_info(uid, profile_id);
3885
- let { prompt, conversation_doc, attachments = [], ai_agents } = req;
3886
+ let { prompt, conversation_doc, attachments = [], ai_agents, _auto_response = false } = req;
3886
3887
 
3887
3888
  const conversation_id = conversation_doc._id;
3888
3889
 
@@ -3905,9 +3906,9 @@ const contact_chat_conversation = async function (req, job_id, headers) {
3905
3906
  }
3906
3907
 
3907
3908
  await db_module.save_app_couch_doc_native(receiver_app_id, receiver_conversation_doc);
3908
- if (conversation_doc.reference_type === 'contacts') {
3909
- const receiver_account_profile_info = await get_active_account_profile_info(conversation_doc.uid);
3910
- auto_response(receiver_account_profile_info.uid, receiver_account_profile_info.profile_id, conversation_doc.reference_id, 'chat');
3909
+ if (conversation_doc.reference_type === 'contacts' && !_auto_response) {
3910
+ const receiver_account_profile_info = await get_active_account_profile_info(receiver_contact_doc.contact_uid);
3911
+ auto_response(receiver_account_profile_info.uid, receiver_account_profile_info.account_profile_id, receiver_conversation_doc.reference_id, 'chat');
3911
3912
  }
3912
3913
 
3913
3914
  await db_module.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
@@ -4105,7 +4106,7 @@ const chat_email = async function (req, job_id, headers) {
4105
4106
  }
4106
4107
 
4107
4108
  if (conversation_doc.reference_type === 'contacts' && from_mailbox) {
4108
- auto_response(uid, profile_id, conversation_doc.reference_id, conversation_id, 'email');
4109
+ auto_response(uid, profile_id, conversation_doc.reference_id, 'email');
4109
4110
  }
4110
4111
 
4111
4112
  return save_ret;
@@ -5165,7 +5166,7 @@ const add_conversation_item = async function (uid, profile_id, conversation_id,
5165
5166
  }
5166
5167
  };
5167
5168
 
5168
- const auto_response = async function (uid, profile_id, contact_id, conversation_type) {
5169
+ const auto_response = async function (uid, profile_id, contact_id, conversation_type = 'email') {
5169
5170
  try {
5170
5171
  const account_profile_info = await get_active_account_profile_info(uid, profile_id);
5171
5172
 
@@ -5182,6 +5183,20 @@ const auto_response = async function (uid, profile_id, contact_id, conversation_
5182
5183
  const contact_doc = await get_contact_info(account_profile_info.uid, null, contact_id);
5183
5184
  if (!contact_doc?.contact_reference_conversation_id) return;
5184
5185
 
5186
+ const conversation_docs = await db_module.find_app_couch_query(account_profile_info.app_id, {
5187
+ selector: {
5188
+ docType: 'chat_conversation',
5189
+ reference_type: 'contacts',
5190
+ reference_id: contact_id,
5191
+ conversation_type,
5192
+ stat: { $lt: 4 },
5193
+ },
5194
+ limit: 1,
5195
+ sort: [{ ts: 'desc' }],
5196
+ });
5197
+ const conversation_doc = conversation_docs.docs[0];
5198
+ if (!conversation_doc) return;
5199
+
5185
5200
  // const conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
5186
5201
 
5187
5202
  let auto_respond_agents = account_profile_doc.auto_respond_agents;
@@ -5216,7 +5231,7 @@ const auto_response = async function (uid, profile_id, contact_id, conversation_
5216
5231
  contact_uid: contact_doc.contact_uid,
5217
5232
  },
5218
5233
  auto_response: true,
5219
- conversation_type: 'email',
5234
+ conversation_type,
5220
5235
  profile_name: account_profile_doc.profile_name,
5221
5236
  profile_signature: account_profile_doc.profile_signature,
5222
5237
  };
@@ -5252,13 +5267,17 @@ const auto_response = async function (uid, profile_id, contact_id, conversation_
5252
5267
  name: `${ai_agent_doc._id}`.substring(0, 55),
5253
5268
  instructions: `${ai_agent_doc.agentConfig.agent_instructions}
5254
5269
 
5255
- You are composing an automatic email reply.
5270
+ You are composing an automatic ${conversation_type === 'chat' ? 'chat' : 'email'} reply.
5256
5271
  Use the existing conversation history as context.
5257
5272
  Reply as ${account_profile_doc.profile_name || userName}.
5258
- Return only the email body text, ready to send.
5259
- Keep the response concise, natural, and professional.
5273
+ ${conversation_type === 'chat' ? 'Return only the chat message text, ready to send.' : 'Return only the email body text, ready to send.'}
5274
+ Keep the response concise, natural, and professional.${
5275
+ conversation_type === 'email'
5276
+ ? `
5260
5277
  If a signature is available, append it at the end:
5261
- ${account_profile_doc.profile_signature || ''}`.trim(),
5278
+ ${account_profile_doc.profile_signature || ''}`
5279
+ : ''
5280
+ }`.trim(),
5262
5281
  model,
5263
5282
  tools: tools_ret.tools,
5264
5283
  metadata: {
@@ -5285,14 +5304,20 @@ ${account_profile_doc.profile_signature || ''}`.trim(),
5285
5304
  } else {
5286
5305
  active_agent = new Agent({
5287
5306
  name: 'Auto Response Triage Agent',
5288
- instructions: `Select the best specialist agent to draft an automatic email response.
5289
- Return only the final email body text.`,
5307
+ instructions: `Select the best specialist agent to draft an automatic ${conversation_type === 'chat' ? 'chat' : 'email'} response.
5308
+ Return only the final ${conversation_type === 'chat' ? 'chat message text' : 'email body text'}.`,
5290
5309
  model,
5291
5310
  handoffs: agents,
5292
5311
  });
5293
5312
  }
5294
5313
 
5295
- const prompt = `Write an automatic email reply to the latest incoming message in this conversation.
5314
+ const prompt =
5315
+ conversation_type === 'chat'
5316
+ ? `Write an automatic reply to the latest incoming chat message in this conversation.
5317
+ Do not mention that the reply is automated.
5318
+ Use the conversation history for context.
5319
+ Return only the chat message text.`
5320
+ : `Write an automatic email reply to the latest incoming message in this conversation.
5296
5321
  Do not mention that the reply is automated.
5297
5322
  Use the conversation history for context.
5298
5323
  Return only the email body.`;
@@ -5306,19 +5331,35 @@ Return only the email body.`;
5306
5331
  const response_text = output?.state?._currentStep?.output?.trim();
5307
5332
  if (!response_text) return;
5308
5333
 
5309
- await chat_email(
5310
- {
5311
- profile_id,
5312
- uid,
5313
- prompt: response_text,
5314
- conversation_doc,
5315
- attachments: [],
5316
- perform_ai_execution: false,
5317
- direction: 'out',
5318
- },
5319
- undefined,
5320
- undefined,
5321
- );
5334
+ if (conversation_type === 'chat') {
5335
+ await contact_chat_conversation(
5336
+ {
5337
+ profile_id,
5338
+ uid,
5339
+ prompt: response_text,
5340
+ conversation_doc,
5341
+ attachments: [],
5342
+ ai_agents: auto_respond_agents,
5343
+ _auto_response: true,
5344
+ },
5345
+ undefined,
5346
+ undefined,
5347
+ );
5348
+ } else {
5349
+ await chat_email(
5350
+ {
5351
+ profile_id,
5352
+ uid,
5353
+ prompt: response_text,
5354
+ conversation_doc,
5355
+ attachments: [],
5356
+ perform_ai_execution: false,
5357
+ direction: 'out',
5358
+ },
5359
+ undefined,
5360
+ undefined,
5361
+ );
5362
+ }
5322
5363
  } catch (err) {
5323
5364
  console.error('auto_response failed', err);
5324
5365
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5559",
3
+ "version": "1.1.5561",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",