@xuda.io/ai_module 1.1.5492 → 1.1.5494
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 +81 -16
- package/index_ms.mjs +4 -0
- package/index_msa.mjs +4 -0
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -3515,6 +3515,10 @@ function getFirstNWords(text, n = 10) {
|
|
|
3515
3515
|
return words.slice(0, n).join(' ');
|
|
3516
3516
|
}
|
|
3517
3517
|
|
|
3518
|
+
export const create_openai_conversation = async function () {
|
|
3519
|
+
return await client.conversations.create();
|
|
3520
|
+
};
|
|
3521
|
+
|
|
3518
3522
|
export const create_conversation = async function (req, job_id, headers) {
|
|
3519
3523
|
const { profile_id, uid, prompt, perform_ai_execution = true, email_id, direction = 'out', from_mailbox, date_created, ai_model = _conf.default_ai_model } = req;
|
|
3520
3524
|
let { reference_type, reference_id = '', conversation_type, email_recipient_type } = req;
|
|
@@ -3539,7 +3543,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3539
3543
|
|
|
3540
3544
|
let conversation_obj;
|
|
3541
3545
|
try {
|
|
3542
|
-
conversation_obj = await
|
|
3546
|
+
conversation_obj = await create_openai_conversation();
|
|
3543
3547
|
report_ai_status('conversations');
|
|
3544
3548
|
} catch (err) {
|
|
3545
3549
|
report_ai_status('conversations', err);
|
|
@@ -4113,21 +4117,6 @@ const chat_email = async function (req, job_id, headers) {
|
|
|
4113
4117
|
return { code: -15, data: err.message };
|
|
4114
4118
|
}
|
|
4115
4119
|
};
|
|
4116
|
-
|
|
4117
|
-
const auto_response = async function (uid, profile_id, conversation_id) {
|
|
4118
|
-
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
4119
|
-
|
|
4120
|
-
const account_profile_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, profile_id);
|
|
4121
|
-
if (!account_profile_doc.auto_respond_mode) return;
|
|
4122
|
-
|
|
4123
|
-
let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
|
|
4124
|
-
|
|
4125
|
-
if (account_profile_doc.auto_respond_mode === 'when_offline' && account_doc.socket_id) return;
|
|
4126
|
-
if (account_profile_doc.auto_respond_mode !== 'always') return;
|
|
4127
|
-
|
|
4128
|
-
///
|
|
4129
|
-
};
|
|
4130
|
-
|
|
4131
4120
|
const ai_chat_conversation = async function (req, job_id, headers) {
|
|
4132
4121
|
const { uid, gtp_token, profile_id, conversation_item_id } = req;
|
|
4133
4122
|
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
@@ -5123,6 +5112,82 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
5123
5112
|
}
|
|
5124
5113
|
};
|
|
5125
5114
|
|
|
5115
|
+
const add_conversation_item = async function (uid, contact_id, text, conversation_type, metadata = {}) {
|
|
5116
|
+
const { uid, profile_id } = req;
|
|
5117
|
+
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
5118
|
+
let { prompt: body, conversation_doc, attachments = [], ai_agents } = req;
|
|
5119
|
+
try {
|
|
5120
|
+
const conversation_id = conversation_doc._id;
|
|
5121
|
+
|
|
5122
|
+
const sender_app_id = account_profile_info.app_id; //await get_account_default_project_id(uid);
|
|
5123
|
+
let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
|
|
5124
|
+
|
|
5125
|
+
await attachment_handler(uid, sender_app_id, conversation_id, attachments, account_profile_info);
|
|
5126
|
+
let item;
|
|
5127
|
+
try {
|
|
5128
|
+
item = await client.conversations.items.create(conversation_doc.reference_conversation_id, {
|
|
5129
|
+
items: [
|
|
5130
|
+
{
|
|
5131
|
+
type: 'message',
|
|
5132
|
+
role: 'user',
|
|
5133
|
+
content: [{ type: 'input_text', text: body }],
|
|
5134
|
+
},
|
|
5135
|
+
],
|
|
5136
|
+
});
|
|
5137
|
+
report_ai_status('conversations');
|
|
5138
|
+
} catch (err) {
|
|
5139
|
+
report_ai_status('conversations', err);
|
|
5140
|
+
throw err;
|
|
5141
|
+
}
|
|
5142
|
+
|
|
5143
|
+
// let obj = { id: item.data[0].id, ts: Date.now(), uid, read: true, conversation_type: 'note' };
|
|
5144
|
+
|
|
5145
|
+
// sender_conversation_doc.messages.push(obj);
|
|
5146
|
+
// await db_module.save_app_couch_doc_native(sender_app_id, sender_conversation_doc);
|
|
5147
|
+
|
|
5148
|
+
let out_conversation_item_obj = {
|
|
5149
|
+
_id: await _common.xuda_get_uuid('chat_conversation_item'),
|
|
5150
|
+
stat: 3,
|
|
5151
|
+
docType: 'chat_conversation_item',
|
|
5152
|
+
uid,
|
|
5153
|
+
conversation_type: 'note',
|
|
5154
|
+
type: 'note',
|
|
5155
|
+
date_created_ts: Date.now(),
|
|
5156
|
+
ts: Date.now(),
|
|
5157
|
+
conversation_id,
|
|
5158
|
+
text: body,
|
|
5159
|
+
reference_id: conversation_doc.reference_id,
|
|
5160
|
+
conversation_item_reference_id: item.data[0].id,
|
|
5161
|
+
direction: 'out',
|
|
5162
|
+
role: 'user',
|
|
5163
|
+
read: { [uid]: Date.now() },
|
|
5164
|
+
rtl: _common.detectRTL(body),
|
|
5165
|
+
};
|
|
5166
|
+
|
|
5167
|
+
const save_ret = await db_module.save_app_couch_doc(sender_app_id, out_conversation_item_obj);
|
|
5168
|
+
|
|
5169
|
+
// await db_module.save_app_couch_doc_native(sender_app_id, { _id: obj.id, stat: 3, docType: 'chat_conversation_item', ts: obj.ts, uid, conversation_type: obj.conversation_type, date_created_ts: obj.ts, conversation_id, body, reference_id: conversation_doc.reference_id });
|
|
5170
|
+
|
|
5171
|
+
return save_ret; //{ code: 15, data: item };
|
|
5172
|
+
} catch (err) {
|
|
5173
|
+
return { code: -15, data: err.message };
|
|
5174
|
+
}
|
|
5175
|
+
};
|
|
5176
|
+
|
|
5177
|
+
const auto_response = async function (uid, profile_id, conversation_id) {
|
|
5178
|
+
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
5179
|
+
|
|
5180
|
+
const account_profile_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, profile_id);
|
|
5181
|
+
if (!account_profile_doc.auto_respond_mode) return;
|
|
5182
|
+
|
|
5183
|
+
let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
|
|
5184
|
+
|
|
5185
|
+
if (account_profile_doc.auto_respond_mode === 'when_offline' && account_doc.socket_id) return;
|
|
5186
|
+
if (account_profile_doc.auto_respond_mode !== 'always') return;
|
|
5187
|
+
|
|
5188
|
+
///
|
|
5189
|
+
};
|
|
5190
|
+
|
|
5126
5191
|
setTimeout(async () => {
|
|
5127
5192
|
const app_id = 'prj712ffdf5aa8adce6cedef988f9c12392'; //'prj3937cb6f9a31c8c7dea25055bba845b1'; //
|
|
5128
5193
|
const uid = 'd39126e0e2c51ffbd1aad10709fc8335';
|
package/index_ms.mjs
CHANGED
|
@@ -109,6 +109,10 @@ export const submit_chat_gpt_prompt = async function (...args) {
|
|
|
109
109
|
return await broker.send_to_queue("submit_chat_gpt_prompt", ...args);
|
|
110
110
|
};
|
|
111
111
|
|
|
112
|
+
export const create_openai_conversation = async function (...args) {
|
|
113
|
+
return await broker.send_to_queue("create_openai_conversation", ...args);
|
|
114
|
+
};
|
|
115
|
+
|
|
112
116
|
export const create_conversation = async function (...args) {
|
|
113
117
|
return await broker.send_to_queue("create_conversation", ...args);
|
|
114
118
|
};
|
package/index_msa.mjs
CHANGED
|
@@ -109,6 +109,10 @@ export const submit_chat_gpt_prompt = function (...args) {
|
|
|
109
109
|
broker.send_to_queue_async("submit_chat_gpt_prompt", ...args);
|
|
110
110
|
};
|
|
111
111
|
|
|
112
|
+
export const create_openai_conversation = function (...args) {
|
|
113
|
+
broker.send_to_queue_async("create_openai_conversation", ...args);
|
|
114
|
+
};
|
|
115
|
+
|
|
112
116
|
export const create_conversation = function (...args) {
|
|
113
117
|
broker.send_to_queue_async("create_conversation", ...args);
|
|
114
118
|
};
|