@xuda.io/ai_module 1.1.5558 → 1.1.5560

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