@xuda.io/ai_module 1.1.4647 → 1.1.4649
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 +65 -184
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -4220,14 +4220,28 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
4220
4220
|
|
|
4221
4221
|
let category_info = {};
|
|
4222
4222
|
switch (conversation_type) {
|
|
4223
|
+
case 'ai': {
|
|
4224
|
+
category_info = await categorize_ai_prompt(uid, prompt, account_profile_info);
|
|
4225
|
+
break;
|
|
4226
|
+
}
|
|
4227
|
+
|
|
4223
4228
|
case 'email': {
|
|
4224
4229
|
category_info = await categorize_email_subject(uid, prompt, account_profile_info);
|
|
4225
4230
|
break;
|
|
4226
4231
|
}
|
|
4227
4232
|
|
|
4233
|
+
case 'chat': {
|
|
4234
|
+
category_info = await categorize_text_message(uid, prompt, account_profile_info);
|
|
4235
|
+
break;
|
|
4236
|
+
}
|
|
4237
|
+
|
|
4228
4238
|
default:
|
|
4229
4239
|
break;
|
|
4230
4240
|
}
|
|
4241
|
+
|
|
4242
|
+
conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
|
|
4243
|
+
await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
|
|
4244
|
+
|
|
4231
4245
|
if (perform_ai_execution) {
|
|
4232
4246
|
// await update_thumbnail('conversation', conversation_doc, account_profile_info.app_id, uid, job_id, headers, null, null, account_profile_info);
|
|
4233
4247
|
}
|
|
@@ -7760,8 +7774,7 @@ function getDomainFromCategory(category) {
|
|
|
7760
7774
|
}
|
|
7761
7775
|
return 'General';
|
|
7762
7776
|
}
|
|
7763
|
-
|
|
7764
|
-
export const categorize_text_message = async function (uid, message_text, contact_info, metadata = {}) {
|
|
7777
|
+
export const categorize_text_message = async function (uid, text_message, account_profile_info) {
|
|
7765
7778
|
const text_message_categories = [
|
|
7766
7779
|
// Personal Communication (1-20)
|
|
7767
7780
|
'Casual Conversation',
|
|
@@ -7999,37 +8012,23 @@ export const categorize_text_message = async function (uid, message_text, contac
|
|
|
7999
8012
|
'Quick Reply',
|
|
8000
8013
|
'Acknowledgment Only',
|
|
8001
8014
|
];
|
|
8002
|
-
const prompt = `Categorize the text message into one of these exact categories:
|
|
8003
|
-
|
|
8004
|
-
Text Message: "${message_text.substring(0, 300)}${message_text.length > 300 ? '...' : ''}"
|
|
8005
8015
|
|
|
8006
|
-
|
|
8007
|
-
- Relationship: ${contact_info?.relationship || 'Unknown'}
|
|
8008
|
-
- Frequency: ${metadata?.message_frequency || 'Unknown'}
|
|
8009
|
-
- Time of Day: ${new Date().getHours()}h
|
|
8010
|
-
- Previous Context: ${metadata?.previous_topic || 'Not provided'}
|
|
8016
|
+
const prompt = `Categorize this text message into one of the 200 message categories based only on the message content.
|
|
8011
8017
|
|
|
8012
|
-
|
|
8013
|
-
${text_message_categories
|
|
8014
|
-
.slice(0, 25)
|
|
8015
|
-
.map((cat, index) => `${index + 1}. ${cat}`)
|
|
8016
|
-
.join('\n')}
|
|
8017
|
-
[Full list of 200 categories available to the system]
|
|
8018
|
+
Text Message: "${text_message}"
|
|
8018
8019
|
|
|
8019
|
-
|
|
8020
|
-
1. Analyze the message content
|
|
8021
|
-
2.
|
|
8022
|
-
3.
|
|
8023
|
-
4.
|
|
8024
|
-
5.
|
|
8025
|
-
|
|
8026
|
-
|
|
8027
|
-
|
|
8028
|
-
|
|
8029
|
-
|
|
8030
|
-
|
|
8031
|
-
7. If unsure, consider the most common interpretation
|
|
8032
|
-
8. For ambiguous messages, choose based on typical usage patterns
|
|
8020
|
+
INSTRUCTIONS:
|
|
8021
|
+
1. Analyze ONLY the message content - no external context
|
|
8022
|
+
2. Choose ONE category that best fits the message's primary intent
|
|
8023
|
+
3. Consider: What is the sender trying to achieve?
|
|
8024
|
+
4. Return the exact category name from the list
|
|
8025
|
+
5. If truly ambiguous, choose "General Chat"
|
|
8026
|
+
|
|
8027
|
+
Think step by step:
|
|
8028
|
+
1. What is the main purpose of this message?
|
|
8029
|
+
2. Is it informational, social, practical, emotional, or urgent?
|
|
8030
|
+
3. What action or response is expected?
|
|
8031
|
+
4. What is the tone and style?
|
|
8033
8032
|
|
|
8034
8033
|
Category:`;
|
|
8035
8034
|
|
|
@@ -8037,73 +8036,70 @@ Category:`;
|
|
|
8037
8036
|
const message_category_ret = await submit_chat_gpt_prompt({
|
|
8038
8037
|
uid,
|
|
8039
8038
|
prompt: prompt,
|
|
8040
|
-
model: 'gpt-4o-mini',
|
|
8039
|
+
model: 'gpt-4o-mini',
|
|
8041
8040
|
response_format: z.object({
|
|
8042
8041
|
message_category: z.enum(text_message_categories),
|
|
8043
8042
|
confidence_score: z.number().min(0).max(100),
|
|
8044
|
-
|
|
8045
|
-
|
|
8046
|
-
|
|
8047
|
-
relationship_dynamics: z.string().max(100).optional(),
|
|
8043
|
+
primary_intent: z.string(),
|
|
8044
|
+
emotional_tone: z.enum(['Positive', 'Neutral', 'Negative', 'Urgent', 'Playful', 'Formal', 'Casual', 'Mixed']),
|
|
8045
|
+
urgency_level: z.enum(['None', 'Low', 'Medium', 'High', 'Emergency']),
|
|
8048
8046
|
}),
|
|
8049
8047
|
metadata: {
|
|
8050
8048
|
func: 'categorize_text_message',
|
|
8051
|
-
message_length:
|
|
8052
|
-
|
|
8053
|
-
contact_relationship: contact_info?.relationship,
|
|
8054
|
-
time_sent: new Date().toISOString(),
|
|
8055
|
-
platform: metadata?.platform || 'Unknown',
|
|
8056
|
-
is_group_chat: metadata?.is_group_chat || false,
|
|
8049
|
+
message_length: text_message.length,
|
|
8050
|
+
timestamp: new Date().toISOString(),
|
|
8057
8051
|
},
|
|
8058
|
-
account_profile_info,
|
|
8052
|
+
account_profile_info, // Still passed to submit_chat_gpt_prompt but not used for context
|
|
8059
8053
|
});
|
|
8060
8054
|
|
|
8061
8055
|
if (message_category_ret && message_category_ret.code > -1) {
|
|
8062
8056
|
const data = typeof message_category_ret.data === 'string' ? JSON.parse(message_category_ret.data) : message_category_ret.data;
|
|
8063
8057
|
|
|
8064
|
-
//
|
|
8065
|
-
const message_analysis =
|
|
8058
|
+
// Simple local analysis
|
|
8059
|
+
const message_analysis = {
|
|
8060
|
+
word_count: text_message.split(/\s+/).filter((w) => w.length > 0).length,
|
|
8061
|
+
char_count: text_message.length,
|
|
8062
|
+
has_question: /[?]|(can you|could you|would you|how|what|when|where|why|who)/i.test(text_message),
|
|
8063
|
+
has_request: /(please|can you|could you|would you|help|need|want)/i.test(text_message),
|
|
8064
|
+
has_urgency: /(urgent|asap|emergency|immediately|now|quick)/i.test(text_message),
|
|
8065
|
+
};
|
|
8066
8066
|
|
|
8067
|
-
// Determine priority
|
|
8068
|
-
|
|
8067
|
+
// Determine priority based on urgency and category
|
|
8068
|
+
let priority = 3;
|
|
8069
|
+
if (data.urgency_level === 'Emergency' || data.message_category.includes('Emergency')) priority = 1;
|
|
8070
|
+
else if (data.urgency_level === 'High') priority = 2;
|
|
8071
|
+
else if (data.urgency_level === 'Low') priority = 4;
|
|
8072
|
+
else if (data.message_category.includes('Casual') || data.message_category.includes('General')) priority = 5;
|
|
8069
8073
|
|
|
8070
8074
|
return {
|
|
8071
8075
|
success: true,
|
|
8072
8076
|
category: data.message_category,
|
|
8073
8077
|
confidence: data.confidence_score,
|
|
8078
|
+
primary_intent: data.primary_intent,
|
|
8074
8079
|
emotional_tone: data.emotional_tone,
|
|
8075
8080
|
urgency: data.urgency_level,
|
|
8076
|
-
|
|
8077
|
-
priority: priority_level,
|
|
8078
|
-
relationship_dynamics: data.relationship_dynamics,
|
|
8081
|
+
priority: priority,
|
|
8079
8082
|
|
|
8080
|
-
//
|
|
8083
|
+
// Basic message analysis
|
|
8081
8084
|
word_count: message_analysis.word_count,
|
|
8082
|
-
|
|
8083
|
-
|
|
8084
|
-
|
|
8085
|
-
|
|
8086
|
-
// Context
|
|
8087
|
-
relationship: contact_info?.relationship || 'Unknown',
|
|
8088
|
-
typical_response_time: estimateResponseTime(data.message_category, contact_info?.relationship),
|
|
8085
|
+
char_count: message_analysis.char_count,
|
|
8086
|
+
has_question: message_analysis.has_question,
|
|
8087
|
+
has_request: message_analysis.has_request,
|
|
8089
8088
|
|
|
8090
8089
|
// Metadata
|
|
8091
|
-
message_preview: message_text.substring(0, 150),
|
|
8092
8090
|
timestamp: new Date().toISOString(),
|
|
8093
|
-
|
|
8094
|
-
// Suggestions
|
|
8095
|
-
auto_reply_suggestion: generateAutoReplySuggestion(data.message_category, data.emotional_tone),
|
|
8096
|
-
follow_up_reminder: shouldScheduleFollowUp(data.message_category, data.urgency_level),
|
|
8091
|
+
message_preview: text_message.substring(0, 100) + (text_message.length > 100 ? '...' : ''),
|
|
8097
8092
|
};
|
|
8098
8093
|
} else {
|
|
8099
8094
|
return {
|
|
8100
8095
|
success: false,
|
|
8101
8096
|
category: 'General Chat',
|
|
8102
8097
|
confidence: 0,
|
|
8098
|
+
primary_intent: 'Unknown',
|
|
8103
8099
|
emotional_tone: 'Neutral',
|
|
8104
|
-
urgency: '
|
|
8105
|
-
priority:
|
|
8106
|
-
message_preview:
|
|
8100
|
+
urgency: 'None',
|
|
8101
|
+
priority: 5,
|
|
8102
|
+
message_preview: text_message.substring(0, 100),
|
|
8107
8103
|
error: message_category_ret?.message || 'API call failed',
|
|
8108
8104
|
timestamp: new Date().toISOString(),
|
|
8109
8105
|
};
|
|
@@ -8114,128 +8110,13 @@ Category:`;
|
|
|
8114
8110
|
success: false,
|
|
8115
8111
|
category: 'General Chat',
|
|
8116
8112
|
confidence: 0,
|
|
8113
|
+
primary_intent: 'Error',
|
|
8117
8114
|
emotional_tone: 'Neutral',
|
|
8118
|
-
urgency: '
|
|
8119
|
-
priority:
|
|
8120
|
-
message_preview:
|
|
8115
|
+
urgency: 'None',
|
|
8116
|
+
priority: 5,
|
|
8117
|
+
message_preview: text_message.substring(0, 100),
|
|
8121
8118
|
error: error.message,
|
|
8122
8119
|
timestamp: new Date().toISOString(),
|
|
8123
8120
|
};
|
|
8124
8121
|
}
|
|
8125
8122
|
};
|
|
8126
|
-
|
|
8127
|
-
// Helper functions
|
|
8128
|
-
function analyzeMessageCharacteristics(message, category) {
|
|
8129
|
-
const wordCount = message.split(/\s+/).length;
|
|
8130
|
-
const charCount = message.length;
|
|
8131
|
-
const containsQuestions = /[\?]|(can you|could you|would you|will you|do you|are you|is there|how|what|when|where|why|who)/i.test(message);
|
|
8132
|
-
const containsRequests = /(please|can you|could you|would you|need you|want you|help me|assist)/i.test(message);
|
|
8133
|
-
const containsUrgency = /(urgent|asap|immediately|right now|emergency|quick|fast|hurry)/i.test(message);
|
|
8134
|
-
const containsTimeReferences = /\b(today|tomorrow|now|soon|later|tonight|morning|afternoon|evening)\b/i.test(message);
|
|
8135
|
-
const containsEmoticons = /[:;][\-]?[\)\(DP]|<\/?3|\([^\)]*\)|[\u{1F600}-\u{1F64F}]/gu.test(message);
|
|
8136
|
-
|
|
8137
|
-
return {
|
|
8138
|
-
word_count: wordCount,
|
|
8139
|
-
char_count: charCount,
|
|
8140
|
-
contains_questions: containsQuestions,
|
|
8141
|
-
contains_requests: containsRequests,
|
|
8142
|
-
time_sensitive: containsTimeReferences || containsUrgency,
|
|
8143
|
-
has_emoticons: containsEmoticons,
|
|
8144
|
-
message_type: charCount < 50 ? 'Short' : charCount < 200 ? 'Medium' : 'Long',
|
|
8145
|
-
};
|
|
8146
|
-
}
|
|
8147
|
-
|
|
8148
|
-
function calculateMessagePriority(urgency, category, relationship) {
|
|
8149
|
-
let priority = 3; // Default medium priority
|
|
8150
|
-
|
|
8151
|
-
// Emergency categories get highest priority
|
|
8152
|
-
if (urgency === 'Emergency' || category.includes('Emergency')) {
|
|
8153
|
-
return 1;
|
|
8154
|
-
}
|
|
8155
|
-
|
|
8156
|
-
// High urgency
|
|
8157
|
-
if (urgency === 'High' || category.includes('Urgent')) {
|
|
8158
|
-
priority = 2;
|
|
8159
|
-
}
|
|
8160
|
-
|
|
8161
|
-
// Low urgency or casual messages
|
|
8162
|
-
if (urgency === 'Low' || category.includes('Casual') || category.includes('General Chat') || category.includes('Random Thought')) {
|
|
8163
|
-
priority = 4;
|
|
8164
|
-
}
|
|
8165
|
-
|
|
8166
|
-
// Adjust based on relationship
|
|
8167
|
-
if (relationship === 'Family' || relationship === 'Spouse' || relationship === 'Close Friend') {
|
|
8168
|
-
priority = Math.max(1, priority - 1); // Higher priority for close relations
|
|
8169
|
-
}
|
|
8170
|
-
|
|
8171
|
-
return Math.min(Math.max(priority, 1), 5); // Ensure 1-5 scale
|
|
8172
|
-
}
|
|
8173
|
-
|
|
8174
|
-
function estimateResponseTime(category, relationship) {
|
|
8175
|
-
const baseTimes = {
|
|
8176
|
-
'Emergency Alert': 'Immediately',
|
|
8177
|
-
'Urgent Assistance': 'Within minutes',
|
|
8178
|
-
'Help Request': 'Within 30 minutes',
|
|
8179
|
-
'Question Asking': 'Within hours',
|
|
8180
|
-
'Meeting Coordination': 'Within 2 hours',
|
|
8181
|
-
'Casual Conversation': 'Within 24 hours',
|
|
8182
|
-
'General Chat': 'Within 48 hours',
|
|
8183
|
-
};
|
|
8184
|
-
|
|
8185
|
-
// Find matching base time
|
|
8186
|
-
for (const [key, time] of Object.entries(baseTimes)) {
|
|
8187
|
-
if (category.includes(key) || key.includes(category.split(' ')[0])) {
|
|
8188
|
-
return time;
|
|
8189
|
-
}
|
|
8190
|
-
}
|
|
8191
|
-
|
|
8192
|
-
// Default based on relationship
|
|
8193
|
-
switch (relationship) {
|
|
8194
|
-
case 'Close Friend':
|
|
8195
|
-
case 'Family':
|
|
8196
|
-
case 'Spouse':
|
|
8197
|
-
return 'Within 12 hours';
|
|
8198
|
-
case 'Colleague':
|
|
8199
|
-
case 'Professional':
|
|
8200
|
-
return 'Within business hours';
|
|
8201
|
-
case 'Acquaintance':
|
|
8202
|
-
return 'Within 2 days';
|
|
8203
|
-
default:
|
|
8204
|
-
return 'Within 24 hours';
|
|
8205
|
-
}
|
|
8206
|
-
}
|
|
8207
|
-
|
|
8208
|
-
function generateAutoReplySuggestion(category, tone) {
|
|
8209
|
-
const suggestions = {
|
|
8210
|
-
'Meeting Coordination': "Got it! I'll be there.",
|
|
8211
|
-
'Question Asking': 'Let me check and get back to you.',
|
|
8212
|
-
'Urgent Assistance': 'On it! Getting help now.',
|
|
8213
|
-
'Casual Conversation': 'Hey! Good to hear from you.',
|
|
8214
|
-
'Thank You Message': "You're welcome! 😊",
|
|
8215
|
-
'Birthday Wishes': 'Thank you so much! 🎉',
|
|
8216
|
-
};
|
|
8217
|
-
|
|
8218
|
-
for (const [key, reply] of Object.entries(suggestions)) {
|
|
8219
|
-
if (category.includes(key)) {
|
|
8220
|
-
return reply;
|
|
8221
|
-
}
|
|
8222
|
-
}
|
|
8223
|
-
|
|
8224
|
-
// Generic suggestions based on tone
|
|
8225
|
-
switch (tone) {
|
|
8226
|
-
case 'Positive':
|
|
8227
|
-
return 'Great! Thanks for sharing!';
|
|
8228
|
-
case 'Negative':
|
|
8229
|
-
return 'I understand. Let me know if I can help.';
|
|
8230
|
-
case 'Urgent':
|
|
8231
|
-
return 'Received. Looking into this now.';
|
|
8232
|
-
default:
|
|
8233
|
-
return 'Got your message. Talk soon!';
|
|
8234
|
-
}
|
|
8235
|
-
}
|
|
8236
|
-
|
|
8237
|
-
function shouldScheduleFollowUp(category, urgency) {
|
|
8238
|
-
const followUpCategories = ['Question Asking', 'Information Request', 'Meeting Coordination', 'Help Request', 'Task Assignment', 'Deadline Reminder'];
|
|
8239
|
-
|
|
8240
|
-
return followUpCategories.some((fc) => category.includes(fc)) && (urgency === 'Medium' || urgency === 'High');
|
|
8241
|
-
}
|