utilitas 2000.3.32 → 2000.3.34

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/lib/alan.mjs CHANGED
@@ -8,8 +8,8 @@ import {
8
8
  MIME_MOV, MIME_MPEG, MIME_MP4, MIME_MPG, MIME_AVI, MIME_WMV, MIME_MPEGPS,
9
9
  MIME_FLV, MIME_GIF, MIME_WEBP, MIME_PDF, MIME_AAC, MIME_FLAC, MIME_MP3,
10
10
  MIME_MPEGA, MIME_M4A, MIME_MPGA, MIME_OPUS, MIME_PCM, MIME_WAV, MIME_WEBM,
11
- MIME_TGPP, MIME_PCM16, MIME_OGG, convert, formatDataURL,
12
- getTempPath, decodeBase64DataURL,
11
+ MIME_TGPP, MIME_PCM16, MIME_OGG, convert, getTempPath, decodeBase64DataURL,
12
+ getMime,
13
13
  } from './storage.mjs';
14
14
 
15
15
  import {
@@ -583,46 +583,51 @@ const isOpenrouter = (provider, model) => insensitiveCompare(
583
583
  provider, OPENROUTER
584
584
  ) && (model ? model?.source : true);
585
585
 
586
- const buildMessage = (content, options) => {
586
+ const buildMessage = async (content, options) => {
587
587
  content = content || '';
588
- const attachments = (options?.attachments || []).map(x => {
588
+ const attachments = await Promise.all((options?.attachments || []).map(async x => {
589
589
  assert(
590
590
  MODELS[options?.model]?.supportedMimeTypes?.includes?.(x.mime_type),
591
591
  `Unsupported mime type: '${x.mime_type}'.`
592
592
  );
593
+ if ([
594
+ MIME_PNG, MIME_JPEG, MIME_GIF, MIME_WEBP
595
+ ].includes?.(x.mime_type)) {
596
+ return {
597
+ type: 'image_url',
598
+ image_url: {
599
+ url: x.url || await convert(x.data, {
600
+ input: BUFFER, expected: DATAURL,
601
+ }), detail: 'high',
602
+ },
603
+ };
604
+ } else if ([
605
+ MIME_AAC, MIME_FLAC, MIME_MP3, MIME_MPEGA, MIME_M4A, MIME_MPGA,
606
+ MIME_OPUS, MIME_PCM, MIME_WAV, MIME_TGPP, MIME_PCM16, MIME_OGG,
607
+ ].includes?.(x.mime_type)) {
608
+ return {
609
+ type: 'input_audio',
610
+ input_audio: {
611
+ data: await convert(x.data, {
612
+ input: BUFFER, expected: BASE64,
613
+ }), format: WAV,
614
+ },
615
+ };
616
+ }
617
+ [
618
+ MIME_TEXT, MIME_MOV, MIME_MPEG, MIME_WEBM, MIME_MP4, MIME_MPG,
619
+ MIME_AVI, MIME_WMV, MIME_MPEGPS, MIME_FLV, MIME_PDF,
620
+ ].includes?.(x.mime_type)
621
+ || log(`Unknown mime type: '${x.mime_type}', fallbacked.`);
593
622
  return {
594
- type: 'image_url',
595
- image_url: { url: x.url, detail: 'high' },
623
+ type: 'file', file: {
624
+ file_data: await convert(x.data, {
625
+ input: BUFFER, expected: DATAURL,
626
+ }), filename: x.file_name
627
+ || `${uuidv4()}.${x.mime_type.split('/')[1]}`,
628
+ },
596
629
  };
597
- // if ([
598
- // MIME_PNG, MIME_JPEG, MIME_GIF, MIME_WEBP
599
- // ].includes?.(x.mime_type)) {
600
- // return {
601
- // type: 'image_url',
602
- // image_url: { url: x.url, detail: 'high' },
603
- // };
604
- // } else if ([
605
- // MIME_AAC, MIME_FLAC, MIME_MP3, MIME_MPEGA, MIME_M4A, MIME_MPGA,
606
- // MIME_OPUS, MIME_PCM, MIME_WAV, MIME_TGPP, MIME_PCM16, MIME_OGG,
607
- // ].includes?.(x.mime_type)) {
608
- // return {
609
- // type: 'input_audio',
610
- // input_audio: { data: x.data, format: WAV },
611
- // };
612
- // }
613
- // [
614
- // MIME_TEXT, MIME_MOV, MIME_MPEG, MIME_WEBM, MIME_MP4, MIME_MPG,
615
- // MIME_AVI, MIME_WMV, MIME_MPEGPS, MIME_FLV, MIME_PDF,
616
- // ].includes?.(x.mime_type)
617
- // || log(`Unknown mime type: '${x.mime_type}', fallbacked.`);
618
- // return {
619
- // type: 'file', file: {
620
- // file_data: formatDataURL(x.mime_type, x.data),
621
- // filename: x.file_name
622
- // || `${uuidv4()}.${x.mime_type.split('/')[1]}`,
623
- // },
624
- // };
625
- });
630
+ }));
626
631
  const message = String.isString(content) ? {
627
632
  role: options?.role || user,
628
633
  content: content.length ? [{ type: TEXT, text: content }] : [],
@@ -783,38 +788,31 @@ const buildPrompts = async (model, input, options = {}) => {
783
788
  options.attachments?.length ? options.attachments : []
784
789
  ).map(async x => {
785
790
  if (String.isString(x)) {
786
- var convResp = await convert(x, { input: FILE, expected: DATAURL, meta: true });
787
- return {
788
- url: convResp.content,
789
- mime_type: convResp.mime,
790
- }
791
+ var convResp = await convert(x, { input: FILE, expected: BUFFER, meta: true });
792
+ return { data: convResp.content, mime_type: convResp.mime };
791
793
  } else if (Buffer.isBuffer(x)) {
792
- var convResp = await convert(x, { input: BUFFER, expected: DATAURL, meta: true });
793
- return {
794
- url: convResp.content,
795
- mime_type: convResp.mime,
796
- }
794
+ return { data: x, mime_type: (await getMime(x))?.mime }
797
795
  } else if (Object.isObject(x)) { return x; } else { return null; }
798
796
  }))).filter(x => (model?.supportedMimeTypes || []).includes(x.mime_type));
799
- const systemPrompt = buildMessage(options.systemPrompt, system);
800
- const msgBuilder = () => {
797
+ const systemPrompt = await buildMessage(options.systemPrompt, system);
798
+ const msgBuilder = async () => {
801
799
  [history, _history] = [[], []];
802
- (options.messages?.length ? options.messages : []).map((x, i) => {
803
- _history.push(buildMessage(x.request));
804
- _history.push(buildMessage(x.response, _assistant));
805
- });
800
+ await Promise.all((options.messages?.length ? options.messages : []).map(async (x, i) => {
801
+ _history.push(await buildMessage(x.request));
802
+ _history.push(await buildMessage(x.response, _assistant));
803
+ }));
806
804
  history = messages([
807
- systemPrompt, ..._history, buildMessage(content, options),
805
+ systemPrompt, ..._history, await buildMessage(content, options),
808
806
  ...options.toolsResult?.length ? options.toolsResult : []
809
807
  ]);
810
808
  };
811
- msgBuilder();
809
+ await msgBuilder();
812
810
  await trimPrompt(() => [
813
811
  systemPrompt, _history, content, options.toolsResult
814
- ], () => {
812
+ ], async () => {
815
813
  if (options.messages?.length) {
816
814
  options.messages?.shift();
817
- msgBuilder();
815
+ await msgBuilder();
818
816
  } else if (options.trimBeginning) {
819
817
  content = '...' + trimBeginning(trimBeginning(content).slice(1));
820
818
  } else {
@@ -1233,7 +1231,7 @@ const initChat = async (options = {}) => {
1233
1231
  }
1234
1232
  options.instructions && (chatConfig.systemPrompt = options.instructions);
1235
1233
  // Use Gemini instead of ChatGPT because of the longer package.
1236
- const [spTokens, ais] = await Promise.all([countTokens([buildMessage(
1234
+ const [spTokens, ais] = await Promise.all([countTokens([await buildMessage(
1237
1235
  chatConfig.systemPrompt, system
1238
1236
  )]), getAi(null, { all: true })]);
1239
1237
  for (const ai of ais.filter(x => ![
@@ -1330,15 +1328,9 @@ const distillFile = async (attachments, o) => {
1330
1328
  o?.keepPaging ? '' : '- If the document has multiple pages, merge them into one page. Please do not return any paging information.',
1331
1329
  o?.keepDecoration ? '' : '- If the document has side notes, headers, footers, or watermarks, please ignore them.',
1332
1330
  ].filter(x => x).join('\n');
1333
- attachments = await Promise.all(ensureArray(attachments).map(async x => {
1334
- const convResp = await convert(
1335
- x, { expected: DATAURL, ...o || {}, meta: true }
1336
- );
1337
- return { url: convResp.content, mime_type: convResp.mime };
1338
- }));
1339
1331
  return await prompt(strPmt, {
1340
1332
  select: { vision: true, hearing: true, fast: true },
1341
- simple: true, ...o, attachments,
1333
+ simple: true, ...o, attachments: ensureArray(attachments),
1342
1334
  });
1343
1335
  };
1344
1336
 
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "2000.3.32",
4
+ "version": "2000.3.34",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/storage.mjs CHANGED
@@ -538,6 +538,7 @@ export {
538
538
  getConfigFilename,
539
539
  getGcUrlByBucket,
540
540
  getIdByGs,
541
+ getMime,
541
542
  getTempPath,
542
543
  handleError,
543
544
  init,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "2000.3.32",
4
+ "version": "2000.3.34",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",