@xuda.io/ai_module 1.1.4829 → 1.1.4831

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 +14 -231
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -3340,229 +3340,6 @@ export const upload_prompt_attachment = async function (req, job_id, headers, fi
3340
3340
  }
3341
3341
  };
3342
3342
 
3343
- // export const upload_prompt_attachment2 = async function (req, job_id, headers, file_obj) {
3344
- // try {
3345
- // if (!file_obj?.['originalname']) {
3346
- // throw new Error('file data missing');
3347
- // }
3348
-
3349
- // const file_name = file_obj?.['originalname'];
3350
-
3351
- // const validation_ret = file_upload_validator(file_name);
3352
-
3353
- // if (!validation_ret.valid) {
3354
- // throw new Error(validation_ret.error);
3355
- // }
3356
-
3357
- // const tempPath = file_obj.path;
3358
-
3359
- // // Create the stream from the temp path
3360
- // const fileStream = fs.createReadStream(tempPath);
3361
- // let file;
3362
- // let transcript;
3363
- // switch (validation_ret.category) {
3364
- // case 'audio':
3365
- // case 'video': {
3366
- // transcript = await transcribe(fileStream);
3367
- // file = new File([Buffer.from(transcript, 'utf8')], file_name, {
3368
- // type: 'text/plain',
3369
- // });
3370
-
3371
- // break;
3372
- // }
3373
-
3374
- // case 'image': {
3375
- // const base64Image = encodeImage(tempPath);
3376
- // const ret = await submit_chat_gpt_prompt({
3377
- // prompt: [
3378
- // {
3379
- // role: 'user',
3380
- // content: [
3381
- // {
3382
- // type: 'input_text',
3383
- // text: `1. describe the image in detail. 2. Extract the text from this image into a clean markdown format `,
3384
- // },
3385
- // {
3386
- // type: 'input_image',
3387
- // image_url: `data:${validation_ret.mime};base64,${base64Image}`,
3388
- // },
3389
- // ],
3390
- // },
3391
- // ],
3392
- // });
3393
- // if (ret.code < 0) {
3394
- // throw new Error('something went wrong try again later');
3395
- // }
3396
- // transcript = ret.data;
3397
- // file = new File([Buffer.from(transcript, 'utf8')], file_name + '.txt', {
3398
- // type: 'text/plain',
3399
- // });
3400
-
3401
- // break;
3402
- // }
3403
-
3404
- // case 'document':
3405
- // case 'text': {
3406
- // // async function streamToText(stream) {
3407
- // // const chunks = [];
3408
- // // for await (const chunk of stream) {
3409
- // // chunks.push(chunk);
3410
- // // }
3411
- // // return Buffer.concat(chunks).toString('utf8');
3412
- // // }
3413
-
3414
- // // async function transcript_file(Buffer, fileName) {
3415
- // // try {
3416
- // // const ret = await submit_chat_gpt_prompt({
3417
- // // prompt: [
3418
- // // {
3419
- // // role: 'user',
3420
- // // content: [
3421
- // // {
3422
- // // type: 'input_text',
3423
- // // text: 'Please extract all text from this document. Maintain the structure and formatting as much as possible.',
3424
- // // },
3425
- // // {
3426
- // // type: 'input_file',
3427
- // // source: {
3428
- // // type: 'base64',
3429
- // // // media_type: validation_ret.mime,
3430
- // // // data: Buffer.toString('base64'),
3431
- // // url: `data:${validation_ret.mime};base64,${Buffer.toString('base64')}`,
3432
- // // },
3433
- // // },
3434
- // // ],
3435
- // // },
3436
- // // ],
3437
- // // });
3438
- // // debugger;
3439
- // // return ret;
3440
- // // } catch (error) {
3441
- // // console.error('Error transcribing PDF:', error);
3442
- // // throw error;
3443
- // // }
3444
- // // }
3445
-
3446
- // // // file = await toFile(fileStream, file_name);
3447
- // // // transcript = await streamToText(fileStream);
3448
-
3449
- // async function streamToBuffer(fileStream) {
3450
- // const chunks = [];
3451
- // for await (const chunk of fileStream) {
3452
- // chunks.push(chunk);
3453
- // }
3454
- // return Buffer.concat(chunks);
3455
- // }
3456
- // // transcript = await transcript_file(await streamToBuffer(fileStream));
3457
-
3458
- // // async function transcript_file(fileBuffer, fileName) {
3459
- // // try {
3460
- // // const ret = await submit_chat_gpt_prompt({
3461
- // // prompt: [
3462
- // // {
3463
- // // role: 'user',
3464
- // // content: [
3465
- // // {
3466
- // // type: 'input_text',
3467
- // // text: 'Please extract all text from this document. Maintain the structure and formatting as much as possible.',
3468
- // // },
3469
- // // {
3470
- // // type: 'input_file',
3471
- // // // FIX: Flattened structure. Do NOT use 'source'.
3472
- // // // Place these properties directly at the content item level.
3473
- // // media_type: validation_ret.mime,
3474
- // // data: fileBuffer.toString('base64'),
3475
- // // },
3476
- // // ],
3477
- // // },
3478
- // // ],
3479
- // // });
3480
- // // debugger;
3481
- // // return ret;
3482
- // // } catch (error) {
3483
- // // console.error('Error transcribing PDF:', error);
3484
- // // throw error;
3485
- // // }
3486
- // // }
3487
-
3488
- // // // Helper to convert stream to buffer
3489
- // // async function streamToBuffer(fileStream) {
3490
- // // const chunks = [];
3491
- // // for await (const chunk of fileStream) {
3492
- // // chunks.push(chunk);
3493
- // // }
3494
- // // return Buffer.concat(chunks);
3495
- // // }
3496
-
3497
- // // // Execution
3498
- // // // Note: This consumes the stream completely.
3499
- // // // If you uncomment 'toFile' later, you must use the PassThrough logic discussed previously.
3500
- // // const buffer = await streamToBuffer(fileStream);
3501
- // // transcript = await transcript_file(buffer);
3502
-
3503
- // async function transcript_file(fileBuffer, fileName, mimeType) {
3504
- // try {
3505
- // const base64 = fileBuffer.toString('base64');
3506
-
3507
- // const ret = await submit_chat_gpt_prompt({
3508
- // // assuming your wrapper maps this to `input` for responses.create
3509
- // prompt: [
3510
- // {
3511
- // role: 'user',
3512
- // content: [
3513
- // {
3514
- // type: 'input_text',
3515
- // text: 'Please extract all text from this document. Maintain the structure and formatting as much as possible.',
3516
- // },
3517
- // {
3518
- // type: 'input_file',
3519
- // filename: fileName || 'document.pdf',
3520
- // // IMPORTANT: file_data is a *data URI* with mime type + base64
3521
- // file_data: `data:${mimeType || 'application/pdf'};base64,${base64}`,
3522
- // },
3523
- // ],
3524
- // },
3525
- // ],
3526
- // });
3527
-
3528
- // debugger;
3529
- // return ret;
3530
- // } catch (error) {
3531
- // console.error('Error transcribing PDF:', error);
3532
- // throw error;
3533
- // }
3534
- // }
3535
- // const buffer = await streamToBuffer(fileStream);
3536
- // transcript = await transcript_file(buffer, file_name, validation_ret.mime);
3537
-
3538
- // break;
3539
- // }
3540
-
3541
- // default:
3542
- // break;
3543
- // }
3544
-
3545
- // // // Convert stream to an OpenAI-friendly file object with the specific name
3546
- // // const convertedFile = await toFile(fileStream, file_name);
3547
-
3548
- // // const uploadedFile = await client.files.create({
3549
- // // file,
3550
- // // purpose: 'assistants',
3551
- // // });
3552
-
3553
- // // const vector_ret = await client.vectorStores.files.create(_conf.OPENAI_VECTOR_STORE_ID, {
3554
- // // file_id: uploadedFile.id,
3555
- // // });
3556
-
3557
- // // return { code: 1, data: { id: vector_ret.id, vector_store_id: vector_ret.vector_store_id, file_name: file_name, transcript } };
3558
- // return { code: 1, data: { file_name: file_name, transcript } };
3559
- // } catch (err) {
3560
- // // It is good practice to log the specific error for debugging
3561
- // console.error('Upload Error:', err);
3562
- // return { code: -1, data: err.message };
3563
- // }
3564
- // };
3565
-
3566
3343
  export const delete_prompt_attachment = async function (req, job_id, headers, file_obj) {
3567
3344
  const { file_id } = req;
3568
3345
 
@@ -3583,15 +3360,21 @@ export const submit_chat_gpt_prompt = async function (req) {
3583
3360
  }
3584
3361
 
3585
3362
  try {
3586
- const response = await client.responses.create({
3587
- model,
3588
- input: prompt,
3589
- tools,
3363
+ let response;
3364
+ try {
3365
+ response = await client.responses.create({
3366
+ model,
3367
+ input: prompt,
3368
+ tools,
3590
3369
 
3591
- text: {
3592
- format: formattedParams,
3593
- },
3594
- });
3370
+ text: {
3371
+ format: formattedParams,
3372
+ },
3373
+ });
3374
+ } catch (err) {
3375
+ report_ai_status('vectorStores', err);
3376
+ throw err;
3377
+ }
3595
3378
  account_module.record_ai_usage(uid, response.usage.input_tokens, response.usage.output_tokens, 'submit chat', prompt, model, metadata, account_profile_info, tools);
3596
3379
  return { code: 5, data: response.output_text };
3597
3380
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4829",
3
+ "version": "1.1.4831",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",