@gringow/gringow 0.1.2 → 0.1.4

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 (58) hide show
  1. package/README.md +313 -35
  2. package/dist/browser.d.mts +2 -1
  3. package/dist/browser.mjs +61 -4
  4. package/dist/config.d.mts +1 -0
  5. package/dist/config.mjs +5 -3
  6. package/dist/config.test.mjs +52 -67
  7. package/dist/constants/default-config.mjs +1 -0
  8. package/dist/{data/create-cache-id.d.mts → create-cache-id.d.mts} +2 -2
  9. package/dist/create-cache-id.mjs +54 -0
  10. package/dist/create-cache-id.test.d.mts +2 -0
  11. package/dist/create-cache-id.test.mjs +18464 -0
  12. package/dist/data/check-cache.d.mts +1 -1
  13. package/dist/data/check-cache.mjs +14 -7
  14. package/dist/data/clear-cache.mjs +14 -7
  15. package/dist/data/create-cache-dir.mjs +5 -3
  16. package/dist/data/delete-cache-item.d.mts +1 -1
  17. package/dist/data/delete-cache-item.mjs +14 -7
  18. package/dist/data/get-cache.mjs +5 -3
  19. package/dist/data/get-or-create-cache.d.mts +1 -1
  20. package/dist/data/get-or-create-cache.mjs +14 -7
  21. package/dist/data/md5.d.mts +13 -0
  22. package/dist/data/md5.mjs +47 -0
  23. package/dist/data/md5.test.d.mts +2 -0
  24. package/dist/data/md5.test.mjs +18457 -0
  25. package/dist/data/save-cache.d.mts +2 -2
  26. package/dist/data/save-cache.mjs +14 -7
  27. package/dist/data/write-cache-to-disk.d.mts +1 -1
  28. package/dist/data/write-cache-to-disk.mjs +14 -7
  29. package/dist/flat-template-string.test.mjs +10 -10
  30. package/dist/gringow.d.mts +1 -1
  31. package/dist/gringow.mjs +100 -35
  32. package/dist/index.d.mts +1 -1
  33. package/dist/index.mjs +100 -35
  34. package/dist/interpolate-template.d.mts +3 -0
  35. package/dist/interpolate-template.mjs +14 -0
  36. package/dist/io/anthropic/ask.mjs +5 -3
  37. package/dist/io/anthropic/commit-response.d.mts +1 -1
  38. package/dist/io/anthropic/index.d.mts +1 -1
  39. package/dist/io/anthropic/index.mjs +5 -3
  40. package/dist/io/anthropic/parse.d.mts +1 -1
  41. package/dist/io/ask-llm.d.mts +1 -1
  42. package/dist/io/ask-llm.mjs +38 -25
  43. package/dist/io/gemini/ask.mjs +38 -25
  44. package/dist/io/gemini/index.d.mts +1 -1
  45. package/dist/io/gemini/index.mjs +38 -25
  46. package/dist/io/gemini/parse.d.mts +1 -1
  47. package/dist/io/huggingface/ask.d.mts +1 -1
  48. package/dist/io/huggingface/ask.mjs +5 -3
  49. package/dist/io/huggingface/commit-response.d.mts +1 -1
  50. package/dist/io/huggingface/index.d.mts +1 -1
  51. package/dist/io/huggingface/index.mjs +5 -3
  52. package/dist/io/huggingface/parse.d.mts +1 -1
  53. package/dist/logger.test.mjs +10 -10
  54. package/dist/schemas/config.shcema.d.mts +5 -0
  55. package/dist/schemas/config.shcema.mjs +1 -0
  56. package/dist/types/index.d.mts +1 -1
  57. package/package.json +10 -10
  58. package/dist/data/create-cache-id.mjs +0 -10
@@ -3,8 +3,8 @@ import '../config.mjs';
3
3
  import '../constants/default-config.mjs';
4
4
  import 'zod';
5
5
  import '../schemas/config.shcema.mjs';
6
+ import '../create-cache-id.mjs';
6
7
  import './clear-cache.mjs';
7
- import './create-cache-id.mjs';
8
8
  import './get-cache.mjs';
9
9
  import '../flat-template-string.mjs';
10
10
 
@@ -14,6 +14,6 @@ import '../flat-template-string.mjs';
14
14
  * @param cache - The cache to save.
15
15
  * @returns The cache.
16
16
  */
17
- declare function saveCache(cache: GringowCache): Promise<void>;
17
+ declare function saveCache(cache: GringowCache): Promise<GringowCache>;
18
18
 
19
19
  export { saveCache };
@@ -21,6 +21,7 @@ var defaultConfig = {
21
21
  },
22
22
  keys: {},
23
23
  sourceLanguage: "en-US",
24
+ defaultLanguage: "pt-BR",
24
25
  languages: ["pt-BR", "fr-CA"],
25
26
  llm: {
26
27
  choice: "huggingface",
@@ -48,6 +49,7 @@ var configSchema = z.object({
48
49
  geminiApiKey: z.string().optional()
49
50
  }),
50
51
  sourceLanguage: languageSchema,
52
+ defaultLanguage: languageSchema,
51
53
  languages: z.array(languageSchema),
52
54
  llm: z.object({
53
55
  choice: llmOptionsSchema,
@@ -104,9 +106,9 @@ async function loadUserConfig() {
104
106
  var awaitedConfig = loadUserConfig();
105
107
  function defaultPrePrompt(config, extraInstructions) {
106
108
  return `
107
- You are a translation generator.
109
+ You are a ultimate translation generator server api.
108
110
 
109
- Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting outside the JSON**.
111
+ Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
110
112
 
111
113
  Follow these rules strictly:
112
114
 
@@ -116,7 +118,7 @@ Follow these rules strictly:
116
118
  - Apply these creatively but **without altering the meaning or punctuation** of the original text.
117
119
  3. Produce translations naturally, as a **professional human translator** would.
118
120
  4. Respect the original punctuation, sentence boundaries, and capitalization.
119
- 5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
121
+ 5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
120
122
  6. The final output must be a **valid JSON** following this exact schema:
121
123
 
122
124
  {
@@ -368,12 +370,17 @@ async function createCacheDir() {
368
370
  }
369
371
 
370
372
  // src/data/save-cache.ts
373
+ var savingQueue = Promise.resolve();
371
374
  async function saveCache(cache) {
372
375
  try {
373
- const config = await awaitedConfig;
374
- await createCacheDir();
375
- const contents = JSON.stringify(cache, null, 2);
376
- await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
376
+ savingQueue = savingQueue.then(async () => {
377
+ const config = await awaitedConfig;
378
+ await createCacheDir();
379
+ const contents = JSON.stringify(cache, null, 2);
380
+ await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
381
+ });
382
+ await savingQueue;
383
+ return cache;
377
384
  } catch (error) {
378
385
  throw error;
379
386
  }
@@ -3,7 +3,7 @@ import '../config.mjs';
3
3
  import '../constants/default-config.mjs';
4
4
  import 'zod';
5
5
  import '../schemas/config.shcema.mjs';
6
+ import '../create-cache-id.mjs';
6
7
  import './clear-cache.mjs';
7
- import './create-cache-id.mjs';
8
8
  import './get-cache.mjs';
9
9
  import '../flat-template-string.mjs';
@@ -21,6 +21,7 @@ var defaultConfig = {
21
21
  },
22
22
  keys: {},
23
23
  sourceLanguage: "en-US",
24
+ defaultLanguage: "pt-BR",
24
25
  languages: ["pt-BR", "fr-CA"],
25
26
  llm: {
26
27
  choice: "huggingface",
@@ -48,6 +49,7 @@ var configSchema = z.object({
48
49
  geminiApiKey: z.string().optional()
49
50
  }),
50
51
  sourceLanguage: languageSchema,
52
+ defaultLanguage: languageSchema,
51
53
  languages: z.array(languageSchema),
52
54
  llm: z.object({
53
55
  choice: llmOptionsSchema,
@@ -104,9 +106,9 @@ async function loadUserConfig() {
104
106
  var awaitedConfig = loadUserConfig();
105
107
  function defaultPrePrompt(config, extraInstructions) {
106
108
  return `
107
- You are a translation generator.
109
+ You are a ultimate translation generator server api.
108
110
 
109
- Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting outside the JSON**.
111
+ Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
110
112
 
111
113
  Follow these rules strictly:
112
114
 
@@ -116,7 +118,7 @@ Follow these rules strictly:
116
118
  - Apply these creatively but **without altering the meaning or punctuation** of the original text.
117
119
  3. Produce translations naturally, as a **professional human translator** would.
118
120
  4. Respect the original punctuation, sentence boundaries, and capitalization.
119
- 5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
121
+ 5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
120
122
  6. The final output must be a **valid JSON** following this exact schema:
121
123
 
122
124
  {
@@ -378,12 +380,17 @@ async function getCache() {
378
380
  throw error;
379
381
  }
380
382
  }
383
+ var savingQueue = Promise.resolve();
381
384
  async function saveCache(cache) {
382
385
  try {
383
- const config = await awaitedConfig;
384
- await createCacheDir();
385
- const contents = JSON.stringify(cache, null, 2);
386
- await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
386
+ savingQueue = savingQueue.then(async () => {
387
+ const config = await awaitedConfig;
388
+ await createCacheDir();
389
+ const contents = JSON.stringify(cache, null, 2);
390
+ await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
391
+ });
392
+ await savingQueue;
393
+ return cache;
387
394
  } catch (error) {
388
395
  throw error;
389
396
  }
@@ -1389,7 +1389,7 @@ var require_utils = __commonJS({
1389
1389
  // ../node_modules/.pnpm/expect-type@1.2.2/node_modules/expect-type/dist/index.js
1390
1390
  var require_dist = __commonJS({
1391
1391
  "../node_modules/.pnpm/expect-type@1.2.2/node_modules/expect-type/dist/index.js"(exports) {
1392
- var __createBinding = exports && exports.__createBinding || (Object.create ? function(o, m3, k2, k22) {
1392
+ var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m3, k2, k22) {
1393
1393
  if (k22 === void 0) k22 = k2;
1394
1394
  var desc = Object.getOwnPropertyDescriptor(m3, k2);
1395
1395
  if (!desc || ("get" in desc ? !m3.__esModule : desc.writable || desc.configurable)) {
@@ -1398,10 +1398,10 @@ var require_dist = __commonJS({
1398
1398
  } };
1399
1399
  }
1400
1400
  Object.defineProperty(o, k22, desc);
1401
- } : function(o, m3, k2, k22) {
1401
+ }) : (function(o, m3, k2, k22) {
1402
1402
  if (k22 === void 0) k22 = k2;
1403
1403
  o[k22] = m3[k2];
1404
- });
1404
+ }));
1405
1405
  var __exportStar = exports && exports.__exportStar || function(m3, exports2) {
1406
1406
  for (var p4 in m3) if (p4 !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p4)) __createBinding(exports2, m3, p4);
1407
1407
  };
@@ -13236,7 +13236,7 @@ function findTestFileStackTrace(error, each) {
13236
13236
  globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
13237
13237
  globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
13238
13238
 
13239
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/utils.C8RiOc4B.js
13239
+ // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4_lightningcss@1.30.2_terser@5.44.1/node_modules/vitest/dist/chunks/utils.C8RiOc4B.js
13240
13240
  var NAME_WORKER_STATE = "__vitest_worker__";
13241
13241
  function getWorkerState() {
13242
13242
  const workerState = globalThis[NAME_WORKER_STATE];
@@ -13296,7 +13296,7 @@ async function waitForImportsToResolve() {
13296
13296
  await waitForImportsToResolve();
13297
13297
  }
13298
13298
 
13299
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/_commonjsHelpers.BFTU3MAI.js
13299
+ // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4_lightningcss@1.30.2_terser@5.44.1/node_modules/vitest/dist/chunks/_commonjsHelpers.BFTU3MAI.js
13300
13300
  var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
13301
13301
  function getDefaultExportFromCjs2(x2) {
13302
13302
  return x2 && x2.__esModule && Object.prototype.hasOwnProperty.call(x2, "default") ? x2["default"] : x2;
@@ -15330,7 +15330,7 @@ var SnapshotClient = class {
15330
15330
  }
15331
15331
  };
15332
15332
 
15333
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/date.W2xKR2qe.js
15333
+ // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4_lightningcss@1.30.2_terser@5.44.1/node_modules/vitest/dist/chunks/date.W2xKR2qe.js
15334
15334
  var RealDate = Date;
15335
15335
  var now2 = null;
15336
15336
  var MockDate = class _MockDate extends RealDate {
@@ -15383,7 +15383,7 @@ function resetDate() {
15383
15383
  globalThis.Date = RealDate;
15384
15384
  }
15385
15385
 
15386
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/vi.DgezovHB.js
15386
+ // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4_lightningcss@1.30.2_terser@5.44.1/node_modules/vitest/dist/chunks/vi.DgezovHB.js
15387
15387
  var unsupported = [
15388
15388
  // .poll is meant to retry matchers until they succeed, and
15389
15389
  // snapshots will always succeed as long as the poll method doesn't thow an error
@@ -16212,7 +16212,7 @@ function requireTypeDetect() {
16212
16212
  (function(module, exports) {
16213
16213
  (function(global3, factory) {
16214
16214
  module.exports = factory();
16215
- })(typeDetect, function() {
16215
+ })(typeDetect, (function() {
16216
16216
  var promiseExists = typeof Promise === "function";
16217
16217
  var globalObject = typeof self === "object" ? self : commonjsGlobal;
16218
16218
  var symbolExists = typeof Symbol !== "undefined";
@@ -16321,7 +16321,7 @@ function requireTypeDetect() {
16321
16321
  return Object.prototype.toString.call(obj).slice(toStringLeftSliceLength, toStringRightSliceLength);
16322
16322
  }
16323
16323
  return typeDetect2;
16324
- });
16324
+ }));
16325
16325
  })(typeDetect$1);
16326
16326
  return typeDetect$1.exports;
16327
16327
  }
@@ -18115,7 +18115,7 @@ function getImporter(name) {
18115
18115
  return stack?.file || "";
18116
18116
  }
18117
18117
 
18118
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/index.js
18118
+ // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4_lightningcss@1.30.2_terser@5.44.1/node_modules/vitest/dist/index.js
18119
18119
  __toESM(require_dist(), 1);
18120
18120
 
18121
18121
  // src/flat-template-string.ts
@@ -3,7 +3,7 @@ import './config.mjs';
3
3
  import './constants/default-config.mjs';
4
4
  import 'zod';
5
5
  import './schemas/config.shcema.mjs';
6
+ import './create-cache-id.mjs';
6
7
  import './data/clear-cache.mjs';
7
- import './data/create-cache-id.mjs';
8
8
  import './data/get-cache.mjs';
9
9
  import './flat-template-string.mjs';
package/dist/gringow.mjs CHANGED
@@ -4,7 +4,6 @@ import { z } from 'zod';
4
4
  import fs from 'fs/promises';
5
5
  import { dirname, resolve, parse } from 'path';
6
6
  import { mkdir, stat, statSync, mkdirSync } from 'fs';
7
- import CryptoJS from 'crypto-js';
8
7
  import Anthropic from '@anthropic-ai/sdk';
9
8
  import { GoogleGenerativeAI } from '@google/generative-ai';
10
9
  import { env, pipeline } from '@huggingface/transformers';
@@ -25,6 +24,7 @@ var defaultConfig = {
25
24
  },
26
25
  keys: {},
27
26
  sourceLanguage: "en-US",
27
+ defaultLanguage: "pt-BR",
28
28
  languages: ["pt-BR", "fr-CA"],
29
29
  llm: {
30
30
  choice: "huggingface",
@@ -52,6 +52,7 @@ var configSchema = z.object({
52
52
  geminiApiKey: z.string().optional()
53
53
  }),
54
54
  sourceLanguage: languageSchema,
55
+ defaultLanguage: languageSchema,
55
56
  languages: z.array(languageSchema),
56
57
  llm: z.object({
57
58
  choice: llmOptionsSchema,
@@ -108,9 +109,9 @@ async function loadUserConfig() {
108
109
  var awaitedConfig = loadUserConfig();
109
110
  function defaultPrePrompt(config, extraInstructions) {
110
111
  return `
111
- You are a translation generator.
112
+ You are a ultimate translation generator server api.
112
113
 
113
- Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting outside the JSON**.
114
+ Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
114
115
 
115
116
  Follow these rules strictly:
116
117
 
@@ -120,7 +121,7 @@ Follow these rules strictly:
120
121
  - Apply these creatively but **without altering the meaning or punctuation** of the original text.
121
122
  3. Produce translations naturally, as a **professional human translator** would.
122
123
  4. Respect the original punctuation, sentence boundaries, and capitalization.
123
- 5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
124
+ 5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
124
125
  6. The final output must be a **valid JSON** following this exact schema:
125
126
 
126
127
  {
@@ -159,6 +160,59 @@ function postPrompt(config, prepend, replace) {
159
160
  replace ? String(replace) : defaultPostPrompt()
160
161
  ].join(" ").trim();
161
162
  }
163
+
164
+ // src/data/md5.ts
165
+ var INITIAL_STATE = [1831565813, 461845907, 2246822507, 3266489909];
166
+ var MULTIPLIERS = [668265261, 374761393, 2654435769, 2496678331];
167
+ var AVA_MULT1 = 2146121005;
168
+ var AVA_MULT2 = 2221713035;
169
+ function seedState(input) {
170
+ const state = [...INITIAL_STATE];
171
+ for (const char of input) {
172
+ const codePoint = char.codePointAt(0) ?? 0;
173
+ for (let i = 0; i < state.length; i++) {
174
+ state[i] = Math.imul(state[i] ^ codePoint, MULTIPLIERS[i]);
175
+ state[i] = rotateLeft(state[i], (i + 1) * 3);
176
+ }
177
+ state[0] ^= state[3] >>> 11;
178
+ state[1] ^= state[0] << 7;
179
+ state[2] ^= state[1] >>> 13;
180
+ state[3] ^= state[2] << 5;
181
+ }
182
+ return state;
183
+ }
184
+ function rotateLeft(value, shift) {
185
+ return value << shift | value >>> 32 - shift;
186
+ }
187
+ function avalanche(value) {
188
+ value ^= value >>> 16;
189
+ value = Math.imul(value, AVA_MULT1);
190
+ value ^= value >>> 15;
191
+ value = Math.imul(value, AVA_MULT2);
192
+ value ^= value >>> 16;
193
+ return value >>> 0;
194
+ }
195
+ function toHex(value) {
196
+ return (value >>> 0).toString(16).padStart(8, "0");
197
+ }
198
+ function mixState([h1, h2, h3, h4]) {
199
+ h1 = avalanche(h1 ^ h3);
200
+ h2 = avalanche(h2 ^ h4);
201
+ h3 = avalanche(h3 ^ h1);
202
+ h4 = avalanche(h4 ^ h2);
203
+ return [h1, h2, h3, h4];
204
+ }
205
+ function md5(input) {
206
+ const state = mixState(seedState(input));
207
+ return state.map(toHex).join("");
208
+ }
209
+
210
+ // src/create-cache-id.ts
211
+ function createCacheId(text) {
212
+ const hexHash = md5(text);
213
+ const cacheId = `gid_${hexHash}`;
214
+ return cacheId;
215
+ }
162
216
  var optsArg = (opts) => {
163
217
  if (!opts) {
164
218
  opts = { mode: 511 };
@@ -382,12 +436,17 @@ async function getCache() {
382
436
  throw error;
383
437
  }
384
438
  }
439
+ var savingQueue = Promise.resolve();
385
440
  async function saveCache(cache) {
386
441
  try {
387
- const config = await awaitedConfig;
388
- await createCacheDir();
389
- const contents = JSON.stringify(cache, null, 2);
390
- await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
442
+ savingQueue = savingQueue.then(async () => {
443
+ const config = await awaitedConfig;
444
+ await createCacheDir();
445
+ const contents = JSON.stringify(cache, null, 2);
446
+ await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
447
+ });
448
+ await savingQueue;
449
+ return cache;
391
450
  } catch (error) {
392
451
  throw error;
393
452
  }
@@ -460,11 +519,6 @@ async function checkCache(cacheId) {
460
519
  }
461
520
  return void 0;
462
521
  }
463
- function createCacheId(text) {
464
- const hexHash = CryptoJS.MD5(text).toString();
465
- const cacheId = `gid_${hexHash}`;
466
- return cacheId;
467
- }
468
522
 
469
523
  // src/data/write-cache-to-disk.ts
470
524
  async function writeCacheToDisk(cacheId, cached) {
@@ -537,23 +591,40 @@ function parseAnthropic(unparsed) {
537
591
  const parsed = JSON.parse(unparsed);
538
592
  return parsed;
539
593
  }
540
- async function generateResponse(prompt, temperature = 1) {
541
- const config = await awaitedConfig;
542
- if (!config.keys.geminiApiKey) {
543
- throw new Error("Gemini API key is not set in the config");
544
- }
545
- const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
546
- const model = generativeAIClient.getGenerativeModel({
547
- model: config.llm.model,
548
- generationConfig: {
549
- temperature
594
+ async function generateResponse(prompt, config) {
595
+ try {
596
+ if (!config.keys.geminiApiKey) {
597
+ throw new Error("Gemini API key is not set in the config");
550
598
  }
551
- });
552
- if (config.debug) {
553
- console.log("Gemini model prompt:", JSON.stringify({ prompt }));
599
+ const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
600
+ const model = generativeAIClient.getGenerativeModel({
601
+ model: config.llm.model,
602
+ generationConfig: {
603
+ temperature: config.llm.temperature ?? 0.8
604
+ }
605
+ });
606
+ if (config.debug) {
607
+ console.log("Gemini model prompt:", JSON.stringify({ prompt }));
608
+ }
609
+ const response = await model.generateContent([prompt]);
610
+ return response;
611
+ } catch (err) {
612
+ const error = err;
613
+ console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
614
+ const retryDelay = error?.errorDetails?.find(
615
+ (detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
616
+ )?.retryDelay ?? void 0;
617
+ if (error.status === 429 && retryDelay !== void 0) {
618
+ const sleepTime = config.llm.sleep ?? 3;
619
+ console.warn(
620
+ `Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
621
+ );
622
+ await new Promise((resolve2) => setTimeout(resolve2, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
623
+ return generateResponse(prompt, config);
624
+ }
625
+ console.error("Error generating response from Gemini model", { error });
626
+ throw error;
554
627
  }
555
- const response = await model.generateContent([prompt]);
556
- return response;
557
628
  }
558
629
  async function askGemini(prompt, prePrompt2, postPrompt2) {
559
630
  const config = await awaitedConfig;
@@ -575,13 +646,7 @@ ${prompt}
575
646
  ${postPrompt2}`).trim();
576
647
  let response;
577
648
  for (let i = 0; i < 3; i++) {
578
- const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
579
- console.log(
580
- "maybeResponse:",
581
- "\n\n",
582
- JSON.stringify({ maybeResponse: maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text }),
583
- "\n"
584
- );
649
+ const maybeResponse = await generateResponse(fullPrompt, config);
585
650
  const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
586
651
  if (responseString.startsWith("{") && responseString.endsWith("}")) {
587
652
  response = maybeResponse;
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { awaitedConfig, getConfigSource, isLocalConfigFound } from './config.mjs';
2
+ export { createCacheId } from './create-cache-id.mjs';
2
3
  export { clearCache } from './data/clear-cache.mjs';
3
- export { createCacheId } from './data/create-cache-id.mjs';
4
4
  export { getCache } from './data/get-cache.mjs';
5
5
  export { flatTemplateString } from './flat-template-string.mjs';
6
6
  export { GringowConfig } from './constants/default-config.mjs';
package/dist/index.mjs CHANGED
@@ -4,7 +4,6 @@ import { z } from 'zod';
4
4
  import fs from 'fs/promises';
5
5
  import { dirname, resolve, parse } from 'path';
6
6
  import { mkdir, stat, statSync, mkdirSync } from 'fs';
7
- import CryptoJS from 'crypto-js';
8
7
  import Anthropic from '@anthropic-ai/sdk';
9
8
  import { GoogleGenerativeAI } from '@google/generative-ai';
10
9
  import { env, pipeline } from '@huggingface/transformers';
@@ -25,6 +24,7 @@ var defaultConfig = {
25
24
  },
26
25
  keys: {},
27
26
  sourceLanguage: "en-US",
27
+ defaultLanguage: "pt-BR",
28
28
  languages: ["pt-BR", "fr-CA"],
29
29
  llm: {
30
30
  choice: "huggingface",
@@ -52,6 +52,7 @@ var configSchema = z.object({
52
52
  geminiApiKey: z.string().optional()
53
53
  }),
54
54
  sourceLanguage: languageSchema,
55
+ defaultLanguage: languageSchema,
55
56
  languages: z.array(languageSchema),
56
57
  llm: z.object({
57
58
  choice: llmOptionsSchema,
@@ -114,9 +115,9 @@ async function loadUserConfig() {
114
115
  var awaitedConfig = loadUserConfig();
115
116
  function defaultPrePrompt(config, extraInstructions) {
116
117
  return `
117
- You are a translation generator.
118
+ You are a ultimate translation generator server api.
118
119
 
119
- Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting outside the JSON**.
120
+ Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
120
121
 
121
122
  Follow these rules strictly:
122
123
 
@@ -126,7 +127,7 @@ Follow these rules strictly:
126
127
  - Apply these creatively but **without altering the meaning or punctuation** of the original text.
127
128
  3. Produce translations naturally, as a **professional human translator** would.
128
129
  4. Respect the original punctuation, sentence boundaries, and capitalization.
129
- 5. Consider only the following target languages: ${(config?.languages ? config.languages : []).join(", ")}.
130
+ 5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
130
131
  6. The final output must be a **valid JSON** following this exact schema:
131
132
 
132
133
  {
@@ -165,6 +166,59 @@ function postPrompt(config, prepend, replace) {
165
166
  replace ? String(replace) : defaultPostPrompt()
166
167
  ].join(" ").trim();
167
168
  }
169
+
170
+ // src/data/md5.ts
171
+ var INITIAL_STATE = [1831565813, 461845907, 2246822507, 3266489909];
172
+ var MULTIPLIERS = [668265261, 374761393, 2654435769, 2496678331];
173
+ var AVA_MULT1 = 2146121005;
174
+ var AVA_MULT2 = 2221713035;
175
+ function seedState(input) {
176
+ const state = [...INITIAL_STATE];
177
+ for (const char of input) {
178
+ const codePoint = char.codePointAt(0) ?? 0;
179
+ for (let i = 0; i < state.length; i++) {
180
+ state[i] = Math.imul(state[i] ^ codePoint, MULTIPLIERS[i]);
181
+ state[i] = rotateLeft(state[i], (i + 1) * 3);
182
+ }
183
+ state[0] ^= state[3] >>> 11;
184
+ state[1] ^= state[0] << 7;
185
+ state[2] ^= state[1] >>> 13;
186
+ state[3] ^= state[2] << 5;
187
+ }
188
+ return state;
189
+ }
190
+ function rotateLeft(value, shift) {
191
+ return value << shift | value >>> 32 - shift;
192
+ }
193
+ function avalanche(value) {
194
+ value ^= value >>> 16;
195
+ value = Math.imul(value, AVA_MULT1);
196
+ value ^= value >>> 15;
197
+ value = Math.imul(value, AVA_MULT2);
198
+ value ^= value >>> 16;
199
+ return value >>> 0;
200
+ }
201
+ function toHex(value) {
202
+ return (value >>> 0).toString(16).padStart(8, "0");
203
+ }
204
+ function mixState([h1, h2, h3, h4]) {
205
+ h1 = avalanche(h1 ^ h3);
206
+ h2 = avalanche(h2 ^ h4);
207
+ h3 = avalanche(h3 ^ h1);
208
+ h4 = avalanche(h4 ^ h2);
209
+ return [h1, h2, h3, h4];
210
+ }
211
+ function md5(input) {
212
+ const state = mixState(seedState(input));
213
+ return state.map(toHex).join("");
214
+ }
215
+
216
+ // src/create-cache-id.ts
217
+ function createCacheId(text) {
218
+ const hexHash = md5(text);
219
+ const cacheId = `gid_${hexHash}`;
220
+ return cacheId;
221
+ }
168
222
  var optsArg = (opts) => {
169
223
  if (!opts) {
170
224
  opts = { mode: 511 };
@@ -388,12 +442,17 @@ async function getCache() {
388
442
  throw error;
389
443
  }
390
444
  }
445
+ var savingQueue = Promise.resolve();
391
446
  async function saveCache(cache) {
392
447
  try {
393
- const config = await awaitedConfig;
394
- await createCacheDir();
395
- const contents = JSON.stringify(cache, null, 2);
396
- await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
448
+ savingQueue = savingQueue.then(async () => {
449
+ const config = await awaitedConfig;
450
+ await createCacheDir();
451
+ const contents = JSON.stringify(cache, null, 2);
452
+ await fs.writeFile(`${config.cache.dir}/${config.cache.file}`, contents);
453
+ });
454
+ await savingQueue;
455
+ return cache;
397
456
  } catch (error) {
398
457
  throw error;
399
458
  }
@@ -471,11 +530,6 @@ async function checkCache(cacheId) {
471
530
  async function clearCache() {
472
531
  await saveCache({});
473
532
  }
474
- function createCacheId(text) {
475
- const hexHash = CryptoJS.MD5(text).toString();
476
- const cacheId = `gid_${hexHash}`;
477
- return cacheId;
478
- }
479
533
 
480
534
  // src/data/delete-cache-item.ts
481
535
  async function deleteCacheItem(cacheId) {
@@ -559,23 +613,40 @@ function parseAnthropic(unparsed) {
559
613
  const parsed = JSON.parse(unparsed);
560
614
  return parsed;
561
615
  }
562
- async function generateResponse(prompt, temperature = 1) {
563
- const config = await awaitedConfig;
564
- if (!config.keys.geminiApiKey) {
565
- throw new Error("Gemini API key is not set in the config");
566
- }
567
- const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
568
- const model = generativeAIClient.getGenerativeModel({
569
- model: config.llm.model,
570
- generationConfig: {
571
- temperature
616
+ async function generateResponse(prompt, config) {
617
+ try {
618
+ if (!config.keys.geminiApiKey) {
619
+ throw new Error("Gemini API key is not set in the config");
572
620
  }
573
- });
574
- if (config.debug) {
575
- console.log("Gemini model prompt:", JSON.stringify({ prompt }));
621
+ const generativeAIClient = new GoogleGenerativeAI(config.keys.geminiApiKey);
622
+ const model = generativeAIClient.getGenerativeModel({
623
+ model: config.llm.model,
624
+ generationConfig: {
625
+ temperature: config.llm.temperature ?? 0.8
626
+ }
627
+ });
628
+ if (config.debug) {
629
+ console.log("Gemini model prompt:", JSON.stringify({ prompt }));
630
+ }
631
+ const response = await model.generateContent([prompt]);
632
+ return response;
633
+ } catch (err) {
634
+ const error = err;
635
+ console.log("Error details from Gemini model", JSON.stringify({ error }, null, 2));
636
+ const retryDelay = error?.errorDetails?.find(
637
+ (detail) => detail?.["@type"] === "type.googleapis.com/google.rpc.RetryInfo"
638
+ )?.retryDelay ?? void 0;
639
+ if (error.status === 429 && retryDelay !== void 0) {
640
+ const sleepTime = config.llm.sleep ?? 3;
641
+ console.warn(
642
+ `Rate limit exceeded when generating response from Gemini model, retrying in... ${retryDelay} + ${sleepTime / 1e3}s`
643
+ );
644
+ await new Promise((resolve2) => setTimeout(resolve2, (parseInt(retryDelay, 10) + sleepTime / 1e3) * 1e3));
645
+ return generateResponse(prompt, config);
646
+ }
647
+ console.error("Error generating response from Gemini model", { error });
648
+ throw error;
576
649
  }
577
- const response = await model.generateContent([prompt]);
578
- return response;
579
650
  }
580
651
  async function askGemini(prompt, prePrompt2, postPrompt2) {
581
652
  const config = await awaitedConfig;
@@ -597,13 +668,7 @@ ${prompt}
597
668
  ${postPrompt2}`).trim();
598
669
  let response;
599
670
  for (let i = 0; i < 3; i++) {
600
- const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
601
- console.log(
602
- "maybeResponse:",
603
- "\n\n",
604
- JSON.stringify({ maybeResponse: maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text }),
605
- "\n"
606
- );
671
+ const maybeResponse = await generateResponse(fullPrompt, config);
607
672
  const responseString = String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").replace(/```json/g, "").replace(/```/g, "").trim();
608
673
  if (responseString.startsWith("{") && responseString.endsWith("}")) {
609
674
  response = maybeResponse;
@@ -0,0 +1,3 @@
1
+ declare function interpolateTemplate(template: string, values: unknown[]): string;
2
+
3
+ export { interpolateTemplate };