@xuda.io/ai_module 1.1.4026 → 1.1.4027
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 +148 -148
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -4953,153 +4953,6 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4953
4953
|
return list;
|
|
4954
4954
|
};
|
|
4955
4955
|
|
|
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
4956
|
const set_ts_to_agent = async function () {
|
|
5104
4957
|
try {
|
|
5105
4958
|
let agent_doc = await db_module.get_app_couch_doc_native(app_id, reference_id);
|
|
@@ -5118,7 +4971,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
5118
4971
|
/////////////
|
|
5119
4972
|
if (attachments?.length) {
|
|
5120
4973
|
await update_job('analyzing attachments');
|
|
5121
|
-
await attachment_handler();
|
|
4974
|
+
await attachment_handler(uid, app_id, conversation_id, attachments);
|
|
5122
4975
|
}
|
|
5123
4976
|
////////////
|
|
5124
4977
|
|
|
@@ -6438,3 +6291,150 @@ export const unpin_ai_agent = async function (req) {
|
|
|
6438
6291
|
};
|
|
6439
6292
|
}
|
|
6440
6293
|
};
|
|
6294
|
+
|
|
6295
|
+
const attachment_handler = async function (uid, app_id, conversation_id, attachments) {
|
|
6296
|
+
if (!attachments?.length) return;
|
|
6297
|
+
debugger;
|
|
6298
|
+
const get_transcript = async function (filename) {
|
|
6299
|
+
async function streamToBuffer(fileStream) {
|
|
6300
|
+
const chunks = [];
|
|
6301
|
+
for await (const chunk of fileStream) {
|
|
6302
|
+
chunks.push(chunk);
|
|
6303
|
+
}
|
|
6304
|
+
return Buffer.concat(chunks);
|
|
6305
|
+
}
|
|
6306
|
+
|
|
6307
|
+
const validation_ret = file_upload_validator(filename);
|
|
6308
|
+
if (!validation_ret.valid) {
|
|
6309
|
+
throw new Error(validation_ret.error);
|
|
6310
|
+
}
|
|
6311
|
+
|
|
6312
|
+
const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
|
|
6313
|
+
|
|
6314
|
+
let transcript;
|
|
6315
|
+
switch (validation_ret.category) {
|
|
6316
|
+
case 'audio':
|
|
6317
|
+
case 'video': {
|
|
6318
|
+
transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category });
|
|
6319
|
+
|
|
6320
|
+
break;
|
|
6321
|
+
}
|
|
6322
|
+
|
|
6323
|
+
case 'image': {
|
|
6324
|
+
const fileBuffer = await streamToBuffer(fileStream);
|
|
6325
|
+
const base64 = fileBuffer.toString('base64');
|
|
6326
|
+
|
|
6327
|
+
const ret = await submit_chat_gpt_prompt({
|
|
6328
|
+
uid,
|
|
6329
|
+
prompt: [
|
|
6330
|
+
{
|
|
6331
|
+
role: 'user',
|
|
6332
|
+
content: [
|
|
6333
|
+
{
|
|
6334
|
+
type: 'input_text',
|
|
6335
|
+
text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
|
|
6336
|
+
},
|
|
6337
|
+
{
|
|
6338
|
+
type: 'input_image',
|
|
6339
|
+
image_url: `data:${validation_ret.mime};base64,${base64}`,
|
|
6340
|
+
},
|
|
6341
|
+
],
|
|
6342
|
+
},
|
|
6343
|
+
],
|
|
6344
|
+
metadata: { conversation_id, func: 'attachment_handler' },
|
|
6345
|
+
});
|
|
6346
|
+
if (ret.code < 0) {
|
|
6347
|
+
throw new Error('something went wrong try again later');
|
|
6348
|
+
}
|
|
6349
|
+
transcript = ret.data;
|
|
6350
|
+
|
|
6351
|
+
break;
|
|
6352
|
+
}
|
|
6353
|
+
|
|
6354
|
+
case 'document':
|
|
6355
|
+
case 'text': {
|
|
6356
|
+
async function transcript_file(fileBuffer, fileName, mimeType) {
|
|
6357
|
+
try {
|
|
6358
|
+
const base64 = fileBuffer.toString('base64');
|
|
6359
|
+
|
|
6360
|
+
const ret = await submit_chat_gpt_prompt({
|
|
6361
|
+
uid,
|
|
6362
|
+
prompt: [
|
|
6363
|
+
{
|
|
6364
|
+
role: 'user',
|
|
6365
|
+
content: [
|
|
6366
|
+
{
|
|
6367
|
+
type: 'input_text',
|
|
6368
|
+
text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
|
|
6369
|
+
},
|
|
6370
|
+
{
|
|
6371
|
+
type: 'input_file',
|
|
6372
|
+
filename: fileName || 'document.pdf',
|
|
6373
|
+
|
|
6374
|
+
file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
|
|
6375
|
+
},
|
|
6376
|
+
],
|
|
6377
|
+
},
|
|
6378
|
+
],
|
|
6379
|
+
metadata: { conversation_id, func: 'attachment_handler' },
|
|
6380
|
+
});
|
|
6381
|
+
|
|
6382
|
+
return ret;
|
|
6383
|
+
} catch (error) {
|
|
6384
|
+
console.error('Error transcribing PDF:', error);
|
|
6385
|
+
throw error;
|
|
6386
|
+
}
|
|
6387
|
+
}
|
|
6388
|
+
const buffer = await streamToBuffer(fileStream);
|
|
6389
|
+
transcript = await transcript_file(buffer, filename, validation_ret.mime);
|
|
6390
|
+
|
|
6391
|
+
break;
|
|
6392
|
+
}
|
|
6393
|
+
|
|
6394
|
+
default:
|
|
6395
|
+
break;
|
|
6396
|
+
}
|
|
6397
|
+
|
|
6398
|
+
return transcript;
|
|
6399
|
+
};
|
|
6400
|
+
|
|
6401
|
+
let file_names = [];
|
|
6402
|
+
let system_message_items = [];
|
|
6403
|
+
for (const file of attachments) {
|
|
6404
|
+
file_names.push(file.filename);
|
|
6405
|
+
const transcript = await get_transcript(file.filename);
|
|
6406
|
+
|
|
6407
|
+
// system_message_items.push({
|
|
6408
|
+
// type: 'message',
|
|
6409
|
+
// role: 'system',
|
|
6410
|
+
// content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
6411
|
+
// });
|
|
6412
|
+
|
|
6413
|
+
// prompt += await generate_XU_markdown('attachment', file); //`[]`;
|
|
6414
|
+
|
|
6415
|
+
const items = await client.conversations.items.create(conversation_id, {
|
|
6416
|
+
items: [
|
|
6417
|
+
{
|
|
6418
|
+
type: 'message',
|
|
6419
|
+
role: 'system',
|
|
6420
|
+
content: [{ type: 'input_text', text: `the user attached file name: ${file.filename} with content: ${transcript.data || transcript}` }],
|
|
6421
|
+
},
|
|
6422
|
+
],
|
|
6423
|
+
});
|
|
6424
|
+
system_message_items.push({ id: items.last_id, ts: Date.now(), conversation_type: 'attachment', file });
|
|
6425
|
+
}
|
|
6426
|
+
|
|
6427
|
+
// const items = await client.conversations.items.create(conversation_id, {
|
|
6428
|
+
// items: system_message_items,
|
|
6429
|
+
// });
|
|
6430
|
+
|
|
6431
|
+
// console.log(items.data);
|
|
6432
|
+
|
|
6433
|
+
let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
|
|
6434
|
+
|
|
6435
|
+
conversation_doc.ts = Date.now();
|
|
6436
|
+
conversation_doc.stat = 2;
|
|
6437
|
+
conversation_doc.messages = [...conversation_doc.messages, ...system_message_items];
|
|
6438
|
+
// s.push(obj);
|
|
6439
|
+
await db_module.save_app_couch_doc_native(app_id, conversation_doc);
|
|
6440
|
+
};
|