@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
@@ -1392,7 +1392,7 @@ var require_utils = __commonJS({
1392
1392
  // ../node_modules/.pnpm/expect-type@1.2.2/node_modules/expect-type/dist/index.js
1393
1393
  var require_dist = __commonJS({
1394
1394
  "../node_modules/.pnpm/expect-type@1.2.2/node_modules/expect-type/dist/index.js"(exports) {
1395
- var __createBinding = exports && exports.__createBinding || (Object.create ? function(o, m3, k2, k22) {
1395
+ var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m3, k2, k22) {
1396
1396
  if (k22 === void 0) k22 = k2;
1397
1397
  var desc = Object.getOwnPropertyDescriptor(m3, k2);
1398
1398
  if (!desc || ("get" in desc ? !m3.__esModule : desc.writable || desc.configurable)) {
@@ -1401,10 +1401,10 @@ var require_dist = __commonJS({
1401
1401
  } };
1402
1402
  }
1403
1403
  Object.defineProperty(o, k22, desc);
1404
- } : function(o, m3, k2, k22) {
1404
+ }) : (function(o, m3, k2, k22) {
1405
1405
  if (k22 === void 0) k22 = k2;
1406
1406
  o[k22] = m3[k2];
1407
- });
1407
+ }));
1408
1408
  var __exportStar = exports && exports.__exportStar || function(m3, exports2) {
1409
1409
  for (var p4 in m3) if (p4 !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p4)) __createBinding(exports2, m3, p4);
1410
1410
  };
@@ -13239,7 +13239,7 @@ function findTestFileStackTrace(error, each) {
13239
13239
  globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
13240
13240
  globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
13241
13241
 
13242
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/utils.C8RiOc4B.js
13242
+ // ../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
13243
13243
  var NAME_WORKER_STATE = "__vitest_worker__";
13244
13244
  function getWorkerState() {
13245
13245
  const workerState = globalThis[NAME_WORKER_STATE];
@@ -13299,7 +13299,7 @@ async function waitForImportsToResolve() {
13299
13299
  await waitForImportsToResolve();
13300
13300
  }
13301
13301
 
13302
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/_commonjsHelpers.BFTU3MAI.js
13302
+ // ../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
13303
13303
  var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
13304
13304
  function getDefaultExportFromCjs2(x2) {
13305
13305
  return x2 && x2.__esModule && Object.prototype.hasOwnProperty.call(x2, "default") ? x2["default"] : x2;
@@ -15333,7 +15333,7 @@ var SnapshotClient = class {
15333
15333
  }
15334
15334
  };
15335
15335
 
15336
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/date.W2xKR2qe.js
15336
+ // ../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
15337
15337
  var RealDate = Date;
15338
15338
  var now2 = null;
15339
15339
  var MockDate = class _MockDate extends RealDate {
@@ -15386,7 +15386,7 @@ function resetDate() {
15386
15386
  globalThis.Date = RealDate;
15387
15387
  }
15388
15388
 
15389
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/chunks/vi.DgezovHB.js
15389
+ // ../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
15390
15390
  var unsupported = [
15391
15391
  // .poll is meant to retry matchers until they succeed, and
15392
15392
  // snapshots will always succeed as long as the poll method doesn't thow an error
@@ -16215,7 +16215,7 @@ function requireTypeDetect() {
16215
16215
  (function(module, exports) {
16216
16216
  (function(global3, factory) {
16217
16217
  module.exports = factory();
16218
- })(typeDetect, function() {
16218
+ })(typeDetect, (function() {
16219
16219
  var promiseExists = typeof Promise === "function";
16220
16220
  var globalObject = typeof self === "object" ? self : commonjsGlobal;
16221
16221
  var symbolExists = typeof Symbol !== "undefined";
@@ -16324,7 +16324,7 @@ function requireTypeDetect() {
16324
16324
  return Object.prototype.toString.call(obj).slice(toStringLeftSliceLength, toStringRightSliceLength);
16325
16325
  }
16326
16326
  return typeDetect2;
16327
- });
16327
+ }));
16328
16328
  })(typeDetect$1);
16329
16329
  return typeDetect$1.exports;
16330
16330
  }
@@ -18118,7 +18118,7 @@ function getImporter(name) {
18118
18118
  return stack?.file || "";
18119
18119
  }
18120
18120
 
18121
- // ../node_modules/.pnpm/vitest@2.1.9_@types+node@22.16.4/node_modules/vitest/dist/index.js
18121
+ // ../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
18122
18122
  __toESM(require_dist(), 1);
18123
18123
 
18124
18124
  // src/constants/default-config.ts
@@ -18135,6 +18135,7 @@ var defaultConfig = {
18135
18135
  },
18136
18136
  keys: {},
18137
18137
  sourceLanguage: "en-US",
18138
+ defaultLanguage: "pt-BR",
18138
18139
  languages: ["pt-BR", "fr-CA"],
18139
18140
  llm: {
18140
18141
  choice: "huggingface",
@@ -18162,6 +18163,7 @@ var configSchema = z.object({
18162
18163
  geminiApiKey: z.string().optional()
18163
18164
  }),
18164
18165
  sourceLanguage: languageSchema,
18166
+ defaultLanguage: languageSchema,
18165
18167
  languages: z.array(languageSchema),
18166
18168
  llm: z.object({
18167
18169
  choice: llmOptionsSchema,
@@ -18218,9 +18220,9 @@ async function loadUserConfig() {
18218
18220
  loadUserConfig();
18219
18221
  function defaultPrePrompt(config2, extraInstructions) {
18220
18222
  return `
18221
- You are a translation generator.
18223
+ You are a ultimate translation generator server api.
18222
18224
 
18223
- Your task is to output **only one valid JSON object**, with **no text, explanations, or formatting outside the JSON**.
18225
+ Your task is to translate a given text and output **only one valid JSON object**, with **no text, explanations, or formatting than the JSON**.
18224
18226
 
18225
18227
  Follow these rules strictly:
18226
18228
 
@@ -18230,7 +18232,7 @@ Follow these rules strictly:
18230
18232
  - Apply these creatively but **without altering the meaning or punctuation** of the original text.
18231
18233
  3. Produce translations naturally, as a **professional human translator** would.
18232
18234
  4. Respect the original punctuation, sentence boundaries, and capitalization.
18233
- 5. Consider only the following target languages: ${(config2?.languages ? config2.languages : []).join(", ")}.
18235
+ 5. Consider only the following target languages: ${(config2?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config2.languages, config2.sourceLanguage])) : []).join(", ")}.
18234
18236
  6. The final output must be a **valid JSON** following this exact schema:
18235
18237
 
18236
18238
  {
@@ -18273,26 +18275,27 @@ function postPrompt(config2, prepend, replace) {
18273
18275
  // src/config.test.ts
18274
18276
  describe("config", () => {
18275
18277
  describe("defaultPrePrompt", () => {
18276
- it("should generate pre-message prompt with correct languages", () => {
18278
+ it("should describe translation task with configured languages", () => {
18277
18279
  const config2 = {
18278
18280
  ...defaultConfig,
18279
18281
  languages: ["pt-BR", "fr-CA", "es-ES"]
18280
18282
  };
18281
18283
  const result2 = defaultPrePrompt(config2);
18284
+ globalExpect(result2).toContain("You are a translation generator.");
18285
+ globalExpect(result2).toContain("Your task is to output **only one valid JSON object**");
18282
18286
  globalExpect(result2).toContain("pt-BR, fr-CA, es-ES");
18283
- globalExpect(result2).toContain("[PRE-MESSAGE]");
18284
- globalExpect(result2).toContain("[/PRE-MESSAGE]");
18285
- globalExpect(result2).toContain('"original"');
18286
- globalExpect(result2).toContain('"translation"');
18287
+ globalExpect(result2).toContain('"original": "<original input text>"');
18288
+ globalExpect(result2).toContain('"translation": {');
18287
18289
  });
18288
- it("should include JSON example in prompt", () => {
18290
+ it("should include extraInstructions block when provided", () => {
18289
18291
  const config2 = {
18290
18292
  ...defaultConfig,
18291
18293
  languages: ["pt-BR"]
18292
18294
  };
18293
- const result2 = defaultPrePrompt(config2);
18294
- globalExpect(result2).toContain('"en-US": "The quick brown fox jumps over the lazy dog"');
18295
- globalExpect(result2).toContain('"pt-BR": "A r\xE1pida raposa marrom salta sobre o c\xE3o pregui\xE7oso"');
18295
+ const result2 = defaultPrePrompt(config2, "Use pirate voice");
18296
+ globalExpect(result2).toContain("Now, using these instructions and applying the following extra parameters:");
18297
+ globalExpect(result2).toContain("extraInstructions: Use pirate voice");
18298
+ globalExpect(result2).toContain("Translate the following text faithfully:");
18296
18299
  });
18297
18300
  });
18298
18301
  describe("defaultPostPrompt", () => {
@@ -18301,87 +18304,69 @@ describe("config", () => {
18301
18304
  ...defaultConfig
18302
18305
  });
18303
18306
  const result2 = defaultPostPrompt();
18304
- globalExpect(result2).toContain("[POST-MESSAGE]");
18305
- globalExpect(result2).toContain("[/POST-MESSAGE]");
18306
- globalExpect(result2).toContain("DO NOT WRAP IT IN A CODE BLOCK");
18307
- globalExpect(result2).toContain("FIRST CHARACTER MUST BE A {");
18308
- globalExpect(result2).toContain("LAST CHARACTER MUST BE A }");
18307
+ globalExpect(result2).toBe("");
18309
18308
  });
18310
18309
  });
18311
18310
  describe("prePrompt", () => {
18312
18311
  it("should use prePromptReplace when provided", () => {
18313
18312
  const config2 = {
18314
- ...defaultConfig,
18315
- llm: {
18316
- ...defaultConfig.llm}
18313
+ ...defaultConfig
18317
18314
  };
18318
- const result2 = prePrompt(config2);
18315
+ const result2 = prePrompt(config2, void 0, "Custom pre-prompt");
18319
18316
  globalExpect(result2).toBe("Custom pre-prompt");
18320
18317
  });
18321
18318
  it("should use prePromptAppend when provided", () => {
18322
18319
  const config2 = {
18323
- ...defaultConfig,
18324
- llm: {
18325
- ...defaultConfig.llm}
18320
+ ...defaultConfig
18326
18321
  };
18327
- const result2 = prePrompt(config2);
18328
- globalExpect(result2).toContain("Additional instructions");
18329
- globalExpect(result2).toContain("[PRE-MESSAGE]");
18322
+ const result2 = prePrompt(config2, "Additional instructions");
18323
+ globalExpect(result2).toContain("extraInstructions: Additional instructions");
18324
+ globalExpect(result2).toContain("You are a translation generator.");
18330
18325
  });
18331
18326
  it("should use prePromptReplace and prePromptAppend together", () => {
18332
18327
  const config2 = {
18333
- ...defaultConfig,
18334
- llm: {
18335
- ...defaultConfig.llm}
18328
+ ...defaultConfig
18336
18329
  };
18337
- const result2 = prePrompt(config2);
18338
- globalExpect(result2).toBe("Custom pre-prompt Additional instructions");
18330
+ const result2 = prePrompt(config2, "Additional instructions", "Custom pre-prompt");
18331
+ globalExpect(result2).toBe("Custom pre-prompt");
18339
18332
  });
18340
18333
  it("should use default prompt when no customization is provided", () => {
18341
18334
  const config2 = {
18342
18335
  ...defaultConfig
18343
18336
  };
18344
18337
  const result2 = prePrompt(config2);
18345
- globalExpect(result2).toContain("[PRE-MESSAGE]");
18346
- globalExpect(result2).toContain("[/PRE-MESSAGE]");
18338
+ globalExpect(result2).toContain("You are a translation generator.");
18339
+ globalExpect(result2).toContain("Translate the following text faithfully:");
18347
18340
  });
18348
18341
  });
18349
18342
  describe("postPrompt", () => {
18350
18343
  it("should use postPromptReplace when provided", () => {
18351
- ({
18352
- ...defaultConfig,
18353
- llm: {
18354
- ...defaultConfig.llm}
18355
- });
18356
- const result2 = postPrompt();
18344
+ const config2 = {
18345
+ ...defaultConfig
18346
+ };
18347
+ const result2 = postPrompt(config2, void 0, "Custom post-prompt");
18357
18348
  globalExpect(result2).toBe("Custom post-prompt");
18358
18349
  });
18359
18350
  it("should use postPromptPrepend when provided", () => {
18360
- ({
18361
- ...defaultConfig,
18362
- llm: {
18363
- ...defaultConfig.llm}
18364
- });
18365
- const result2 = postPrompt();
18366
- globalExpect(result2).toContain("Additional post instructions");
18367
- globalExpect(result2).toContain("[POST-MESSAGE]");
18351
+ const config2 = {
18352
+ ...defaultConfig
18353
+ };
18354
+ const result2 = postPrompt(config2, "Additional post instructions");
18355
+ globalExpect(result2).toBe("[SPECIFIC_INPUTS] Additional post instructions [/SPECIFIC_INPUTS]");
18368
18356
  });
18369
18357
  it("should use postPromptReplace and postPromptPrepend together", () => {
18370
- ({
18371
- ...defaultConfig,
18372
- llm: {
18373
- ...defaultConfig.llm}
18374
- });
18375
- const result2 = postPrompt();
18376
- globalExpect(result2).toBe("Additional post instructions Custom post-prompt");
18358
+ const config2 = {
18359
+ ...defaultConfig
18360
+ };
18361
+ const result2 = postPrompt(config2, "Additional post instructions", "Custom post-prompt");
18362
+ globalExpect(result2).toBe("[SPECIFIC_INPUTS] Additional post instructions [/SPECIFIC_INPUTS] Custom post-prompt");
18377
18363
  });
18378
18364
  it("should use default prompt when no customization is provided", () => {
18379
18365
  ({
18380
18366
  ...defaultConfig
18381
18367
  });
18382
18368
  const result2 = postPrompt();
18383
- globalExpect(result2).toContain("[POST-MESSAGE]");
18384
- globalExpect(result2).toContain("[/POST-MESSAGE]");
18369
+ globalExpect(result2).toBe("");
18385
18370
  });
18386
18371
  });
18387
18372
  });
@@ -12,6 +12,7 @@ var defaultConfig = {
12
12
  },
13
13
  keys: {},
14
14
  sourceLanguage: "en-US",
15
+ defaultLanguage: "pt-BR",
15
16
  languages: ["pt-BR", "fr-CA"],
16
17
  llm: {
17
18
  choice: "huggingface",
@@ -1,8 +1,8 @@
1
1
  /**
2
- * Creates a unique cache ID for a given text using MD5 hash
2
+ * Creates a stable cache ID for a given text using a deterministic hash
3
3
  *
4
4
  * @param text - The text string to create a cache ID for
5
- * @returns MD5 hash of the text as a hex string
5
+ * @returns Deterministic hash of the text as a hex string
6
6
  *
7
7
  * @example
8
8
  * const id = createCacheId("my text")
@@ -0,0 +1,54 @@
1
+ // src/data/md5.ts
2
+ var INITIAL_STATE = [1831565813, 461845907, 2246822507, 3266489909];
3
+ var MULTIPLIERS = [668265261, 374761393, 2654435769, 2496678331];
4
+ var AVA_MULT1 = 2146121005;
5
+ var AVA_MULT2 = 2221713035;
6
+ function seedState(input) {
7
+ const state = [...INITIAL_STATE];
8
+ for (const char of input) {
9
+ const codePoint = char.codePointAt(0) ?? 0;
10
+ for (let i = 0; i < state.length; i++) {
11
+ state[i] = Math.imul(state[i] ^ codePoint, MULTIPLIERS[i]);
12
+ state[i] = rotateLeft(state[i], (i + 1) * 3);
13
+ }
14
+ state[0] ^= state[3] >>> 11;
15
+ state[1] ^= state[0] << 7;
16
+ state[2] ^= state[1] >>> 13;
17
+ state[3] ^= state[2] << 5;
18
+ }
19
+ return state;
20
+ }
21
+ function rotateLeft(value, shift) {
22
+ return value << shift | value >>> 32 - shift;
23
+ }
24
+ function avalanche(value) {
25
+ value ^= value >>> 16;
26
+ value = Math.imul(value, AVA_MULT1);
27
+ value ^= value >>> 15;
28
+ value = Math.imul(value, AVA_MULT2);
29
+ value ^= value >>> 16;
30
+ return value >>> 0;
31
+ }
32
+ function toHex(value) {
33
+ return (value >>> 0).toString(16).padStart(8, "0");
34
+ }
35
+ function mixState([h1, h2, h3, h4]) {
36
+ h1 = avalanche(h1 ^ h3);
37
+ h2 = avalanche(h2 ^ h4);
38
+ h3 = avalanche(h3 ^ h1);
39
+ h4 = avalanche(h4 ^ h2);
40
+ return [h1, h2, h3, h4];
41
+ }
42
+ function md5(input) {
43
+ const state = mixState(seedState(input));
44
+ return state.map(toHex).join("");
45
+ }
46
+
47
+ // src/create-cache-id.ts
48
+ function createCacheId(text) {
49
+ const hexHash = md5(text);
50
+ const cacheId = `gid_${hexHash}`;
51
+ return cacheId;
52
+ }
53
+
54
+ export { createCacheId };
@@ -0,0 +1,2 @@
1
+
2
+ export { }