@xuda.io/ai_module 1.1.5596 → 1.1.5597
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 +80 -52
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1490,6 +1490,9 @@ export const get_chat_conversation = async function (req) {
|
|
|
1490
1490
|
|
|
1491
1491
|
for (let conversation_item_doc of conversation_items.docs || []) {
|
|
1492
1492
|
conversation_item_doc.metadata = conversation_item_doc.metadata || {};
|
|
1493
|
+
if (conversation_item_doc.plan_mode && typeof conversation_item_doc.plan_accepted === 'undefined') {
|
|
1494
|
+
conversation_item_doc.plan_accepted = false;
|
|
1495
|
+
}
|
|
1493
1496
|
if (!conversation_item_doc.read) {
|
|
1494
1497
|
conversation_item_doc.read = {};
|
|
1495
1498
|
}
|
|
@@ -4299,15 +4302,30 @@ const contactGuardrailAgent = new Agent({
|
|
|
4299
4302
|
// };
|
|
4300
4303
|
|
|
4301
4304
|
export const submit_chat_conversation = async function (req, job_id, headers) {
|
|
4302
|
-
const { profile_id, uid, metadata = {} } = req;
|
|
4305
|
+
const { profile_id, uid, metadata = {}, accepted_plan_item_id } = req;
|
|
4303
4306
|
let { conversation_id } = req;
|
|
4304
4307
|
try {
|
|
4305
4308
|
if (!conversation_id) throw new Error('missing conversation_id');
|
|
4306
4309
|
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
4307
4310
|
|
|
4308
4311
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
4309
|
-
|
|
4310
|
-
|
|
4312
|
+
let accepted_plan_item;
|
|
4313
|
+
if (accepted_plan_item_id) {
|
|
4314
|
+
accepted_plan_item = await db_module.get_app_couch_doc_native(account_profile_info.app_id, accepted_plan_item_id);
|
|
4315
|
+
if (accepted_plan_item.docType !== 'chat_conversation_item') throw new Error('accepted plan item must be a chat_conversation_item');
|
|
4316
|
+
if (accepted_plan_item.conversation_id !== conversation_id) throw new Error('accepted plan item does not belong to this conversation');
|
|
4317
|
+
if (!normalize_boolean(accepted_plan_item.plan_mode)) throw new Error('accepted plan item is not a plan-mode item');
|
|
4318
|
+
|
|
4319
|
+
if (!accepted_plan_item.plan_accepted) {
|
|
4320
|
+
accepted_plan_item.plan_accepted = true;
|
|
4321
|
+
accepted_plan_item.plan_accepted_at = Date.now();
|
|
4322
|
+
accepted_plan_item.plan_accepted_by_uid = uid;
|
|
4323
|
+
await db_module.save_app_couch_doc_native(account_profile_info.app_id, accepted_plan_item);
|
|
4324
|
+
}
|
|
4325
|
+
}
|
|
4326
|
+
|
|
4327
|
+
const normalized_plan_mode = accepted_plan_item_id ? false : normalize_boolean(req.plan_mode ?? conversation_doc.plan_mode);
|
|
4328
|
+
const route_to_studio = conversation_doc.conversation_type === 'studio' || req.conversation_type === 'studio' || normalized_plan_mode || Boolean(accepted_plan_item_id);
|
|
4311
4329
|
|
|
4312
4330
|
if (route_to_studio && conversation_doc.conversation_type !== 'studio') {
|
|
4313
4331
|
conversation_doc.conversation_type = 'studio';
|
|
@@ -4317,71 +4335,76 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
|
|
|
4317
4335
|
|
|
4318
4336
|
req.plan_mode = normalized_plan_mode;
|
|
4319
4337
|
req.conversation_doc = conversation_doc;
|
|
4338
|
+
req.accepted_plan_item = accepted_plan_item;
|
|
4320
4339
|
req.metadata = metadata || {};
|
|
4321
4340
|
let ret;
|
|
4322
4341
|
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4342
|
+
if (route_to_studio) {
|
|
4343
|
+
ret = await chat_studio(req, job_id, headers);
|
|
4344
|
+
} else {
|
|
4345
|
+
switch (conversation_doc.reference_type) {
|
|
4346
|
+
case 'contacts': {
|
|
4347
|
+
req._thread_reentry = Date.now() - conversation_doc.date_created_ts > 60000;
|
|
4326
4348
|
|
|
4327
|
-
|
|
4349
|
+
account_msa.ts_contact(uid, conversation_doc.reference_id);
|
|
4328
4350
|
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4351
|
+
switch (conversation_doc.conversation_type) {
|
|
4352
|
+
case 'note': {
|
|
4353
|
+
ret = await chat_note(req, job_id, headers);
|
|
4354
|
+
break;
|
|
4355
|
+
}
|
|
4356
|
+
case 'ai_chat': {
|
|
4357
|
+
ret = await ai_chat_conversation(req, job_id, headers);
|
|
4358
|
+
break;
|
|
4359
|
+
}
|
|
4338
4360
|
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4361
|
+
case 'email': {
|
|
4362
|
+
ret = await chat_email(req, job_id, headers);
|
|
4363
|
+
break;
|
|
4364
|
+
}
|
|
4343
4365
|
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4366
|
+
case 'chat': {
|
|
4367
|
+
ret = await contact_chat_conversation(req, job_id, headers);
|
|
4368
|
+
break;
|
|
4369
|
+
}
|
|
4370
|
+
|
|
4371
|
+
default:
|
|
4372
|
+
ret = { code: -44, data: 'invalid conversation_type ' + conversation_doc.conversation_type };
|
|
4373
|
+
break;
|
|
4347
4374
|
}
|
|
4348
4375
|
|
|
4349
|
-
|
|
4350
|
-
ret = { code: -44, data: 'invalid conversation_type ' + conversation_doc.conversation_type };
|
|
4351
|
-
break;
|
|
4376
|
+
break;
|
|
4352
4377
|
}
|
|
4353
4378
|
|
|
4354
|
-
|
|
4355
|
-
|
|
4379
|
+
case 'ai_chats': {
|
|
4380
|
+
ret = await ai_chat_conversation(req, job_id, headers);
|
|
4381
|
+
break;
|
|
4382
|
+
}
|
|
4356
4383
|
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4384
|
+
case 'apps': {
|
|
4385
|
+
ret = await chat_studio(req, job_id, headers);
|
|
4386
|
+
break;
|
|
4387
|
+
}
|
|
4361
4388
|
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4389
|
+
case 'ai_agent': {
|
|
4390
|
+
ret = await ai_chat_conversation(req, job_id, headers);
|
|
4391
|
+
break;
|
|
4392
|
+
}
|
|
4366
4393
|
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4394
|
+
case 'studio': {
|
|
4395
|
+
ret = await chat_studio(req, job_id, headers);
|
|
4396
|
+
break;
|
|
4397
|
+
}
|
|
4371
4398
|
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4399
|
+
case 'dashboard': {
|
|
4400
|
+
ret = await dashboard_chat(req, job_id, headers);
|
|
4401
|
+
break;
|
|
4402
|
+
}
|
|
4376
4403
|
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4404
|
+
default:
|
|
4405
|
+
ret = await ai_chat_conversation(req, job_id, headers);
|
|
4406
|
+
break;
|
|
4380
4407
|
}
|
|
4381
|
-
|
|
4382
|
-
default:
|
|
4383
|
-
ret = await ai_chat_conversation(req, job_id, headers);
|
|
4384
|
-
break;
|
|
4385
4408
|
}
|
|
4386
4409
|
|
|
4387
4410
|
// if (route_to_studio) {
|
|
@@ -4704,7 +4727,7 @@ const chat_email = async function (req, job_id, headers) {
|
|
|
4704
4727
|
const chat_studio = async function (req, job_id, headers) {
|
|
4705
4728
|
const { uid, profile_id } = req;
|
|
4706
4729
|
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
4707
|
-
let { prompt, conversation_doc, attachments = [], plan_mode = false, stream = true } = req;
|
|
4730
|
+
let { prompt, conversation_doc, attachments = [], plan_mode = false, stream = true, accepted_plan_item_id, accepted_plan_item } = req;
|
|
4708
4731
|
|
|
4709
4732
|
const conversation_id = conversation_doc._id;
|
|
4710
4733
|
const normalized_plan_mode = normalize_boolean(plan_mode);
|
|
@@ -4800,6 +4823,8 @@ const chat_studio = async function (req, job_id, headers) {
|
|
|
4800
4823
|
|
|
4801
4824
|
const items = (ret.docs || []).reverse().filter((item) => item.text || item.prompt || item?.file?.filename);
|
|
4802
4825
|
const conversation_history = items.map(formatConversationHistoryItem).join('\n\n');
|
|
4826
|
+
const accepted_plan_text = accepted_plan_item_id ? (accepted_plan_item?.studio_result || accepted_plan_item?.text || accepted_plan_item?.prompt || '').toString().trim() : '';
|
|
4827
|
+
const accepted_plan_context = accepted_plan_text ? `\nThe user has accepted the following plan. Execute it end-to-end:\n\n${accepted_plan_text}\n` : '';
|
|
4803
4828
|
|
|
4804
4829
|
return `You are working inside Xuda Studio.
|
|
4805
4830
|
|
|
@@ -4807,6 +4832,7 @@ Use the conversation history below as the main source of context for the current
|
|
|
4807
4832
|
The last user message is the latest request that should be handled now.
|
|
4808
4833
|
When plan_mode is true, create a plan only and do not save studio docs.
|
|
4809
4834
|
When plan_mode is false, generate the studio docs and save them into the project.
|
|
4835
|
+
${accepted_plan_context}
|
|
4810
4836
|
|
|
4811
4837
|
Conversation history:
|
|
4812
4838
|
${conversation_history || `User (studio): ${prompt}`}
|
|
@@ -4914,6 +4940,7 @@ ${conversation_history || `User (studio): ${prompt}`}
|
|
|
4914
4940
|
rtl: _common.detectRTL(prompt),
|
|
4915
4941
|
job_id,
|
|
4916
4942
|
plan_mode: normalized_plan_mode,
|
|
4943
|
+
plan_accepted: false,
|
|
4917
4944
|
studio_method: plugin_method,
|
|
4918
4945
|
target_app_id,
|
|
4919
4946
|
};
|
|
@@ -5031,6 +5058,7 @@ ${conversation_history || `User (studio): ${prompt}`}
|
|
|
5031
5058
|
rtl: _common.detectRTL(response_text),
|
|
5032
5059
|
job_id,
|
|
5033
5060
|
plan_mode: normalized_plan_mode,
|
|
5061
|
+
plan_accepted: false,
|
|
5034
5062
|
studio_method: plugin_method,
|
|
5035
5063
|
studio_result,
|
|
5036
5064
|
target_app_id,
|