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