@xuda.io/ai_module 1.1.4026 → 1.1.4028
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 +150 -148
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -4384,6 +4384,8 @@ const chat_note = async function (req, job_id, headers) {
|
|
|
4384
4384
|
const sender_app_id = await account_module.get_account_default_project_id(uid);
|
|
4385
4385
|
let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
|
|
4386
4386
|
|
|
4387
|
+
await attachment_handler(uid, sender_app_id, conversation_id, attachments);
|
|
4388
|
+
|
|
4387
4389
|
const item = await client.conversations.items.create(conversation_id, {
|
|
4388
4390
|
items: [
|
|
4389
4391
|
{
|
|
@@ -4953,153 +4955,6 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4953
4955
|
return list;
|
|
4954
4956
|
};
|
|
4955
4957
|
|
|
4956
|
-
const attachment_handler = async function () {
|
|
4957
|
-
if (!attachments?.length) return;
|
|
4958
|
-
debugger;
|
|
4959
|
-
const get_transcript = async function (filename) {
|
|
4960
|
-
async function streamToBuffer(fileStream) {
|
|
4961
|
-
const chunks = [];
|
|
4962
|
-
for await (const chunk of fileStream) {
|
|
4963
|
-
chunks.push(chunk);
|
|
4964
|
-
}
|
|
4965
|
-
return Buffer.concat(chunks);
|
|
4966
|
-
}
|
|
4967
|
-
|
|
4968
|
-
const validation_ret = file_upload_validator(filename);
|
|
4969
|
-
if (!validation_ret.valid) {
|
|
4970
|
-
throw new Error(validation_ret.error);
|
|
4971
|
-
}
|
|
4972
|
-
|
|
4973
|
-
const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
|
|
4974
|
-
|
|
4975
|
-
let transcript;
|
|
4976
|
-
switch (validation_ret.category) {
|
|
4977
|
-
case 'audio':
|
|
4978
|
-
case 'video': {
|
|
4979
|
-
transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category });
|
|
4980
|
-
|
|
4981
|
-
break;
|
|
4982
|
-
}
|
|
4983
|
-
|
|
4984
|
-
case 'image': {
|
|
4985
|
-
const fileBuffer = await streamToBuffer(fileStream);
|
|
4986
|
-
const base64 = fileBuffer.toString('base64');
|
|
4987
|
-
|
|
4988
|
-
const ret = await submit_chat_gpt_prompt({
|
|
4989
|
-
uid,
|
|
4990
|
-
prompt: [
|
|
4991
|
-
{
|
|
4992
|
-
role: 'user',
|
|
4993
|
-
content: [
|
|
4994
|
-
{
|
|
4995
|
-
type: 'input_text',
|
|
4996
|
-
text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
|
|
4997
|
-
},
|
|
4998
|
-
{
|
|
4999
|
-
type: 'input_image',
|
|
5000
|
-
image_url: `data:${validation_ret.mime};base64,${base64}`,
|
|
5001
|
-
},
|
|
5002
|
-
],
|
|
5003
|
-
},
|
|
5004
|
-
],
|
|
5005
|
-
metadata: { conversation_id, func: 'attachment_handler' },
|
|
5006
|
-
});
|
|
5007
|
-
if (ret.code < 0) {
|
|
5008
|
-
throw new Error('something went wrong try again later');
|
|
5009
|
-
}
|
|
5010
|
-
transcript = ret.data;
|
|
5011
|
-
|
|
5012
|
-
break;
|
|
5013
|
-
}
|
|
5014
|
-
|
|
5015
|
-
case 'document':
|
|
5016
|
-
case 'text': {
|
|
5017
|
-
async function transcript_file(fileBuffer, fileName, mimeType) {
|
|
5018
|
-
try {
|
|
5019
|
-
const base64 = fileBuffer.toString('base64');
|
|
5020
|
-
|
|
5021
|
-
const ret = await submit_chat_gpt_prompt({
|
|
5022
|
-
uid,
|
|
5023
|
-
prompt: [
|
|
5024
|
-
{
|
|
5025
|
-
role: 'user',
|
|
5026
|
-
content: [
|
|
5027
|
-
{
|
|
5028
|
-
type: 'input_text',
|
|
5029
|
-
text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
|
|
5030
|
-
},
|
|
5031
|
-
{
|
|
5032
|
-
type: 'input_file',
|
|
5033
|
-
filename: fileName || 'document.pdf',
|
|
5034
|
-
|
|
5035
|
-
file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
|
|
5036
|
-
},
|
|
5037
|
-
],
|
|
5038
|
-
},
|
|
5039
|
-
],
|
|
5040
|
-
metadata: { conversation_id, func: 'attachment_handler' },
|
|
5041
|
-
});
|
|
5042
|
-
|
|
5043
|
-
return ret;
|
|
5044
|
-
} catch (error) {
|
|
5045
|
-
console.error('Error transcribing PDF:', error);
|
|
5046
|
-
throw error;
|
|
5047
|
-
}
|
|
5048
|
-
}
|
|
5049
|
-
const buffer = await streamToBuffer(fileStream);
|
|
5050
|
-
transcript = await transcript_file(buffer, filename, validation_ret.mime);
|
|
5051
|
-
|
|
5052
|
-
break;
|
|
5053
|
-
}
|
|
5054
|
-
|
|
5055
|
-
default:
|
|
5056
|
-
break;
|
|
5057
|
-
}
|
|
5058
|
-
|
|
5059
|
-
return transcript;
|
|
5060
|
-
};
|
|
5061
|
-
|
|
5062
|
-
let file_names = [];
|
|
5063
|
-
let system_message_items = [];
|
|
5064
|
-
for (const file of attachments) {
|
|
5065
|
-
file_names.push(file.filename);
|
|
5066
|
-
const transcript = await get_transcript(file.filename);
|
|
5067
|
-
|
|
5068
|
-
// system_message_items.push({
|
|
5069
|
-
// type: 'message',
|
|
5070
|
-
// role: 'system',
|
|
5071
|
-
// content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
5072
|
-
// });
|
|
5073
|
-
|
|
5074
|
-
// prompt += await generate_XU_markdown('attachment', file); //`[]`;
|
|
5075
|
-
|
|
5076
|
-
const items = await client.conversations.items.create(conversation_id, {
|
|
5077
|
-
items: [
|
|
5078
|
-
{
|
|
5079
|
-
type: 'message',
|
|
5080
|
-
role: 'system',
|
|
5081
|
-
content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
5082
|
-
},
|
|
5083
|
-
],
|
|
5084
|
-
});
|
|
5085
|
-
system_message_items.push({ id: items.last_id, ts: Date.now(), conversation_type: 'attachment', file });
|
|
5086
|
-
}
|
|
5087
|
-
|
|
5088
|
-
// const items = await client.conversations.items.create(conversation_id, {
|
|
5089
|
-
// items: system_message_items,
|
|
5090
|
-
// });
|
|
5091
|
-
|
|
5092
|
-
// console.log(items.data);
|
|
5093
|
-
|
|
5094
|
-
conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
|
|
5095
|
-
|
|
5096
|
-
conversation_doc.ts = Date.now();
|
|
5097
|
-
conversation_doc.stat = 2;
|
|
5098
|
-
conversation_doc.messages = [...conversation_doc.messages, ...system_message_items];
|
|
5099
|
-
// s.push(obj);
|
|
5100
|
-
await db_module.save_app_couch_doc_native(app_id, conversation_doc);
|
|
5101
|
-
};
|
|
5102
|
-
|
|
5103
4958
|
const set_ts_to_agent = async function () {
|
|
5104
4959
|
try {
|
|
5105
4960
|
let agent_doc = await db_module.get_app_couch_doc_native(app_id, reference_id);
|
|
@@ -5118,7 +4973,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
5118
4973
|
/////////////
|
|
5119
4974
|
if (attachments?.length) {
|
|
5120
4975
|
await update_job('analyzing attachments');
|
|
5121
|
-
await attachment_handler();
|
|
4976
|
+
await attachment_handler(uid, app_id, conversation_id, attachments);
|
|
5122
4977
|
}
|
|
5123
4978
|
////////////
|
|
5124
4979
|
|
|
@@ -6438,3 +6293,150 @@ export const unpin_ai_agent = async function (req) {
|
|
|
6438
6293
|
};
|
|
6439
6294
|
}
|
|
6440
6295
|
};
|
|
6296
|
+
|
|
6297
|
+
const attachment_handler = async function (uid, app_id, conversation_id, attachments) {
|
|
6298
|
+
if (!attachments?.length) return;
|
|
6299
|
+
debugger;
|
|
6300
|
+
const get_transcript = async function (filename) {
|
|
6301
|
+
async function streamToBuffer(fileStream) {
|
|
6302
|
+
const chunks = [];
|
|
6303
|
+
for await (const chunk of fileStream) {
|
|
6304
|
+
chunks.push(chunk);
|
|
6305
|
+
}
|
|
6306
|
+
return Buffer.concat(chunks);
|
|
6307
|
+
}
|
|
6308
|
+
|
|
6309
|
+
const validation_ret = file_upload_validator(filename);
|
|
6310
|
+
if (!validation_ret.valid) {
|
|
6311
|
+
throw new Error(validation_ret.error);
|
|
6312
|
+
}
|
|
6313
|
+
|
|
6314
|
+
const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
|
|
6315
|
+
|
|
6316
|
+
let transcript;
|
|
6317
|
+
switch (validation_ret.category) {
|
|
6318
|
+
case 'audio':
|
|
6319
|
+
case 'video': {
|
|
6320
|
+
transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category });
|
|
6321
|
+
|
|
6322
|
+
break;
|
|
6323
|
+
}
|
|
6324
|
+
|
|
6325
|
+
case 'image': {
|
|
6326
|
+
const fileBuffer = await streamToBuffer(fileStream);
|
|
6327
|
+
const base64 = fileBuffer.toString('base64');
|
|
6328
|
+
|
|
6329
|
+
const ret = await submit_chat_gpt_prompt({
|
|
6330
|
+
uid,
|
|
6331
|
+
prompt: [
|
|
6332
|
+
{
|
|
6333
|
+
role: 'user',
|
|
6334
|
+
content: [
|
|
6335
|
+
{
|
|
6336
|
+
type: 'input_text',
|
|
6337
|
+
text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
|
|
6338
|
+
},
|
|
6339
|
+
{
|
|
6340
|
+
type: 'input_image',
|
|
6341
|
+
image_url: `data:${validation_ret.mime};base64,${base64}`,
|
|
6342
|
+
},
|
|
6343
|
+
],
|
|
6344
|
+
},
|
|
6345
|
+
],
|
|
6346
|
+
metadata: { conversation_id, func: 'attachment_handler' },
|
|
6347
|
+
});
|
|
6348
|
+
if (ret.code < 0) {
|
|
6349
|
+
throw new Error('something went wrong try again later');
|
|
6350
|
+
}
|
|
6351
|
+
transcript = ret.data;
|
|
6352
|
+
|
|
6353
|
+
break;
|
|
6354
|
+
}
|
|
6355
|
+
|
|
6356
|
+
case 'document':
|
|
6357
|
+
case 'text': {
|
|
6358
|
+
async function transcript_file(fileBuffer, fileName, mimeType) {
|
|
6359
|
+
try {
|
|
6360
|
+
const base64 = fileBuffer.toString('base64');
|
|
6361
|
+
|
|
6362
|
+
const ret = await submit_chat_gpt_prompt({
|
|
6363
|
+
uid,
|
|
6364
|
+
prompt: [
|
|
6365
|
+
{
|
|
6366
|
+
role: 'user',
|
|
6367
|
+
content: [
|
|
6368
|
+
{
|
|
6369
|
+
type: 'input_text',
|
|
6370
|
+
text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
|
|
6371
|
+
},
|
|
6372
|
+
{
|
|
6373
|
+
type: 'input_file',
|
|
6374
|
+
filename: fileName || 'document.pdf',
|
|
6375
|
+
|
|
6376
|
+
file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
|
|
6377
|
+
},
|
|
6378
|
+
],
|
|
6379
|
+
},
|
|
6380
|
+
],
|
|
6381
|
+
metadata: { conversation_id, func: 'attachment_handler' },
|
|
6382
|
+
});
|
|
6383
|
+
|
|
6384
|
+
return ret;
|
|
6385
|
+
} catch (error) {
|
|
6386
|
+
console.error('Error transcribing PDF:', error);
|
|
6387
|
+
throw error;
|
|
6388
|
+
}
|
|
6389
|
+
}
|
|
6390
|
+
const buffer = await streamToBuffer(fileStream);
|
|
6391
|
+
transcript = await transcript_file(buffer, filename, validation_ret.mime);
|
|
6392
|
+
|
|
6393
|
+
break;
|
|
6394
|
+
}
|
|
6395
|
+
|
|
6396
|
+
default:
|
|
6397
|
+
break;
|
|
6398
|
+
}
|
|
6399
|
+
|
|
6400
|
+
return transcript;
|
|
6401
|
+
};
|
|
6402
|
+
|
|
6403
|
+
let file_names = [];
|
|
6404
|
+
let system_message_items = [];
|
|
6405
|
+
for (const file of attachments) {
|
|
6406
|
+
file_names.push(file.filename);
|
|
6407
|
+
const transcript = await get_transcript(file.filename);
|
|
6408
|
+
|
|
6409
|
+
// system_message_items.push({
|
|
6410
|
+
// type: 'message',
|
|
6411
|
+
// role: 'system',
|
|
6412
|
+
// content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
6413
|
+
// });
|
|
6414
|
+
|
|
6415
|
+
// prompt += await generate_XU_markdown('attachment', file); //`[]`;
|
|
6416
|
+
|
|
6417
|
+
const items = await client.conversations.items.create(conversation_id, {
|
|
6418
|
+
items: [
|
|
6419
|
+
{
|
|
6420
|
+
type: 'message',
|
|
6421
|
+
role: 'system',
|
|
6422
|
+
content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
6423
|
+
},
|
|
6424
|
+
],
|
|
6425
|
+
});
|
|
6426
|
+
system_message_items.push({ id: items.last_id, ts: Date.now(), conversation_type: 'attachment', file });
|
|
6427
|
+
}
|
|
6428
|
+
|
|
6429
|
+
// const items = await client.conversations.items.create(conversation_id, {
|
|
6430
|
+
// items: system_message_items,
|
|
6431
|
+
// });
|
|
6432
|
+
|
|
6433
|
+
// console.log(items.data);
|
|
6434
|
+
|
|
6435
|
+
let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
|
|
6436
|
+
|
|
6437
|
+
conversation_doc.ts = Date.now();
|
|
6438
|
+
conversation_doc.stat = 2;
|
|
6439
|
+
conversation_doc.messages = [...conversation_doc.messages, ...system_message_items];
|
|
6440
|
+
// s.push(obj);
|
|
6441
|
+
await db_module.save_app_couch_doc_native(app_id, conversation_doc);
|
|
6442
|
+
};
|