@xuda.io/ai_module 1.1.5596 → 1.1.5598

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 +32 -4
  2. 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
- const normalized_plan_mode = normalize_boolean(req.plan_mode ?? conversation_doc.plan_mode);
4310
- const route_to_studio = conversation_doc.conversation_type === 'studio' || req.conversation_type === 'studio' || normalized_plan_mode;
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,9 +4335,13 @@ 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
 
4342
+ // if (route_to_studio) {
4343
+ // ret = await chat_studio(req, job_id, headers);
4344
+ // } else {
4323
4345
  switch (conversation_doc.reference_type) {
4324
4346
  case 'contacts': {
4325
4347
  req._thread_reentry = Date.now() - conversation_doc.date_created_ts > 60000;
@@ -4383,6 +4405,7 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
4383
4405
  ret = await ai_chat_conversation(req, job_id, headers);
4384
4406
  break;
4385
4407
  }
4408
+ // }
4386
4409
 
4387
4410
  // if (route_to_studio) {
4388
4411
  // ret = await chat_studio(req, job_id, headers);
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.5596",
3
+ "version": "1.1.5598",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",