@xuda.io/ai_module 1.1.5572 → 1.1.5574
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 +393 -25
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ console.log('AI Module loaded...');
|
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
6
7
|
|
|
7
8
|
import sharp from 'sharp';
|
|
8
9
|
|
|
@@ -3567,6 +3568,46 @@ function getFirstNWords(text, n = 10) {
|
|
|
3567
3568
|
return words.slice(0, n).join(' ');
|
|
3568
3569
|
}
|
|
3569
3570
|
|
|
3571
|
+
const get_error_message = function (err, fallback = 'Unknown error') {
|
|
3572
|
+
if (!err) return fallback;
|
|
3573
|
+
|
|
3574
|
+
if (typeof err === 'string') {
|
|
3575
|
+
return err;
|
|
3576
|
+
}
|
|
3577
|
+
|
|
3578
|
+
if (err instanceof Error) {
|
|
3579
|
+
return err.message || fallback;
|
|
3580
|
+
}
|
|
3581
|
+
|
|
3582
|
+
if (typeof err?.message === 'string' && err.message) {
|
|
3583
|
+
return err.message;
|
|
3584
|
+
}
|
|
3585
|
+
|
|
3586
|
+
if (typeof err?.data === 'string' && err.data) {
|
|
3587
|
+
return err.data;
|
|
3588
|
+
}
|
|
3589
|
+
|
|
3590
|
+
try {
|
|
3591
|
+
const serialized = JSON.stringify(err);
|
|
3592
|
+
if (serialized && serialized !== '{}') {
|
|
3593
|
+
return serialized;
|
|
3594
|
+
}
|
|
3595
|
+
} catch (error) {}
|
|
3596
|
+
|
|
3597
|
+
return String(err);
|
|
3598
|
+
};
|
|
3599
|
+
|
|
3600
|
+
const get_plugin_import_specifier = function (app_id, plugin_name, resource, dev = true) {
|
|
3601
|
+
const absolute_path = dev ? path.join(process.env.XUDA_HOME, 'plugins', plugin_name, resource) : path.join(_conf.plugins_drive_path, app_id, 'node_modules', plugin_name, resource);
|
|
3602
|
+
|
|
3603
|
+
const file_url = pathToFileURL(absolute_path);
|
|
3604
|
+
if (dev) {
|
|
3605
|
+
file_url.searchParams.set('ts', `${Date.now()}`);
|
|
3606
|
+
}
|
|
3607
|
+
|
|
3608
|
+
return file_url.href;
|
|
3609
|
+
};
|
|
3610
|
+
|
|
3570
3611
|
export const create_openai_conversation = async function () {
|
|
3571
3612
|
return await client.conversations.create();
|
|
3572
3613
|
};
|
|
@@ -3609,7 +3650,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3609
3650
|
title: conversation_type !== 'email' ? getFirstNWords(prompt, 10) : prompt,
|
|
3610
3651
|
date_created_ts: date_created ? new Date(date_created).getTime() : d,
|
|
3611
3652
|
ts: d,
|
|
3612
|
-
stat: !conversation_type ||
|
|
3653
|
+
stat: !conversation_type || ['ai_chat', 'studio'].includes(conversation_type) ? 1 : 3,
|
|
3613
3654
|
uid,
|
|
3614
3655
|
messages: [],
|
|
3615
3656
|
reference_type,
|
|
@@ -3728,6 +3769,11 @@ const process_conversation = async function (uid, conversation_id, account_profi
|
|
|
3728
3769
|
break;
|
|
3729
3770
|
}
|
|
3730
3771
|
|
|
3772
|
+
case 'studio': {
|
|
3773
|
+
category_info = await categorize_ai_prompt(uid, prompt, account_profile_info);
|
|
3774
|
+
break;
|
|
3775
|
+
}
|
|
3776
|
+
|
|
3731
3777
|
case 'email': {
|
|
3732
3778
|
category_info = await categorize_email_subject(uid, prompt, account_profile_info);
|
|
3733
3779
|
break;
|
|
@@ -3812,7 +3858,9 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
|
|
|
3812
3858
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
3813
3859
|
req.conversation_doc = conversation_doc;
|
|
3814
3860
|
let ret;
|
|
3815
|
-
if (conversation_doc.
|
|
3861
|
+
if (conversation_doc.conversation_type === 'studio') {
|
|
3862
|
+
ret = await chat_studio(req, job_id, headers);
|
|
3863
|
+
} else if (conversation_doc.reference_type === 'contacts') {
|
|
3816
3864
|
req._thread_reentry = Date.now() - conversation_doc.date_created_ts > 60000;
|
|
3817
3865
|
|
|
3818
3866
|
account_msa.ts_contact(uid, conversation_doc.reference_id);
|
|
@@ -4127,6 +4175,341 @@ const chat_email = async function (req, job_id, headers) {
|
|
|
4127
4175
|
}
|
|
4128
4176
|
};
|
|
4129
4177
|
|
|
4178
|
+
const chat_studio = async function (req, job_id, headers) {
|
|
4179
|
+
const { uid, profile_id } = req;
|
|
4180
|
+
const account_profile_info = await get_active_account_profile_info(uid, profile_id);
|
|
4181
|
+
let { prompt, conversation_doc, attachments = [], plan_mode = false, stream = true } = req;
|
|
4182
|
+
|
|
4183
|
+
const conversation_id = conversation_doc._id;
|
|
4184
|
+
const normalized_plan_mode = typeof plan_mode === 'string' ? ['true', '1', 'yes', 'on'].includes(plan_mode.toLowerCase()) : Boolean(plan_mode);
|
|
4185
|
+
const save_result_to_studio = normalized_plan_mode ? false : true;
|
|
4186
|
+
const plugin_name = '@xuda.io/xuda-library-plugin-studio-ai-app-builder';
|
|
4187
|
+
const plugin_method = normalized_plan_mode ? 'app_planner' : 'app_builder';
|
|
4188
|
+
const target_app_id = (await _common.get_project_app_id(req.app_id || conversation_doc.reference_id || account_profile_info.app_id, true)) || account_profile_info.app_id;
|
|
4189
|
+
|
|
4190
|
+
const prompt_conversation_item_id = await _common.xuda_get_uuid('chat_conversation_item');
|
|
4191
|
+
const response_conversation_item_id = await _common.xuda_get_uuid('chat_conversation_item');
|
|
4192
|
+
|
|
4193
|
+
const emitToDashboard = (type, content, params) => {
|
|
4194
|
+
ws_dashboard_msa.emit_message_to_dashboard({
|
|
4195
|
+
service: type,
|
|
4196
|
+
to: uid,
|
|
4197
|
+
data: {
|
|
4198
|
+
conversation_id,
|
|
4199
|
+
job_id,
|
|
4200
|
+
delta: content,
|
|
4201
|
+
prompt_conversation_item_id,
|
|
4202
|
+
response_conversation_item_id,
|
|
4203
|
+
params,
|
|
4204
|
+
},
|
|
4205
|
+
});
|
|
4206
|
+
};
|
|
4207
|
+
|
|
4208
|
+
let response_started = false;
|
|
4209
|
+
const ensureResponseStarted = function () {
|
|
4210
|
+
if (!response_started) {
|
|
4211
|
+
response_started = true;
|
|
4212
|
+
emitToDashboard('response_start');
|
|
4213
|
+
}
|
|
4214
|
+
};
|
|
4215
|
+
|
|
4216
|
+
const emitStreamDelta = function (text) {
|
|
4217
|
+
if (!text) return;
|
|
4218
|
+
ensureResponseStarted();
|
|
4219
|
+
emitToDashboard('stream_delta', text);
|
|
4220
|
+
};
|
|
4221
|
+
|
|
4222
|
+
const streamText = function (text, chunk_size = 280) {
|
|
4223
|
+
if (!stream || !text) return;
|
|
4224
|
+
for (let i = 0; i < text.length; i += chunk_size) {
|
|
4225
|
+
emitStreamDelta(text.slice(i, i + chunk_size));
|
|
4226
|
+
}
|
|
4227
|
+
};
|
|
4228
|
+
|
|
4229
|
+
const emitThinkingPhase = function (content) {
|
|
4230
|
+
if (!content) return;
|
|
4231
|
+
emitToDashboard('stream_phase', content.trim(), { update: true });
|
|
4232
|
+
};
|
|
4233
|
+
|
|
4234
|
+
const formatConversationHistoryItem = function (item) {
|
|
4235
|
+
const role = item.role === 'assistant' ? 'Assistant' : 'User';
|
|
4236
|
+
const type = item.conversation_type || item.type || 'message';
|
|
4237
|
+
const file_name = item?.file?.filename ? ` file=${item.file.filename}` : '';
|
|
4238
|
+
let text = (item.text || item.prompt || '').trim();
|
|
4239
|
+
if (text.length > 2000) {
|
|
4240
|
+
text = `${text.slice(0, 2000)}...`;
|
|
4241
|
+
}
|
|
4242
|
+
|
|
4243
|
+
return `${role} (${type}${file_name}): ${text}`;
|
|
4244
|
+
};
|
|
4245
|
+
|
|
4246
|
+
const get_studio_context_prompt = async function () {
|
|
4247
|
+
const ret = await db_module.find_app_couch_query(account_profile_info.app_id, {
|
|
4248
|
+
selector: {
|
|
4249
|
+
docType: 'chat_conversation_item',
|
|
4250
|
+
conversation_id,
|
|
4251
|
+
stat: 3,
|
|
4252
|
+
},
|
|
4253
|
+
limit: 12,
|
|
4254
|
+
sort: [{ date_created_ts: 'desc' }],
|
|
4255
|
+
});
|
|
4256
|
+
|
|
4257
|
+
const items = (ret.docs || []).reverse().filter((item) => item.text || item.prompt || item?.file?.filename);
|
|
4258
|
+
const conversation_history = items.map(formatConversationHistoryItem).join('\n\n');
|
|
4259
|
+
|
|
4260
|
+
return `You are working inside Xuda Studio.
|
|
4261
|
+
|
|
4262
|
+
Use the conversation history below as the main source of context for the current studio task.
|
|
4263
|
+
The last user message is the latest request that should be handled now.
|
|
4264
|
+
When plan_mode is true, create a plan only and do not save studio docs.
|
|
4265
|
+
When plan_mode is false, generate the studio docs and save them into the project.
|
|
4266
|
+
|
|
4267
|
+
Conversation history:
|
|
4268
|
+
${conversation_history || `User (studio): ${prompt}`}
|
|
4269
|
+
`;
|
|
4270
|
+
};
|
|
4271
|
+
|
|
4272
|
+
const get_studio_result_message = function (studio_result) {
|
|
4273
|
+
if (normalized_plan_mode) {
|
|
4274
|
+
if (typeof studio_result === 'string') {
|
|
4275
|
+
return studio_result;
|
|
4276
|
+
}
|
|
4277
|
+
|
|
4278
|
+
const approval_units = studio_result?.approval_units || [];
|
|
4279
|
+
const build_order = studio_result?.build_order || [];
|
|
4280
|
+
const acceptance_checks = studio_result?.acceptance_checks || [];
|
|
4281
|
+
|
|
4282
|
+
const lines = [`Studio plan ready.`, ``, `Thinking:`, `- Mapped the request into ${approval_units.length} planned Xuda unit${approval_units.length === 1 ? '' : 's'}.`, `- Kept this turn in planning mode, so no studio docs were saved.`];
|
|
4283
|
+
|
|
4284
|
+
if (studio_result?.overview) {
|
|
4285
|
+
lines.push('', 'Overview', studio_result.overview);
|
|
4286
|
+
}
|
|
4287
|
+
|
|
4288
|
+
if (approval_units.length) {
|
|
4289
|
+
lines.push('', 'Planned units');
|
|
4290
|
+
for (const unit of approval_units.slice(0, 12)) {
|
|
4291
|
+
lines.push(`- ${unit.unit_type}: ${unit.unit_name}${unit.folder_name ? ` (${unit.folder_name})` : ''}`);
|
|
4292
|
+
}
|
|
4293
|
+
}
|
|
4294
|
+
|
|
4295
|
+
if (build_order.length) {
|
|
4296
|
+
lines.push('', 'Build order');
|
|
4297
|
+
for (const step of build_order.slice(0, 8)) {
|
|
4298
|
+
lines.push(`- ${step.step}. ${step.block_ref}: ${step.deliverable}`);
|
|
4299
|
+
}
|
|
4300
|
+
}
|
|
4301
|
+
|
|
4302
|
+
if (acceptance_checks.length) {
|
|
4303
|
+
lines.push('', 'Acceptance checks');
|
|
4304
|
+
for (const check of acceptance_checks.slice(0, 8)) {
|
|
4305
|
+
lines.push(`- ${check}`);
|
|
4306
|
+
}
|
|
4307
|
+
}
|
|
4308
|
+
|
|
4309
|
+
return lines.join('\n');
|
|
4310
|
+
}
|
|
4311
|
+
|
|
4312
|
+
const docs = Array.isArray(studio_result?.docs) ? studio_result.docs.filter(Boolean) : _.castArray(studio_result).filter(Boolean);
|
|
4313
|
+
const grouped_docs = _.countBy(docs, (doc) => doc?.properties?.menuType || 'unknown');
|
|
4314
|
+
const grouped_docs_summary = Object.entries(grouped_docs)
|
|
4315
|
+
.map(([type, count]) => `${count} ${type}`)
|
|
4316
|
+
.join(', ');
|
|
4317
|
+
|
|
4318
|
+
const lines = [save_result_to_studio ? `Studio build completed and saved.` : `Studio build completed.`, ``, `Thinking:`, `- Turned the request into ${docs.length} studio doc${docs.length === 1 ? '' : 's'}.`];
|
|
4319
|
+
|
|
4320
|
+
if (grouped_docs_summary) {
|
|
4321
|
+
lines.push(`- Generated ${grouped_docs_summary}.`);
|
|
4322
|
+
}
|
|
4323
|
+
|
|
4324
|
+
lines.push(save_result_to_studio ? `- Saved the generated docs into the current studio project.` : `- Returned the generated docs without saving them.`);
|
|
4325
|
+
if (studio_result?.livePreviewTests && !studio_result.livePreviewTests.skipped) {
|
|
4326
|
+
lines.push(`- Live-preview browser smoke test passed for ${studio_result.livePreviewTests.tested || 0} component${studio_result.livePreviewTests.tested === 1 ? '' : 's'}.`);
|
|
4327
|
+
}
|
|
4328
|
+
|
|
4329
|
+
if (docs.length) {
|
|
4330
|
+
lines.push('', 'Created units');
|
|
4331
|
+
for (const doc of docs.slice(0, 12)) {
|
|
4332
|
+
const menu_type = doc?.properties?.menuType || 'doc';
|
|
4333
|
+
const menu_name = doc?.properties?.menuName || doc?.properties?.menuTitle || doc?._id || 'Unnamed';
|
|
4334
|
+
lines.push(`- ${menu_type}: ${menu_name}${doc?._id ? ` [${doc._id}]` : ''}`);
|
|
4335
|
+
}
|
|
4336
|
+
}
|
|
4337
|
+
|
|
4338
|
+
return lines.join('\n');
|
|
4339
|
+
};
|
|
4340
|
+
|
|
4341
|
+
let thinking_interval;
|
|
4342
|
+
let thinking_index = 0;
|
|
4343
|
+
const thinking_messages = normalized_plan_mode
|
|
4344
|
+
? ['Thinking through the request and mapping it into Xuda units...\n\n', 'Checking which folders, tables, globals, and runtime blocks are needed...\n\n', 'Drafting a studio floor plan for approval...\n\n']
|
|
4345
|
+
: ['Thinking through the request and deciding what should be created in Studio...\n\n', 'Checking which Xuda docs are required before building...\n\n', 'Preparing the app builder plugin to generate and save the result...\n\n'];
|
|
4346
|
+
|
|
4347
|
+
try {
|
|
4348
|
+
emitToDashboard('stream_start');
|
|
4349
|
+
emitToDashboard('stream_phase', normalized_plan_mode ? 'Planning studio request' : 'Building studio request');
|
|
4350
|
+
|
|
4351
|
+
const conversation_item_reference_id = await add_conversation_item(uid, profile_id, conversation_id, prompt, 'studio', undefined, { plan_mode: normalized_plan_mode });
|
|
4352
|
+
|
|
4353
|
+
let out_conversation_item_obj = {
|
|
4354
|
+
_id: prompt_conversation_item_id,
|
|
4355
|
+
stat: 3,
|
|
4356
|
+
docType: 'chat_conversation_item',
|
|
4357
|
+
uid,
|
|
4358
|
+
conversation_type: 'studio',
|
|
4359
|
+
type: 'studio',
|
|
4360
|
+
date_created_ts: Date.now(),
|
|
4361
|
+
ts: Date.now(),
|
|
4362
|
+
conversation_id,
|
|
4363
|
+
text: prompt,
|
|
4364
|
+
reference_id: conversation_doc.reference_id,
|
|
4365
|
+
conversation_item_reference_id,
|
|
4366
|
+
direction: 'out',
|
|
4367
|
+
role: 'user',
|
|
4368
|
+
read: { [uid]: Date.now() },
|
|
4369
|
+
rtl: _common.detectRTL(prompt),
|
|
4370
|
+
job_id,
|
|
4371
|
+
plan_mode: normalized_plan_mode,
|
|
4372
|
+
studio_method: plugin_method,
|
|
4373
|
+
target_app_id,
|
|
4374
|
+
};
|
|
4375
|
+
|
|
4376
|
+
const out_conversation_item_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, out_conversation_item_obj);
|
|
4377
|
+
if (out_conversation_item_save_ret.code < 0) throw new Error(out_conversation_item_save_ret.data);
|
|
4378
|
+
|
|
4379
|
+
if (attachments?.length) {
|
|
4380
|
+
emitToDashboard('stream_phase', 'Analyzing attachments', { update: true });
|
|
4381
|
+
await attachment_handler(uid, account_profile_info.app_id, conversation_id, attachments, account_profile_info);
|
|
4382
|
+
}
|
|
4383
|
+
|
|
4384
|
+
conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
4385
|
+
conversation_doc.ts = Date.now();
|
|
4386
|
+
conversation_doc.stat = 2;
|
|
4387
|
+
conversation_doc.job_id = job_id;
|
|
4388
|
+
conversation_doc.plan_mode = normalized_plan_mode;
|
|
4389
|
+
conversation_doc.target_app_id = target_app_id;
|
|
4390
|
+
await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
|
|
4391
|
+
|
|
4392
|
+
emitToDashboard('stream_phase', normalized_plan_mode ? 'Preparing studio plan' : 'Preparing studio build', { update: true });
|
|
4393
|
+
emitThinkingPhase(thinking_messages[thinking_index]);
|
|
4394
|
+
|
|
4395
|
+
if (thinking_messages.length > 1) {
|
|
4396
|
+
thinking_interval = setInterval(async () => {
|
|
4397
|
+
if (thinking_index >= thinking_messages.length - 1) {
|
|
4398
|
+
clearInterval(thinking_interval);
|
|
4399
|
+
thinking_interval = undefined;
|
|
4400
|
+
return;
|
|
4401
|
+
}
|
|
4402
|
+
|
|
4403
|
+
thinking_index += 1;
|
|
4404
|
+
try {
|
|
4405
|
+
if (job_id) {
|
|
4406
|
+
const job_info = await jobs_ms.get_job_info({ job_id });
|
|
4407
|
+
if (job_info.code < 0 || job_info.data.stat === 4) {
|
|
4408
|
+
return;
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4411
|
+
} catch (error) {}
|
|
4412
|
+
|
|
4413
|
+
emitThinkingPhase(thinking_messages[thinking_index]);
|
|
4414
|
+
|
|
4415
|
+
if (thinking_index >= thinking_messages.length - 1) {
|
|
4416
|
+
clearInterval(thinking_interval);
|
|
4417
|
+
thinking_interval = undefined;
|
|
4418
|
+
}
|
|
4419
|
+
}, 1800);
|
|
4420
|
+
}
|
|
4421
|
+
|
|
4422
|
+
const studio_prompt = await get_studio_context_prompt();
|
|
4423
|
+
const plugin_ret = await run_plugin(
|
|
4424
|
+
target_app_id,
|
|
4425
|
+
uid,
|
|
4426
|
+
plugin_name,
|
|
4427
|
+
plugin_method,
|
|
4428
|
+
{
|
|
4429
|
+
prompt: studio_prompt,
|
|
4430
|
+
save: save_result_to_studio,
|
|
4431
|
+
},
|
|
4432
|
+
true,
|
|
4433
|
+
{
|
|
4434
|
+
...req,
|
|
4435
|
+
prompt: studio_prompt,
|
|
4436
|
+
user_prompt: prompt,
|
|
4437
|
+
plan_mode: normalized_plan_mode,
|
|
4438
|
+
target_app_id,
|
|
4439
|
+
},
|
|
4440
|
+
true,
|
|
4441
|
+
{
|
|
4442
|
+
conversation_id,
|
|
4443
|
+
conversation_type: 'studio',
|
|
4444
|
+
plan_mode: normalized_plan_mode,
|
|
4445
|
+
headers,
|
|
4446
|
+
},
|
|
4447
|
+
);
|
|
4448
|
+
|
|
4449
|
+
clearInterval(thinking_interval);
|
|
4450
|
+
thinking_interval = undefined;
|
|
4451
|
+
|
|
4452
|
+
if (!plugin_ret || plugin_ret.code < 0) {
|
|
4453
|
+
throw new Error(get_error_message(plugin_ret?.data || plugin_ret, 'studio plugin failed'));
|
|
4454
|
+
}
|
|
4455
|
+
|
|
4456
|
+
const studio_result = plugin_ret.data;
|
|
4457
|
+
const response_text = get_studio_result_message(studio_result);
|
|
4458
|
+
|
|
4459
|
+
emitToDashboard('stream_phase', normalized_plan_mode ? 'Streaming studio plan' : 'Streaming studio result', { update: true });
|
|
4460
|
+
streamText(response_text);
|
|
4461
|
+
emitToDashboard('stream_end');
|
|
4462
|
+
|
|
4463
|
+
conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
4464
|
+
conversation_doc.ts = Date.now();
|
|
4465
|
+
conversation_doc.stat = 3;
|
|
4466
|
+
conversation_doc.process_stat = 'full';
|
|
4467
|
+
await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
|
|
4468
|
+
|
|
4469
|
+
const in_conversation_item_obj = {
|
|
4470
|
+
_id: response_conversation_item_id,
|
|
4471
|
+
stat: 3,
|
|
4472
|
+
docType: 'chat_conversation_item',
|
|
4473
|
+
uid,
|
|
4474
|
+
conversation_type: 'studio',
|
|
4475
|
+
type: 'studio',
|
|
4476
|
+
date_created_ts: Date.now(),
|
|
4477
|
+
ts: Date.now(),
|
|
4478
|
+
conversation_id,
|
|
4479
|
+
text: response_text,
|
|
4480
|
+
reference_id: conversation_doc.reference_id,
|
|
4481
|
+
direction: 'in',
|
|
4482
|
+
role: 'assistant',
|
|
4483
|
+
read: { [uid]: Date.now() },
|
|
4484
|
+
rtl: _common.detectRTL(response_text),
|
|
4485
|
+
job_id,
|
|
4486
|
+
plan_mode: normalized_plan_mode,
|
|
4487
|
+
studio_method: plugin_method,
|
|
4488
|
+
studio_result,
|
|
4489
|
+
target_app_id,
|
|
4490
|
+
prompt_conversation_item_id,
|
|
4491
|
+
};
|
|
4492
|
+
|
|
4493
|
+
return await db_module.save_app_couch_doc_native(account_profile_info.app_id, in_conversation_item_obj);
|
|
4494
|
+
} catch (err) {
|
|
4495
|
+
if (thinking_interval) {
|
|
4496
|
+
clearInterval(thinking_interval);
|
|
4497
|
+
}
|
|
4498
|
+
|
|
4499
|
+
conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
4500
|
+
conversation_doc.ts = Date.now();
|
|
4501
|
+
conversation_doc.stat = 3;
|
|
4502
|
+
await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
|
|
4503
|
+
|
|
4504
|
+
const error_message = get_error_message(err, 'studio request failed');
|
|
4505
|
+
emitToDashboard('stream_phase', 'Studio request failed', { update: true });
|
|
4506
|
+
streamText(`Studio request failed: ${error_message}`);
|
|
4507
|
+
emitToDashboard('stream_end');
|
|
4508
|
+
|
|
4509
|
+
return { code: -16, data: error_message };
|
|
4510
|
+
}
|
|
4511
|
+
};
|
|
4512
|
+
|
|
4130
4513
|
const get_ai_agent_tools = async function ({ ai_agent_doc, agent_id, reference_type, prompt_suggestion_activated, chat_suggestion_activated, gtp_token, uid, account_profile_info, job_id, headers, context, app_id }) {
|
|
4131
4514
|
let tools = [];
|
|
4132
4515
|
let tool_resources = {};
|
|
@@ -5162,7 +5545,7 @@ const add_conversation_item = async function (uid, profile_id, conversation_id,
|
|
|
5162
5545
|
} catch (err) {
|
|
5163
5546
|
throw err;
|
|
5164
5547
|
}
|
|
5165
|
-
if (contact_id) {
|
|
5548
|
+
if (conversation_doc.reference_type === 'contacts' && contact_id) {
|
|
5166
5549
|
try {
|
|
5167
5550
|
let contact_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, contact_id);
|
|
5168
5551
|
const item = await client.conversations.items.create(contact_doc.contact_reference_conversation_id, {
|
|
@@ -5466,18 +5849,10 @@ setTimeout(async () => {
|
|
|
5466
5849
|
|
|
5467
5850
|
const run_plugin = async function (app_id, uid, plugin_name, method, prop_data, dev = true, req = {}, testing = true, agent_info = {}) {
|
|
5468
5851
|
const db_module = await import(`${module_path}/db_module/index.mjs`);
|
|
5469
|
-
debugger;
|
|
5470
|
-
const get_path = function (resource) {
|
|
5471
|
-
if (dev) {
|
|
5472
|
-
return `../../plugins/${plugin_name}/${resource}`;
|
|
5473
|
-
}
|
|
5474
|
-
|
|
5475
|
-
return `${_conf.plugins_drive_path}/${app_id}/node_modules/${plugin_name}/${resource}`;
|
|
5476
|
-
};
|
|
5477
5852
|
const get_plugin_resource = function (plugin_name, plugin_resource) {
|
|
5478
5853
|
return new Promise(async (resolve, reject) => {
|
|
5479
5854
|
try {
|
|
5480
|
-
const plugin_resource_res = await import(
|
|
5855
|
+
const plugin_resource_res = await import(get_plugin_import_specifier(app_id, plugin_name, plugin_resource, dev));
|
|
5481
5856
|
resolve(plugin_resource_res);
|
|
5482
5857
|
} catch (err) {
|
|
5483
5858
|
console.error(err);
|
|
@@ -5560,12 +5935,11 @@ const run_plugin = async function (app_id, uid, plugin_name, method, prop_data,
|
|
|
5560
5935
|
};
|
|
5561
5936
|
|
|
5562
5937
|
const reject_local = function (e) {
|
|
5563
|
-
reject(
|
|
5564
|
-
// reject(e.message);
|
|
5938
|
+
reject(new Error(get_error_message(e, 'plugin execution failed')));
|
|
5565
5939
|
};
|
|
5566
5940
|
|
|
5567
5941
|
try {
|
|
5568
|
-
const script = new VMScript(`
|
|
5942
|
+
const script = new VMScript(`Promise.resolve().then(() => server_script.${method}(req,params,setup_doc,resolve,reject,env)).catch(reject);`, { filename: app_plugins_path, dirname: app_plugins_path });
|
|
5569
5943
|
let vm = new NodeVM({
|
|
5570
5944
|
require: {
|
|
5571
5945
|
external: true,
|
|
@@ -5579,6 +5953,7 @@ const run_plugin = async function (app_id, uid, plugin_name, method, prop_data,
|
|
|
5579
5953
|
});
|
|
5580
5954
|
} catch (err) {
|
|
5581
5955
|
console.error('Failed to execute script.', err);
|
|
5956
|
+
reject_local(err);
|
|
5582
5957
|
}
|
|
5583
5958
|
// };
|
|
5584
5959
|
});
|
|
@@ -5604,17 +5979,10 @@ const get_plugin_tool = async function (app_id, uid, plugin_name, method, prop_d
|
|
|
5604
5979
|
|
|
5605
5980
|
init(undefined, undefined, _, true, undefined, undefined, undefined, undefined, undefined, true, z);
|
|
5606
5981
|
|
|
5607
|
-
const get_path = function (resource) {
|
|
5608
|
-
if (dev) {
|
|
5609
|
-
return `../../plugins/${plugin_name}/${resource}`;
|
|
5610
|
-
}
|
|
5611
|
-
|
|
5612
|
-
return `${_conf.plugins_drive_path}/${app_id}/node_modules/${plugin_name}/${resource}`;
|
|
5613
|
-
};
|
|
5614
5982
|
const get_plugin_resource = function (plugin_name, plugin_resource) {
|
|
5615
5983
|
return new Promise(async (resolve, reject) => {
|
|
5616
5984
|
try {
|
|
5617
|
-
const plugin_resource_res = await import(
|
|
5985
|
+
const plugin_resource_res = await import(get_plugin_import_specifier(app_id, plugin_name, plugin_resource, dev));
|
|
5618
5986
|
resolve(plugin_resource_res);
|
|
5619
5987
|
} catch (err) {
|
|
5620
5988
|
console.error(err);
|
|
@@ -5692,8 +6060,7 @@ const get_plugin_tool = async function (app_id, uid, plugin_name, method, prop_d
|
|
|
5692
6060
|
};
|
|
5693
6061
|
|
|
5694
6062
|
const reject_local = function (e) {
|
|
5695
|
-
reject(
|
|
5696
|
-
// reject(e.message);
|
|
6063
|
+
reject(new Error(get_error_message(e, 'plugin execution failed')));
|
|
5697
6064
|
};
|
|
5698
6065
|
|
|
5699
6066
|
try {
|
|
@@ -5814,6 +6181,7 @@ const create_and_upload_image_to_drive = async function (drive_type, file_path,
|
|
|
5814
6181
|
};
|
|
5815
6182
|
|
|
5816
6183
|
export const create_avatar = async function (req, job_id, headers) {
|
|
6184
|
+
debugger;
|
|
5817
6185
|
const { docType, uid, url, _id, name, account_type } = req;
|
|
5818
6186
|
let { metadata } = req;
|
|
5819
6187
|
try {
|