utilitas 2000.3.30 → 2000.3.32

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
@@ -1,20 +1,19 @@
1
1
  import { checkSearch, distill, search } from './web.mjs';
2
2
  import { create as createUoid } from './uoid.mjs';
3
- import { fileTypeFromBuffer } from 'file-type';
4
3
  import { packPcmToWav } from './media.mjs';
5
4
  import { v4 as uuidv4 } from 'uuid';
6
5
 
7
6
  import {
8
- STREAM, FILE, BASE64, BUFFER, DATAURL, MIME_BINARY, MIME_TEXT, MIME_PNG,
9
- MIME_JPEG, MIME_MOV, MIME_MPEG, MIME_MP4, MIME_MPG, MIME_AVI, MIME_WMV,
10
- MIME_MPEGPS, MIME_FLV, MIME_GIF, MIME_WEBP, MIME_PDF, MIME_AAC, MIME_FLAC,
11
- MIME_MP3, MIME_MPEGA, MIME_M4A, MIME_MPGA, MIME_OPUS, MIME_PCM, MIME_WAV,
12
- MIME_WEBM, MIME_TGPP, MIME_PCM16, MIME_OGG, convert, formatDataURL,
7
+ STREAM, FILE, BASE64, BUFFER, DATAURL, MIME_TEXT, MIME_PNG, MIME_JPEG,
8
+ MIME_MOV, MIME_MPEG, MIME_MP4, MIME_MPG, MIME_AVI, MIME_WMV, MIME_MPEGPS,
9
+ MIME_FLV, MIME_GIF, MIME_WEBP, MIME_PDF, MIME_AAC, MIME_FLAC, MIME_MP3,
10
+ MIME_MPEGA, MIME_M4A, MIME_MPGA, MIME_OPUS, MIME_PCM, MIME_WAV, MIME_WEBM,
11
+ MIME_TGPP, MIME_PCM16, MIME_OGG, convert, formatDataURL,
13
12
  getTempPath, decodeBase64DataURL,
14
13
  } from './storage.mjs';
15
14
 
16
15
  import {
17
- log as _log, renderText as _renderText, ensureArray, ensureString, extract,
16
+ log as _log, renderText as _renderText, ensureArray, ensureString,
18
17
  ignoreErrFunc, insensitiveCompare, isSet, need, parseJson, throwError,
19
18
  tryUntil, timeout, mergeAtoB,
20
19
  } from './utilitas.mjs';
@@ -225,8 +224,6 @@ let ATTACHMENT_TOKEN_COST = 0;
225
224
  for (const n in MODELS) {
226
225
  MODELS[n]['name'] = n;
227
226
  MODELS[n].supportedMimeTypes = MODELS[n].supportedMimeTypes || [];
228
- MODELS[n].supportedDocTypes = MODELS[n].supportedDocTypes || [];
229
- MODELS[n].supportedAudioTypes = MODELS[n].supportedAudioTypes || [];
230
227
  MODELS[n].maxOutputTokens = MODELS[n].maxOutputTokens
231
228
  || Math.ceil(MODELS[n].contextWindow * 0.4);
232
229
  MODELS[n].maxInputTokens = MODELS[n].maxInputTokens
@@ -258,9 +255,7 @@ for (const n in MODELS) {
258
255
  // );
259
256
  // }
260
257
  // // combine supported types
261
- // for (const key of [
262
- // 'supportedAudioTypes', 'supportedDocTypes', 'supportedMimeTypes',
263
- // ]) {
258
+ // for (const key of ['supportedMimeTypes']) {
264
259
  // MODELS[AUTO][key] = [...new Set(
265
260
  // [...MODELS[AUTO][key] || [], ...MODELS[n][key] || []]
266
261
  // )];
@@ -588,55 +583,46 @@ const isOpenrouter = (provider, model) => insensitiveCompare(
588
583
  provider, OPENROUTER
589
584
  ) && (model ? model?.source : true);
590
585
 
591
- const selectVisionModel = options => {
592
- assert(
593
- MODELS[options.model]?.vision,
594
- `Vision modality is not supported by model: ${options.model}`
595
- );
596
- return String.isString(MODELS[options.model].vision)
597
- ? MODELS[options.model].vision : null;
598
- };
599
-
600
- const selectAudioModel = options => {
601
- assert(
602
- MODELS[options.model]?.audio,
603
- `Audio modality is not supported by model: ${options.model}`
604
- );
605
- return String.isString(MODELS[options.model]?.audio)
606
- ? MODELS[options.model]?.audio : null;
607
- };
608
-
609
586
  const buildMessage = (content, options) => {
610
587
  content = content || '';
611
- let alterModel = options?.audioMode && selectAudioModel(options);
612
588
  const attachments = (options?.attachments || []).map(x => {
613
- assert(MODELS[options?.model], 'Model is required.');
614
- if (MODELS[options.model]?.supportedMimeTypes?.includes?.(x.mime_type)) {
615
- alterModel = selectVisionModel(options); // URL or Base64URL
616
- return {
617
- type: 'image_url',
618
- image_url: { url: x.url, detail: 'high' },
619
- };
620
- } else if (MODELS[options.model]?.supportedDocTypes?.includes?.(x.mime_type)) {
621
- alterModel = selectVisionModel(options);
622
- return {
623
- type: 'file',
624
- file: {
625
- file_data: formatDataURL(x.mime_type, x.data),
626
- filename: x.file_name
627
- || `${uuidv4()}.${x.mime_type.split('/')[1]}`,
628
- },
629
- };
630
- } else if (MODELS[options.model]?.supportedAudioTypes?.includes?.(x.mime_type)) {
631
- alterModel = selectAudioModel(options);
632
- return {
633
- type: 'input_audio',
634
- input_audio: { data: x.data, format: WAV },
635
- };
636
- }
637
- throwError(`Unsupported mime type: '${x.mime_type}'.`);
589
+ assert(
590
+ MODELS[options?.model]?.supportedMimeTypes?.includes?.(x.mime_type),
591
+ `Unsupported mime type: '${x.mime_type}'.`
592
+ );
593
+ return {
594
+ type: 'image_url',
595
+ image_url: { url: x.url, detail: 'high' },
596
+ };
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
+ // };
638
625
  });
639
- alterModel && (options.model = alterModel);
640
626
  const message = String.isString(content) ? {
641
627
  role: options?.role || user,
642
628
  content: content.length ? [{ type: TEXT, text: content }] : [],
@@ -786,10 +772,10 @@ const packModelLabel = (model_reference) => {
786
772
  const buildPrompts = async (model, input, options = {}) => {
787
773
  assert(!(
788
774
  options.jsonMode && !model?.json
789
- ), `This model does not support JSON output: ${options.model}`);
775
+ ), `This model does not support JSON output: ${model.name}`);
790
776
  assert(!(
791
777
  options.reasoning && !model?.reasoning
792
- ), `This model does not support reasoning: ${options.model}`);
778
+ ), `This model does not support reasoning: ${model.name}`);
793
779
  let [history, content, prompt, _model, _assistant, _history]
794
780
  = [null, input, null, { role: MODEL }, { role: assistant }, null];
795
781
  options.systemPrompt = options.systemPrompt || INSTRUCTIONS;
@@ -809,11 +795,7 @@ const buildPrompts = async (model, input, options = {}) => {
809
795
  mime_type: convResp.mime,
810
796
  }
811
797
  } else if (Object.isObject(x)) { return x; } else { return null; }
812
- }))).filter(x => x && [
813
- ...model?.supportedMimeTypes,
814
- ...model?.supportedDocTypes,
815
- ...model?.supportedAudioTypes,
816
- ].includes(x.mime_type));
798
+ }))).filter(x => (model?.supportedMimeTypes || []).includes(x.mime_type));
817
799
  const systemPrompt = buildMessage(options.systemPrompt, system);
818
800
  const msgBuilder = () => {
819
801
  [history, _history] = [[], []];
@@ -1256,7 +1238,7 @@ const initChat = async (options = {}) => {
1256
1238
  )]), getAi(null, { all: true })]);
1257
1239
  for (const ai of ais.filter(x => ![
1258
1240
  IMAGEN_4_ULTRA, VEO_31, GPT_4O_TRANSCRIBE,
1259
- ].includes(x.name))) {
1241
+ ].includes(x.model.name))) {
1260
1242
  const mxPmpt = ai.model.maxInputTokens / 2;
1261
1243
  assert(spTokens < mxPmpt,
1262
1244
  `System prompt is too long: ${spTokens} / ${mxPmpt} tokens.`);
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.30",
4
+ "version": "2000.3.32",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
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.30",
4
+ "version": "2000.3.32",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",