@xuda.io/ai_module 1.1.4872 → 1.1.4873

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 +196 -91
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6078,114 +6078,219 @@ export const unpin_ai_agent = async function (req, job_id, headers) {
6078
6078
  }
6079
6079
  };
6080
6080
 
6081
- const attachment_handler = async function (uid, app_id, conversation_id, attachments, account_profile_info, date_created_ts) {
6082
- if (!attachments?.length) return;
6083
- let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
6084
-
6085
- const get_transcript = async function (filename) {
6086
- async function streamToBuffer(fileStream) {
6087
- const chunks = [];
6088
- for await (const chunk of fileStream) {
6089
- chunks.push(chunk);
6090
- }
6091
- return Buffer.concat(chunks);
6092
- }
6093
-
6094
- const validation_ret = drive_module.file_upload_validator(filename);
6095
- if (!validation_ret.valid) {
6096
- throw new Error(validation_ret.error);
6081
+ const get_transcript = async function (uid, app_id, filename) {
6082
+ async function streamToBuffer(fileStream) {
6083
+ const chunks = [];
6084
+ for await (const chunk of fileStream) {
6085
+ chunks.push(chunk);
6097
6086
  }
6087
+ return Buffer.concat(chunks);
6088
+ }
6098
6089
 
6099
- const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
6090
+ const validation_ret = drive_module.file_upload_validator(filename);
6091
+ if (!validation_ret.valid) {
6092
+ throw new Error(validation_ret.error);
6093
+ }
6100
6094
 
6101
- let transcript;
6102
- switch (validation_ret.category) {
6103
- case 'audio':
6104
- case 'video': {
6105
- transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category }, account_profile_info);
6095
+ const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
6106
6096
 
6107
- break;
6108
- }
6097
+ let transcript;
6098
+ switch (validation_ret.category) {
6099
+ case 'audio':
6100
+ case 'video': {
6101
+ transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category }, account_profile_info);
6109
6102
 
6110
- case 'image': {
6111
- const fileBuffer = await streamToBuffer(fileStream);
6112
- const base64 = fileBuffer.toString('base64');
6103
+ break;
6104
+ }
6113
6105
 
6114
- const ret = await submit_chat_gpt_prompt({
6115
- uid,
6116
- prompt: [
6117
- {
6118
- role: 'user',
6119
- content: [
6120
- {
6121
- type: 'input_text',
6122
- text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
6123
- },
6124
- {
6125
- type: 'input_image',
6126
- image_url: `data:${validation_ret.mime};base64,${base64}`,
6127
- },
6128
- ],
6129
- },
6130
- ],
6131
- metadata: { conversation_id, func: 'attachment_handler' },
6132
- account_profile_info,
6133
- });
6134
- if (ret.code < 0) {
6135
- throw new Error('something went wrong try again later');
6136
- }
6137
- transcript = ret.data;
6106
+ case 'image': {
6107
+ const fileBuffer = await streamToBuffer(fileStream);
6108
+ const base64 = fileBuffer.toString('base64');
6138
6109
 
6139
- break;
6110
+ const ret = await submit_chat_gpt_prompt({
6111
+ uid,
6112
+ prompt: [
6113
+ {
6114
+ role: 'user',
6115
+ content: [
6116
+ {
6117
+ type: 'input_text',
6118
+ text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
6119
+ },
6120
+ {
6121
+ type: 'input_image',
6122
+ image_url: `data:${validation_ret.mime};base64,${base64}`,
6123
+ },
6124
+ ],
6125
+ },
6126
+ ],
6127
+ metadata: { conversation_id, func: 'attachment_handler' },
6128
+ account_profile_info,
6129
+ });
6130
+ if (ret.code < 0) {
6131
+ throw new Error('something went wrong try again later');
6140
6132
  }
6133
+ transcript = ret.data;
6141
6134
 
6142
- case 'document':
6143
- case 'text': {
6144
- async function transcript_file(fileBuffer, fileName, mimeType) {
6145
- try {
6146
- const base64 = fileBuffer.toString('base64');
6135
+ break;
6136
+ }
6147
6137
 
6148
- const ret = await submit_chat_gpt_prompt({
6149
- uid,
6150
- prompt: [
6151
- {
6152
- role: 'user',
6153
- content: [
6154
- {
6155
- type: 'input_text',
6156
- text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
6157
- },
6158
- {
6159
- type: 'input_file',
6160
- filename: fileName || 'document.pdf',
6138
+ case 'document':
6139
+ case 'text': {
6140
+ async function transcript_file(fileBuffer, fileName, mimeType) {
6141
+ try {
6142
+ const base64 = fileBuffer.toString('base64');
6161
6143
 
6162
- file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
6163
- },
6164
- ],
6165
- },
6166
- ],
6167
- metadata: { conversation_id, func: 'attachment_handler' },
6168
- account_profile_info,
6169
- });
6144
+ const ret = await submit_chat_gpt_prompt({
6145
+ uid,
6146
+ prompt: [
6147
+ {
6148
+ role: 'user',
6149
+ content: [
6150
+ {
6151
+ type: 'input_text',
6152
+ text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
6153
+ },
6154
+ {
6155
+ type: 'input_file',
6156
+ filename: fileName || 'document.pdf',
6170
6157
 
6171
- return ret;
6172
- } catch (error) {
6173
- console.error('Error transcribing PDF:', error);
6174
- throw error;
6175
- }
6176
- }
6177
- const buffer = await streamToBuffer(fileStream);
6178
- transcript = await transcript_file(buffer, filename, validation_ret.mime);
6158
+ file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
6159
+ },
6160
+ ],
6161
+ },
6162
+ ],
6163
+ metadata: { conversation_id, func: 'attachment_handler' },
6164
+ account_profile_info,
6165
+ });
6179
6166
 
6180
- break;
6167
+ return ret;
6168
+ } catch (error) {
6169
+ console.error('Error transcribing PDF:', error);
6170
+ throw error;
6171
+ }
6181
6172
  }
6173
+ const buffer = await streamToBuffer(fileStream);
6174
+ transcript = await transcript_file(buffer, filename, validation_ret.mime);
6182
6175
 
6183
- default:
6184
- break;
6176
+ break;
6185
6177
  }
6186
6178
 
6187
- return transcript;
6188
- };
6179
+ default:
6180
+ break;
6181
+ }
6182
+
6183
+ return transcript;
6184
+ };
6185
+
6186
+ const attachment_handler = async function (uid, app_id, conversation_id, attachments, account_profile_info, date_created_ts) {
6187
+ if (!attachments?.length) return;
6188
+ let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
6189
+
6190
+ // const get_transcript = async function (filename) {
6191
+ // async function streamToBuffer(fileStream) {
6192
+ // const chunks = [];
6193
+ // for await (const chunk of fileStream) {
6194
+ // chunks.push(chunk);
6195
+ // }
6196
+ // return Buffer.concat(chunks);
6197
+ // }
6198
+
6199
+ // const validation_ret = drive_module.file_upload_validator(filename);
6200
+ // if (!validation_ret.valid) {
6201
+ // throw new Error(validation_ret.error);
6202
+ // }
6203
+
6204
+ // const fileStream = await get_drive_file_stream('user', app_id, uid, path.join('/', 'Chat Attachments', filename));
6205
+
6206
+ // let transcript;
6207
+ // switch (validation_ret.category) {
6208
+ // case 'audio':
6209
+ // case 'video': {
6210
+ // transcript = await transcribe(uid, fileStream, { conversation_id, func: 'attachment_handler - ' + validation_ret.category }, account_profile_info);
6211
+
6212
+ // break;
6213
+ // }
6214
+
6215
+ // case 'image': {
6216
+ // const fileBuffer = await streamToBuffer(fileStream);
6217
+ // const base64 = fileBuffer.toString('base64');
6218
+
6219
+ // const ret = await submit_chat_gpt_prompt({
6220
+ // uid,
6221
+ // prompt: [
6222
+ // {
6223
+ // role: 'user',
6224
+ // content: [
6225
+ // {
6226
+ // type: 'input_text',
6227
+ // text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
6228
+ // },
6229
+ // {
6230
+ // type: 'input_image',
6231
+ // image_url: `data:${validation_ret.mime};base64,${base64}`,
6232
+ // },
6233
+ // ],
6234
+ // },
6235
+ // ],
6236
+ // metadata: { conversation_id, func: 'attachment_handler' },
6237
+ // account_profile_info,
6238
+ // });
6239
+ // if (ret.code < 0) {
6240
+ // throw new Error('something went wrong try again later');
6241
+ // }
6242
+ // transcript = ret.data;
6243
+
6244
+ // break;
6245
+ // }
6246
+
6247
+ // case 'document':
6248
+ // case 'text': {
6249
+ // async function transcript_file(fileBuffer, fileName, mimeType) {
6250
+ // try {
6251
+ // const base64 = fileBuffer.toString('base64');
6252
+
6253
+ // const ret = await submit_chat_gpt_prompt({
6254
+ // uid,
6255
+ // prompt: [
6256
+ // {
6257
+ // role: 'user',
6258
+ // content: [
6259
+ // {
6260
+ // type: 'input_text',
6261
+ // text: 'extract all text from this document. Maintain the structure and formatting as much as possible.',
6262
+ // },
6263
+ // {
6264
+ // type: 'input_file',
6265
+ // filename: fileName || 'document.pdf',
6266
+
6267
+ // file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
6268
+ // },
6269
+ // ],
6270
+ // },
6271
+ // ],
6272
+ // metadata: { conversation_id, func: 'attachment_handler' },
6273
+ // account_profile_info,
6274
+ // });
6275
+
6276
+ // return ret;
6277
+ // } catch (error) {
6278
+ // console.error('Error transcribing PDF:', error);
6279
+ // throw error;
6280
+ // }
6281
+ // }
6282
+ // const buffer = await streamToBuffer(fileStream);
6283
+ // transcript = await transcript_file(buffer, filename, validation_ret.mime);
6284
+
6285
+ // break;
6286
+ // }
6287
+
6288
+ // default:
6289
+ // break;
6290
+ // }
6291
+
6292
+ // return transcript;
6293
+ // };
6189
6294
 
6190
6295
  // let file_names = [];
6191
6296
  let process_stat = 'full';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4872",
3
+ "version": "1.1.4873",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",