utilitas 1999.1.20 → 1999.1.21

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
@@ -38,7 +38,7 @@ You may be provided with some tools(functions) to help you gather information an
38
38
  - Unless otherwise specified to require the original result, in most cases, you may reorganize the information obtained after using the tool to solve the problem as needed.`;
39
39
 
40
40
  const _NEED = [
41
- '@anthropic-ai/sdk', '@anthropic-ai/vertex-sdk', '@google/generative-ai',
41
+ '@anthropic-ai/sdk', '@anthropic-ai/vertex-sdk', '@google/genai',
42
42
  'js-tiktoken', 'OpenAI',
43
43
  ];
44
44
 
@@ -356,9 +356,9 @@ const toolsGemini = async () => (await toolsOpenAI()).map(x => ({
356
356
  properties: x.def.function.parameters.properties,
357
357
  required: x.def.function.parameters.required,
358
358
  },
359
- response: x.def.function?.response ?? {
360
- type: 'string', description: 'It could be a string or JSON',
361
- },
359
+ // Vertex API only: response: x.def.function?.response ?? {
360
+ // type: 'string', description: 'It could be a string or JSON',
361
+ // },
362
362
  }
363
363
  }));
364
364
 
@@ -427,8 +427,8 @@ const init = async (options = {}) => {
427
427
  break;
428
428
  case GEMINI:
429
429
  assertApiKey(provider, options);
430
- const { GoogleGenerativeAI } = await need('@google/generative-ai');
431
- var client = new GoogleGenerativeAI(options.apiKey);
430
+ const { GoogleGenAI } = await need('@google/genai');
431
+ var client = new GoogleGenAI(options);
432
432
  for (let model of models) {
433
433
  setupAi({
434
434
  provider, model, client,
@@ -506,7 +506,7 @@ const packAi = (ais, options = {}) => {
506
506
  const getAi = async (id, options = {}) => {
507
507
  if (id) {
508
508
  const ai = ais.find(x => x.id === id);
509
- assert(ais, `AI not found: ${id}.`);
509
+ assert(ai, `AI not found: ${id}.`);
510
510
  return options?.client ? ai?.client : ai;
511
511
  } else if (options?.select) {
512
512
  const res = [];
@@ -795,6 +795,8 @@ const buildPrompts = async (model, input, options = {}) => {
795
795
  history.push(buildClaudeMessage(x.response, _assistant));
796
796
  break;
797
797
  case GEMINI:
798
+ // https://github.com/google/generative-ai-js/blob/main/samples/node/advanced-chat.js
799
+ // Google's bug: history is not allowed while using inline_data?
798
800
  if (options.attachments?.length) { return; }
799
801
  history.push(buildGeminiHistory(x.request, _user));
800
802
  history.push(buildGeminiHistory(x.response, { role: MODEL }));
@@ -826,9 +828,9 @@ const buildPrompts = async (model, input, options = {}) => {
826
828
  }
827
829
  };
828
830
  msgBuilder();
829
- await trimPrompt(() => [systemPrompt, history, prompt], () => {
830
- if (options.messages.length) {
831
- options.messages.shift();
831
+ await trimPrompt(() => [systemPrompt, history, content], () => {
832
+ if (options.messages?.length) {
833
+ options.messages?.shift();
832
834
  msgBuilder();
833
835
  } else {
834
836
  content = trimTailing(trimTailing(content).slice(0, -1)) + '...';
@@ -1110,15 +1112,6 @@ const deleteFile = async (aiId, file_id, options) => {
1110
1112
  return await client.files.del(file_id);
1111
1113
  };
1112
1114
 
1113
- const generationConfig = options => ({
1114
- generationConfig: {
1115
- responseMimeType: options.jsonMode ? MIME_JSON : MIME_TEXT,
1116
- responseModalities: options.modalities
1117
- || (options.imageMode ? [TEXT, IMAGE] : undefined),
1118
- ...options?.generationConfig || {},
1119
- },
1120
- });
1121
-
1122
1115
  const packGeminiReferences = (chunks, supports) => {
1123
1116
  let references = null;
1124
1117
  if (chunks?.length && supports?.length) {
@@ -1134,8 +1127,8 @@ const packGeminiReferences = (chunks, supports) => {
1134
1127
 
1135
1128
  const promptGemini = async (aiId, content, options = {}) => {
1136
1129
  let { client, model } = await getAi(aiId);
1137
- let [result, references, functionCalls, responded, images]
1138
- = [options.result ?? '', null, null, false, []];
1130
+ let [event, result, references, functionCalls, responded, images] =
1131
+ [null, options.result ?? '', null, [], false, []];
1139
1132
  options.model = options.model || model.name;
1140
1133
  model?.image === true && (options.imageMode = true);
1141
1134
  assert(!(options.imageMode && !model.image), 'Image mode is not supported.');
@@ -1146,43 +1139,44 @@ const promptGemini = async (aiId, content, options = {}) => {
1146
1139
  }
1147
1140
  const { systemPrompt: systemInstruction, history, prompt }
1148
1141
  = await buildPrompts(model, content, { ...options, flavor: GEMINI });
1149
- const _client = client.getGenerativeModel({
1150
- model: options.model, systemInstruction,
1151
- ...model?.tools && !options.jsonMode
1152
- && options.model !== GEMINI_20_FLASH_EXP ? (options.tools ?? {
1153
- tools: [
1154
- // @todo: Gemini will failed when using these tools together.
1155
- // https://ai.google.dev/gemini-api/docs/function-calling
1156
- // { codeExecution: {} },
1157
- // { googleSearch: {} },
1158
- {
1159
- functionDeclarations: (
1160
- await toolsGemini()
1161
- ).map(x => x.def)
1162
- },
1163
- ],
1164
- toolConfig: { functionCallingConfig: { mode: 'AUTO' } },
1165
- }) : {},
1142
+ const chat = client.chats.create({
1143
+ model: options.model, history, config: {
1144
+ responseMimeType: options.jsonMode ? MIME_JSON : MIME_TEXT,
1145
+ systemInstruction, responseModalities: options.modalities || (
1146
+ options.imageMode ? [TEXT, IMAGE] : undefined
1147
+ ), ...options?.config || {}, ...model?.tools && !options.jsonMode
1148
+ && options.model !== GEMINI_20_FLASH_EXP ? (options.tools ?? {
1149
+ tools: [
1150
+ // @todo: Gemini will failed when using these tools together.
1151
+ // https://ai.google.dev/gemini-api/docs/function-calling
1152
+ // { codeExecution: {} },
1153
+ // { googleSearch: {} },
1154
+ {
1155
+ functionDeclarations: (
1156
+ await toolsGemini()
1157
+ ).map(x => x.def)
1158
+ },
1159
+ ], toolConfig: { functionCallingConfig: { mode: 'AUTO' } },
1160
+ }) : {},
1161
+ },
1166
1162
  });
1167
- // https://github.com/google/generative-ai-js/blob/main/samples/node/advanced-chat.js
1168
- // Google's bug: history is not allowed while using inline_data?
1169
- const chat = _client.startChat({ history, ...generationConfig(options) });
1170
- const resp = await chat.sendMessageStream(prompt);
1171
- for await (const chunk of resp.stream) {
1172
- const deltaImages = [];
1173
- chunk.candidates[0].content?.parts?.filter(
1174
- x => x?.inlineData?.mimeType === png
1175
- )?.map?.(x => {
1176
- deltaImages.push(x.inlineData);
1177
- images.push(x.inlineData);
1163
+ const resp = await chat.sendMessageStream({ message: prompt });
1164
+ for await (const chunk of resp) {
1165
+ event = chunk.candidates[0];
1166
+ let [deltaText, deltaImages] = ['', []];
1167
+ event?.content?.parts?.map(x => {
1168
+ if (x.text) { deltaText = x.text; }
1169
+ else if (x.functionCall) { functionCalls.push(x); }
1170
+ else if (x.inlineData?.mimeType === png) {
1171
+ deltaImages.push(x.inlineData);
1172
+ images.push(x.inlineData);
1173
+ }
1178
1174
  });
1179
- functionCalls || (functionCalls = chunk.functionCalls);
1180
1175
  const rfc = packGeminiReferences(
1181
- chunk.candidates[0]?.groundingMetadata?.groundingChunks,
1182
- chunk.candidates[0]?.groundingMetadata?.groundingSupports
1176
+ event?.groundingMetadata?.groundingChunks,
1177
+ event?.groundingMetadata?.groundingSupports
1183
1178
  );
1184
1179
  rfc && (references = rfc);
1185
- let deltaText = chunk?.text?.() || '';
1186
1180
  options.result && deltaText
1187
1181
  && (responded = responded || (deltaText = `\n\n${deltaText}`));
1188
1182
  result += deltaText;
@@ -1191,10 +1185,6 @@ const promptGemini = async (aiId, content, options = {}) => {
1191
1185
  images: options.delta ? deltaImages : images,
1192
1186
  }, options);
1193
1187
  }
1194
- const _resp = await resp.response;
1195
- functionCalls = (
1196
- functionCalls() || _resp.functionCalls() || []
1197
- ).map(x => ({ functionCall: x }));
1198
1188
  const { toolsResult, toolsResponse } = await handleToolsCall(
1199
1189
  { role: MODEL, parts: functionCalls },
1200
1190
  { ...options, result, flavor: GEMINI }
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": "1999.1.20",
4
+ "version": "1999.1.21",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -19,7 +19,7 @@ const manifest = {
19
19
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz"
20
20
  },
21
21
  "dependencies": {
22
- "file-type": "^20.4.0",
22
+ "file-type": "^20.4.1",
23
23
  "mathjs": "^14.3.1",
24
24
  "uuid": "^11.1.0"
25
25
  },
@@ -28,14 +28,15 @@ const manifest = {
28
28
  "@anthropic-ai/vertex-sdk": "^0.7.0",
29
29
  "@ffmpeg-installer/ffmpeg": "^1.1.0",
30
30
  "@ffprobe-installer/ffprobe": "^2.1.2",
31
- "@google-cloud/speech": "^6.7.1",
31
+ "@google-cloud/speech": "^7.0.0",
32
32
  "@google-cloud/storage": "^7.15.2",
33
- "@google-cloud/text-to-speech": "^5.8.1",
34
- "@google-cloud/vision": "^4.3.3",
33
+ "@google-cloud/text-to-speech": "^6.0.0",
34
+ "@google-cloud/vision": "^5.0.0",
35
+ "@google/genai": "^0.4.0",
35
36
  "@google/generative-ai": "^0.24.0",
36
37
  "@mozilla/readability": "github:mozilla/readability",
37
- "@sentry/node": "^9.5.0",
38
- "@sentry/profiling-node": "^9.5.0",
38
+ "@sentry/node": "^9.6.0",
39
+ "@sentry/profiling-node": "^9.6.0",
39
40
  "acme-client": "^5.4.0",
40
41
  "browserify-fs": "^1.0.0",
41
42
  "buffer": "^6.0.3",
@@ -53,9 +54,9 @@ const manifest = {
53
54
  "node-mailjet": "^6.0.8",
54
55
  "node-polyfill-webpack-plugin": "^4.1.0",
55
56
  "office-text-extractor": "^3.0.3",
56
- "openai": "^4.87.3",
57
- "pdfjs-dist": "^4.10.38",
58
- "pg": "^8.14.0",
57
+ "openai": "^4.87.4",
58
+ "pdfjs-dist": "^5.0.375",
59
+ "pg": "^8.14.1",
59
60
  "pgvector": "^0.2.0",
60
61
  "ping": "^0.4.4",
61
62
  "process": "^0.11.10",
@@ -68,7 +69,7 @@ const manifest = {
68
69
  "url": "github:Leask/node-url",
69
70
  "webpack-cli": "^6.0.1",
70
71
  "whisper-node": "^1.1.1",
71
- "wrangler": "^3.114.1",
72
+ "wrangler": "^4.1.0",
72
73
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz",
73
74
  "youtube-transcript": "^1.2.1"
74
75
  }
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": "1999.1.20",
4
+ "version": "1999.1.21",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -30,7 +30,7 @@
30
30
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz"
31
31
  },
32
32
  "dependencies": {
33
- "file-type": "^20.4.0",
33
+ "file-type": "^20.4.1",
34
34
  "mathjs": "^14.3.1",
35
35
  "uuid": "^11.1.0"
36
36
  },
@@ -39,14 +39,15 @@
39
39
  "@anthropic-ai/vertex-sdk": "^0.7.0",
40
40
  "@ffmpeg-installer/ffmpeg": "^1.1.0",
41
41
  "@ffprobe-installer/ffprobe": "^2.1.2",
42
- "@google-cloud/speech": "^6.7.1",
42
+ "@google-cloud/speech": "^7.0.0",
43
43
  "@google-cloud/storage": "^7.15.2",
44
- "@google-cloud/text-to-speech": "^5.8.1",
45
- "@google-cloud/vision": "^4.3.3",
44
+ "@google-cloud/text-to-speech": "^6.0.0",
45
+ "@google-cloud/vision": "^5.0.0",
46
+ "@google/genai": "^0.4.0",
46
47
  "@google/generative-ai": "^0.24.0",
47
48
  "@mozilla/readability": "github:mozilla/readability",
48
- "@sentry/node": "^9.5.0",
49
- "@sentry/profiling-node": "^9.5.0",
49
+ "@sentry/node": "^9.6.0",
50
+ "@sentry/profiling-node": "^9.6.0",
50
51
  "acme-client": "^5.4.0",
51
52
  "browserify-fs": "^1.0.0",
52
53
  "buffer": "^6.0.3",
@@ -64,9 +65,9 @@
64
65
  "node-mailjet": "^6.0.8",
65
66
  "node-polyfill-webpack-plugin": "^4.1.0",
66
67
  "office-text-extractor": "^3.0.3",
67
- "openai": "^4.87.3",
68
- "pdfjs-dist": "^4.10.38",
69
- "pg": "^8.14.0",
68
+ "openai": "^4.87.4",
69
+ "pdfjs-dist": "^5.0.375",
70
+ "pg": "^8.14.1",
70
71
  "pgvector": "^0.2.0",
71
72
  "ping": "^0.4.4",
72
73
  "process": "^0.11.10",
@@ -79,7 +80,7 @@
79
80
  "url": "github:Leask/node-url",
80
81
  "webpack-cli": "^6.0.1",
81
82
  "whisper-node": "^1.1.1",
82
- "wrangler": "^3.114.1",
83
+ "wrangler": "^4.1.0",
83
84
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz",
84
85
  "youtube-transcript": "^1.2.1"
85
86
  }