@xuda.io/ai_module 1.1.4308 → 1.1.4310

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 +51 -14
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -5289,7 +5289,7 @@ setTimeout(async () => {
5289
5289
  // Location: 'https://xuda-bucket-dev.nyc3.digitaloceanspaces.com/dev.xuda.io/user/d39126e0e2c51ffbd1aad10709fc8335/cb7353fc-4788-49b7-8e73-f7f1be108b69_drv_d39126e0e2c51ffbd1aad10709fc8335_b92337f79e090a2f287bc3dc35d0033e.png',
5290
5290
  // },
5291
5291
  // });
5292
- ret = await get_business_logo(uid, 'bank of america', account_profile_info);
5292
+ // ret = await get_business_logo(uid, 'bank of america', account_profile_info);
5293
5293
 
5294
5294
  console.log(ret);
5295
5295
  }, 1000);
@@ -6724,21 +6724,40 @@ export const is_business_contact = async function (uid, email, name, subject, bo
6724
6724
  };
6725
6725
 
6726
6726
  const get_business_logo = async function (uid, name, account_profile_info) {
6727
- const ret = await submit_chat_gpt_prompt({
6728
- uid,
6729
- prompt: `find "${name}" icon/logo, Return only png,jpg image url single result e.g: "https://example.com/logo_image.png", nothing else`,
6730
- model: 'gpt-5-nano',
6731
- // response_format: z.object({
6732
- // is_business: z.boolean().describe('true if this is a business account/contact, false otherwise'),
6733
- // }),
6734
- metadata: { func: 'get_business_logo' },
6735
- account_profile_info,
6736
- tools: [{ type: 'web_search_preview' }],
6737
- });
6738
- console.log('=============', ret);
6727
+ const get_url = async function () {
6728
+ const ret = await submit_chat_gpt_prompt({
6729
+ uid,
6730
+ prompt: `find "${name}" icon/logo, Return only png,jpg image url single result e.g: "https://example.com/logo_image.png", nothing else`,
6731
+ model: 'gpt-5-nano',
6739
6732
 
6733
+ metadata: { func: 'get_business_logo' },
6734
+ account_profile_info,
6735
+ tools: [{ type: 'web_search_preview' }],
6736
+ });
6737
+ console.log('=============', ret);
6738
+ return ret.data;
6739
+ };
6740
6740
  const url = ret.data;
6741
6741
  const file = await return_image_blob_from_downloaded_image(url);
6742
+ // const imageBase64 = Buffer.from(await file.image_blob.arrayBuffer()).toString('base64');
6743
+ const model = 'chatgpt-image-latest';
6744
+ const prompt = `
6745
+ extract the logo from the image
6746
+ `;
6747
+ const logo_response = await client.images.edit({
6748
+ model,
6749
+ image: file.image_blob, //logo
6750
+ prompt,
6751
+ background: 'transparent',
6752
+ });
6753
+ record_chat_usage(uid, logo_response.usage.input_tokens, logo_response.usage.output_tokens, 'business logo', prompt, model, { url }, account_profile_info);
6754
+ imageBase64 = await normalizeBase64To1024(ai_avatar_response.data[0].b64_json);
6755
+
6756
+ const outputBuffer = Buffer.from(imageBase64, 'base64');
6757
+ const outputPath = file.tempOutputPath;
6758
+ await fs.promises.writeFile(outputPath, outputBuffer);
6759
+
6760
+ console.log('Success! Saved →', outputPath);
6742
6761
 
6743
6762
  console.log(file);
6744
6763
  // prompt: `find "${name}" logo base on the following facts "${facts}", Return only png,jpg img tag , nothing else`,
@@ -6750,6 +6769,24 @@ const get_business_logo = async function (uid, name, account_profile_info) {
6750
6769
  // } catch (error) {}
6751
6770
  };
6752
6771
 
6772
+ async function normalizeBase64To1024(base64Image) {
6773
+ const inputBuffer = Buffer.from(base64Image, 'base64');
6774
+
6775
+ const normalizedBuffer = await sharp(inputBuffer)
6776
+ .resize(1024, 1024, {
6777
+ fit: 'contain', // Preserve aspect ratio, fit within 211x211
6778
+ background: { r: 0, g: 0, b: 0, alpha: 0 }, // Transparent background for padding
6779
+ })
6780
+ .png({
6781
+ quality: 100,
6782
+ compressionLevel: 9,
6783
+ force: true,
6784
+ })
6785
+ .toBuffer();
6786
+
6787
+ return normalizedBuffer.toString('base64'); // Return normalized base64 string
6788
+ }
6789
+
6753
6790
  const return_image_blob_from_downloaded_image = async function (url) {
6754
6791
  async function hasTransparentBackground(filePath) {
6755
6792
  const image = sharp(filePath);
@@ -6840,7 +6877,7 @@ const return_image_blob_from_downloaded_image = async function (url) {
6840
6877
  return { is_transparent: img_ret.is_transparent, image_blob: blob };
6841
6878
  }),
6842
6879
  );
6843
- return images[0];
6880
+ return { image_blob: images[0].image_blob, is_transparent: images[0].is_transparent, tempOutputPath };
6844
6881
  } catch (err) {
6845
6882
  console.error(err);
6846
6883
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4308",
3
+ "version": "1.1.4310",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",