@xuda.io/ai_module 1.1.5553 → 1.1.5554

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 +151 -8
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -5162,19 +5162,162 @@ const add_conversation_item = async function (uid, profile_id, conversation_id,
5162
5162
  };
5163
5163
 
5164
5164
  const auto_response = async function (uid, profile_id, contact_id, conversation_id) {
5165
- const account_profile_info = await get_active_account_profile_info(uid, profile_id);
5165
+ try {
5166
+ const account_profile_info = await get_active_account_profile_info(uid, profile_id);
5167
+
5168
+ const account_profile_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, profile_id);
5169
+ if (account_profile_doc.stat >= 4) return;
5170
+ if (!account_profile_doc.auto_respond) return;
5171
+ if (!account_profile_doc.auto_respond_mode) return;
5172
+
5173
+ let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
5174
+
5175
+ if (account_profile_doc.auto_respond_mode === 'when_offline' && account_doc.socket_id) return;
5176
+ if (!['always', 'when_offline'].includes(account_profile_doc.auto_respond_mode)) return;
5177
+
5178
+ const contact_doc = await get_contact_info(account_profile_info.uid, null, contact_id);
5179
+ if (!contact_doc?.contact_reference_conversation_id) return;
5180
+
5181
+ const conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
5182
+
5183
+ let auto_respond_agents = account_profile_doc.auto_respond_agents;
5184
+ if (auto_respond_agents === null || typeof auto_respond_agents === 'undefined') return;
5185
+
5186
+ if (!Array.isArray(auto_respond_agents)) {
5187
+ auto_respond_agents = [];
5188
+ }
5189
+
5190
+ if (!auto_respond_agents.length) {
5191
+ const ai_agents_ret = await get_user_ai_agents(uid);
5192
+ auto_respond_agents = ai_agents_ret.docs.map((doc) => doc._id).filter(Boolean);
5193
+ }
5166
5194
 
5167
- const account_profile_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, profile_id);
5168
- if (!account_profile_doc.auto_respond_mode) return;
5195
+ if (!auto_respond_agents.length) return;
5169
5196
 
5170
- let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
5197
+ const runner = new Runner();
5198
+ const app_obj = await get_app_obj(account_profile_info.app_id);
5199
+ const userName = await account_ms.get_user_name(uid);
5200
+ const account_info = (await get_account_name({ uid })).data;
5201
+ let context = {
5202
+ app_id: account_profile_info.app_id,
5203
+ uid,
5204
+ userName,
5205
+ account_info,
5206
+ app_obj,
5207
+ account_id: uid,
5208
+ contact: {
5209
+ _id: contact_doc._id,
5210
+ name: contact_doc.name,
5211
+ email: contact_doc.email,
5212
+ contact_uid: contact_doc.contact_uid,
5213
+ },
5214
+ auto_response: true,
5215
+ conversation_type: 'email',
5216
+ profile_name: account_profile_doc.profile_name,
5217
+ profile_signature: account_profile_doc.profile_signature,
5218
+ };
5219
+
5220
+ let agents = [];
5221
+ let model = _conf.default_ai_model;
5171
5222
 
5172
- if (account_profile_doc.auto_respond_mode === 'when_offline' && account_doc.socket_id) return;
5173
- if (account_profile_doc.auto_respond_mode !== 'always') return;
5223
+ for (const agent_id of auto_respond_agents) {
5224
+ try {
5225
+ const ai_agent_doc = await load_ai_agent_doc(account_profile_info.app_id, agent_id);
5226
+ if (ai_agent_doc?.agentConfig?.agent_ai_model) {
5227
+ model = ai_agent_doc.agentConfig.agent_ai_model;
5228
+ }
5174
5229
 
5175
- const contact_doc = await get_contact_info(account_profile_info.uid, null, contact_id);
5230
+ const tools_ret = await get_ai_agent_tools({
5231
+ ai_agent_doc,
5232
+ agent_id,
5233
+ reference_type: 'ai_agents',
5234
+ prompt_suggestion_activated: false,
5235
+ chat_suggestion_activated: false,
5236
+ gtp_token: undefined,
5237
+ uid,
5238
+ account_profile_info,
5239
+ job_id: undefined,
5240
+ headers: {},
5241
+ context,
5242
+ app_id: account_profile_info.app_id,
5243
+ });
5176
5244
 
5177
- ///
5245
+ context = tools_ret.context;
5246
+
5247
+ const agent = new Agent({
5248
+ name: `${ai_agent_doc._id}`.substring(0, 55),
5249
+ instructions: `${ai_agent_doc.agentConfig.agent_instructions}
5250
+
5251
+ You are composing an automatic email reply.
5252
+ Use the existing conversation history as context.
5253
+ Reply as ${account_profile_doc.profile_name || userName}.
5254
+ Return only the email body text, ready to send.
5255
+ Keep the response concise, natural, and professional.
5256
+ If a signature is available, append it at the end:
5257
+ ${account_profile_doc.profile_signature || ''}`.trim(),
5258
+ model,
5259
+ tools: tools_ret.tools,
5260
+ metadata: {
5261
+ agent_id: ai_agent_doc._id,
5262
+ ts: Date.now(),
5263
+ auto_response: true,
5264
+ },
5265
+ tool_resources: tools_ret.tool_resources,
5266
+ });
5267
+
5268
+ if (tools_ret.eligible_agent) {
5269
+ agents.push(agent);
5270
+ }
5271
+ } catch (err) {
5272
+ console.error('auto_response agent load failed', err?.message || err);
5273
+ }
5274
+ }
5275
+
5276
+ if (!agents.length) return;
5277
+
5278
+ let active_agent;
5279
+ if (agents.length === 1) {
5280
+ active_agent = agents[0];
5281
+ } else {
5282
+ active_agent = new Agent({
5283
+ name: 'Auto Response Triage Agent',
5284
+ instructions: `Select the best specialist agent to draft an automatic email response.
5285
+ Return only the final email body text.`,
5286
+ model,
5287
+ handoffs: agents,
5288
+ });
5289
+ }
5290
+
5291
+ const prompt = `Write an automatic email reply to the latest incoming message in this conversation.
5292
+ Do not mention that the reply is automated.
5293
+ Use the conversation history for context.
5294
+ Return only the email body.`;
5295
+
5296
+ const output = await runner.run(active_agent, prompt, {
5297
+ conversationId: contact_doc.contact_reference_conversation_id,
5298
+ context,
5299
+ stream: false,
5300
+ });
5301
+
5302
+ const response_text = output?.state?._currentStep?.output?.trim();
5303
+ if (!response_text) return;
5304
+
5305
+ await chat_email(
5306
+ {
5307
+ profile_id,
5308
+ uid,
5309
+ prompt: response_text,
5310
+ conversation_doc,
5311
+ attachments: [],
5312
+ perform_ai_execution: false,
5313
+ direction: 'out',
5314
+ },
5315
+ undefined,
5316
+ undefined,
5317
+ );
5318
+ } catch (err) {
5319
+ console.error('auto_response failed', err);
5320
+ }
5178
5321
  };
5179
5322
 
5180
5323
  setTimeout(async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5553",
3
+ "version": "1.1.5554",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",