@gringow/gringow 0.6.1 → 0.6.2

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 (44) hide show
  1. package/dist/browser.d.mts +1 -1
  2. package/dist/browser.mjs +22 -22
  3. package/dist/config.d.mts +30 -3
  4. package/dist/config.mjs +12 -22
  5. package/dist/constants/default-config.mjs +2 -2
  6. package/dist/create-cache-id.d.mts +2 -2
  7. package/dist/create-cache-id.mjs +22 -21
  8. package/dist/data/cache-file-path.d.mts +13 -0
  9. package/dist/data/cache-file-path.mjs +6 -0
  10. package/dist/data/cache-queue.d.mts +35 -0
  11. package/dist/data/cache-queue.mjs +372 -0
  12. package/dist/data/check-cache.d.mts +17 -8
  13. package/dist/data/check-cache.mjs +53 -87
  14. package/dist/data/clear-cache.mjs +32 -84
  15. package/dist/data/create-cache-dir.d.mts +3 -0
  16. package/dist/data/create-cache-dir.mjs +11 -71
  17. package/dist/data/delete-cache-item.d.mts +11 -8
  18. package/dist/data/delete-cache-item.mjs +51 -103
  19. package/dist/data/get-cache.d.mts +2 -1
  20. package/dist/data/get-cache.mjs +17 -72
  21. package/dist/data/get-or-create-cache.d.mts +16 -8
  22. package/dist/data/get-or-create-cache.mjs +54 -88
  23. package/dist/data/md5.d.mts +2 -5
  24. package/dist/data/md5.mjs +10 -10
  25. package/dist/data/save-cache.d.mts +2 -9
  26. package/dist/data/save-cache.mjs +32 -84
  27. package/dist/data/write-cache-to-disk.d.mts +12 -8
  28. package/dist/data/write-cache-to-disk.mjs +49 -100
  29. package/dist/gringow.d.mts +19 -8
  30. package/dist/gringow.mjs +116 -91
  31. package/dist/index.d.mts +6 -78
  32. package/dist/index.mjs +130 -99
  33. package/dist/interpolate-template.d.mts +7 -0
  34. package/dist/interpolate-template.mjs +11 -11
  35. package/dist/io/ask-llm.d.mts +1 -8
  36. package/dist/io/ask-llm.mjs +32 -78
  37. package/dist/logger.d.mts +18 -0
  38. package/dist/logger.mjs +18 -0
  39. package/dist/schemas/config.shcema.d.mts +5 -1
  40. package/dist/schemas/config.shcema.mjs +3 -3
  41. package/dist/strip-comments.d.mts +1 -9
  42. package/dist/strip-comments.mjs +0 -1
  43. package/dist/types/index.d.mts +22 -1
  44. package/package.json +7 -8
@@ -1,6 +1,6 @@
1
1
  export { createCacheId } from './create-cache-id.mjs';
2
2
  export { interpolateTemplate } from './interpolate-template.mjs';
3
3
 
4
- declare const GRINGOW_IO_URL = "https://gringow.io";
4
+ declare const GRINGOW_IO_URL = "https://gringow.dev";
5
5
 
6
6
  export { GRINGOW_IO_URL };
package/dist/browser.mjs CHANGED
@@ -4,19 +4,19 @@ var MULTIPLIERS = [668265261, 374761393, 2654435769, 2496678331];
4
4
  var AVA_MULT1 = 2146121005;
5
5
  var AVA_MULT2 = 2221713035;
6
6
  function seedState(input) {
7
- const state = [...INITIAL_STATE];
7
+ let [h1, h2, h3, h4] = INITIAL_STATE;
8
8
  for (const char of input) {
9
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;
10
+ h1 = rotateLeft(Math.imul(h1 ^ codePoint, MULTIPLIERS[0]), 3);
11
+ h2 = rotateLeft(Math.imul(h2 ^ codePoint, MULTIPLIERS[1]), 6);
12
+ h3 = rotateLeft(Math.imul(h3 ^ codePoint, MULTIPLIERS[2]), 9);
13
+ h4 = rotateLeft(Math.imul(h4 ^ codePoint, MULTIPLIERS[3]), 12);
14
+ h1 ^= h4 >>> 11;
15
+ h2 ^= h1 << 7;
16
+ h3 ^= h2 >>> 13;
17
+ h4 ^= h3 << 5;
18
18
  }
19
- return state;
19
+ return [h1, h2, h3, h4];
20
20
  }
21
21
  function rotateLeft(value, shift) {
22
22
  return value << shift | value >>> 32 - shift;
@@ -45,23 +45,23 @@ function md5(input) {
45
45
  }
46
46
 
47
47
  // src/interpolate-template.ts
48
+ function formatValue(value, index) {
49
+ return value === null || value === void 0 ? `\${${index}}` : String(value);
50
+ }
48
51
  function ifArray(template, givenValues) {
49
- const strings = [...template.raw];
50
- const hasValues = givenValues !== void 0;
51
- return strings.reduce((acc, item, index) => {
52
- acc += String(item ?? "");
53
- if (index < strings.length - 1) {
54
- acc += hasValues ? String(givenValues[index] ?? "") : `\${${index}}`;
55
- }
56
- return acc;
57
- }, "");
52
+ const strings = template.raw;
53
+ let output = String(strings[0] ?? "");
54
+ for (let index = 1; index < strings.length; index++) {
55
+ output += formatValue(givenValues?.[index - 1], index - 1);
56
+ output += String(strings[index] ?? "");
57
+ }
58
+ return output;
58
59
  }
59
60
  function ifString(template, givenValues) {
60
61
  if (template?.indexOf("${") === -1) return template;
61
62
  let valueIndex = 0;
62
63
  return template.replace(/\$\{[^}]*\}/g, () => {
63
- const value = givenValues?.[valueIndex];
64
- const output = value === null || value === void 0 ? `\${${valueIndex}}` : String(value);
64
+ const output = formatValue(givenValues?.[valueIndex], valueIndex);
65
65
  valueIndex++;
66
66
  return output;
67
67
  });
@@ -81,6 +81,6 @@ function createCacheId(text) {
81
81
  }
82
82
 
83
83
  // src/browser.ts
84
- var GRINGOW_IO_URL = "https://gringow.io";
84
+ var GRINGOW_IO_URL = "https://gringow.dev";
85
85
 
86
86
  export { GRINGOW_IO_URL, createCacheId, interpolateTemplate };
package/dist/config.d.mts CHANGED
@@ -21,9 +21,7 @@ declare function getConfigSource(): string;
21
21
  */
22
22
  declare function loadUserConfig(): Promise<GringowConfig>;
23
23
  /**
24
- * Waits for the user configuration to load.
25
- *
26
- * @returns The user configuration.
24
+ * The user configuration promise, loaded once when this module is imported.
27
25
  */
28
26
  declare const awaitedConfig: Promise<{
29
27
  log: {
@@ -55,9 +53,38 @@ declare const awaitedConfig: Promise<{
55
53
  debug?: boolean | undefined;
56
54
  globby?: string | undefined;
57
55
  }>;
56
+ /**
57
+ * Builds the default pre-prompt for the configured languages.
58
+ *
59
+ * @param config - The resolved configuration.
60
+ * @param extraInstructions - Optional extra instructions appended to the prompt.
61
+ * @returns The default pre-prompt.
62
+ */
58
63
  declare function defaultPrePrompt(config: GringowConfig, extraInstructions?: string): string;
64
+ /**
65
+ * Builds the default post-prompt. It is empty today.
66
+ *
67
+ * @param _config - The resolved configuration.
68
+ * @returns The default post-prompt.
69
+ */
59
70
  declare function defaultPostPrompt(_config: GringowConfig): string;
71
+ /**
72
+ * Builds the pre-prompt: the replacement when given, else the default with extra instructions.
73
+ *
74
+ * @param config - The resolved configuration.
75
+ * @param prepend - Extra instructions for the default pre-prompt.
76
+ * @param replace - A full replacement for the default pre-prompt.
77
+ * @returns The pre-prompt.
78
+ */
60
79
  declare function prePrompt(config: GringowConfig, prepend?: string, replace?: string): string;
80
+ /**
81
+ * Builds the post-prompt: optional [SPECIFIC_INPUTS] block plus the replacement or the default.
82
+ *
83
+ * @param config - The resolved configuration.
84
+ * @param prepend - Text wrapped in a [SPECIFIC_INPUTS] block.
85
+ * @param replace - A full replacement for the default post-prompt.
86
+ * @returns The post-prompt.
87
+ */
61
88
  declare function postPrompt(config: GringowConfig, prepend?: string, replace?: string): string;
62
89
 
63
90
  export { awaitedConfig, defaultPostPrompt, defaultPrePrompt, getConfigSource, isLocalConfigFound, loadUserConfig, postPrompt, prePrompt };
package/dist/config.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { cosmiconfig } from 'cosmiconfig';
2
- import merge from 'lodash.merge';
2
+ import mergeWith from 'lodash.mergewith';
3
3
  import { z } from 'zod';
4
4
 
5
5
  // src/config.ts
@@ -20,9 +20,9 @@ var defaultConfig = {
20
20
  defaultLanguage: "pt-BR",
21
21
  languages: ["pt-BR", "fr-CA"],
22
22
  llm: {
23
- provider: "openai",
23
+ provider: "ollama",
24
24
  apiKey: void 0,
25
- model: "gpt-4o-mini",
25
+ model: "llama3.2",
26
26
  temperature: 1,
27
27
  sleep: 0
28
28
  },
@@ -44,6 +44,7 @@ var llmProviderSchema = z.enum([
44
44
  "azure",
45
45
  "meta"
46
46
  ]);
47
+ var keylessProviders = ["ollama", "lmstudio"];
47
48
  var configSchema = z.object({
48
49
  debug: z.boolean().optional(),
49
50
  log: z.object({
@@ -75,8 +76,7 @@ var configSchema = z.object({
75
76
  globby: z.string().regex(/^\.\/.*/).optional()
76
77
  }).refine(
77
78
  (data) => {
78
- const noApiKeyRequired = ["ollama", "lmstudio"];
79
- if (noApiKeyRequired.includes(data.llm.provider)) {
79
+ if (keylessProviders.includes(data.llm.provider)) {
80
80
  return true;
81
81
  }
82
82
  return !!data.llm.apiKey;
@@ -96,21 +96,14 @@ function isLocalConfigFound() {
96
96
  function getConfigSource() {
97
97
  return result?.filepath || "default";
98
98
  }
99
+ function replaceArrays(_destination, source) {
100
+ if (Array.isArray(source)) return [...source];
101
+ return void 0;
102
+ }
99
103
  async function loadUserConfig() {
100
104
  try {
101
- const mergedConfig = merge({}, defaultConfig, result?.config);
102
- const parsedConfig = await configSchema.parseAsync(mergedConfig);
103
- parsedConfig.llm.prePrompt = prePrompt(
104
- parsedConfig,
105
- parsedConfig.llm.prePromptAppend ?? "",
106
- parsedConfig.llm.prePromptReplace ?? ""
107
- );
108
- parsedConfig.llm.postPrompt = postPrompt(
109
- parsedConfig,
110
- parsedConfig.llm.postPromptPrepend ?? "",
111
- parsedConfig.llm.postPromptReplace ?? ""
112
- );
113
- return parsedConfig;
105
+ const mergedConfig = mergeWith({}, defaultConfig, result?.config, replaceArrays);
106
+ return await configSchema.parseAsync(mergedConfig);
114
107
  } catch (error) {
115
108
  console.warn(`${error?.message}, using default configuration`);
116
109
  return defaultConfig;
@@ -159,10 +152,7 @@ function defaultPostPrompt(_config) {
159
152
  return "";
160
153
  }
161
154
  function prePrompt(config, prepend, replace) {
162
- return [
163
- // ...(append ? ['[SPECIFIC_INPUTS]', String(append), '[/SPECIFIC_INPUTS]'] : []),
164
- replace ? String(replace) : defaultPrePrompt(config, prepend)
165
- ].join(" ").trim();
155
+ return [replace ? String(replace) : defaultPrePrompt(config, prepend)].join(" ").trim();
166
156
  }
167
157
  function postPrompt(config, prepend, replace) {
168
158
  return [
@@ -14,9 +14,9 @@ var defaultConfig = {
14
14
  defaultLanguage: "pt-BR",
15
15
  languages: ["pt-BR", "fr-CA"],
16
16
  llm: {
17
- provider: "openai",
17
+ provider: "ollama",
18
18
  apiKey: void 0,
19
- model: "gpt-4o-mini",
19
+ model: "llama3.2",
20
20
  temperature: 1,
21
21
  sleep: 0
22
22
  },
@@ -1,8 +1,8 @@
1
1
  /**
2
- * Creates a stable cache ID for a given text using a deterministic hash
2
+ * Creates a stable cache ID from the interpolated text.
3
3
  *
4
4
  * @param text - The text string to create a cache ID for
5
- * @returns Deterministic hash of the text as a hex string
5
+ * @returns The `gid_`-prefixed hex hash of the interpolated text
6
6
  *
7
7
  * @example
8
8
  * const id = createCacheId("my text")
@@ -4,19 +4,19 @@ var MULTIPLIERS = [668265261, 374761393, 2654435769, 2496678331];
4
4
  var AVA_MULT1 = 2146121005;
5
5
  var AVA_MULT2 = 2221713035;
6
6
  function seedState(input) {
7
- const state = [...INITIAL_STATE];
7
+ let [h1, h2, h3, h4] = INITIAL_STATE;
8
8
  for (const char of input) {
9
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;
10
+ h1 = rotateLeft(Math.imul(h1 ^ codePoint, MULTIPLIERS[0]), 3);
11
+ h2 = rotateLeft(Math.imul(h2 ^ codePoint, MULTIPLIERS[1]), 6);
12
+ h3 = rotateLeft(Math.imul(h3 ^ codePoint, MULTIPLIERS[2]), 9);
13
+ h4 = rotateLeft(Math.imul(h4 ^ codePoint, MULTIPLIERS[3]), 12);
14
+ h1 ^= h4 >>> 11;
15
+ h2 ^= h1 << 7;
16
+ h3 ^= h2 >>> 13;
17
+ h4 ^= h3 << 5;
18
18
  }
19
- return state;
19
+ return [h1, h2, h3, h4];
20
20
  }
21
21
  function rotateLeft(value, shift) {
22
22
  return value << shift | value >>> 32 - shift;
@@ -45,29 +45,30 @@ function md5(input) {
45
45
  }
46
46
 
47
47
  // src/interpolate-template.ts
48
+ function formatValue(value, index) {
49
+ return value === null || value === void 0 ? `\${${index}}` : String(value);
50
+ }
48
51
  function ifArray(template, givenValues) {
49
- const strings = [...template.raw];
50
- return strings.reduce((acc, item, index) => {
51
- acc += String(item ?? "");
52
- if (index < strings.length - 1) {
53
- acc += `\${${index}}`;
54
- }
55
- return acc;
56
- }, "");
52
+ const strings = template.raw;
53
+ let output = String(strings[0] ?? "");
54
+ for (let index = 1; index < strings.length; index++) {
55
+ output += formatValue(givenValues?.[index - 1], index - 1);
56
+ output += String(strings[index] ?? "");
57
+ }
58
+ return output;
57
59
  }
58
60
  function ifString(template, givenValues) {
59
61
  if (template?.indexOf("${") === -1) return template;
60
62
  let valueIndex = 0;
61
63
  return template.replace(/\$\{[^}]*\}/g, () => {
62
- const value = givenValues?.[valueIndex];
63
- const output = value === null || value === void 0 ? `\${${valueIndex}}` : String(value);
64
+ const output = formatValue(givenValues?.[valueIndex], valueIndex);
64
65
  valueIndex++;
65
66
  return output;
66
67
  });
67
68
  }
68
69
  function interpolateTemplate(template, givenValues) {
69
70
  if (typeof template === "string") return ifString(template, givenValues);
70
- if (Array.isArray(template)) return ifArray(template);
71
+ if (Array.isArray(template)) return ifArray(template, givenValues);
71
72
  throw new Error("Template must be a string or TemplateStringsArray");
72
73
  }
73
74
 
@@ -0,0 +1,13 @@
1
+ import { GringowConfig } from '../constants/default-config.mjs';
2
+ import 'zod';
3
+ import '../schemas/config.shcema.mjs';
4
+
5
+ /**
6
+ * Builds the cache file path from the configured cache directory and file name.
7
+ *
8
+ * @param config - The resolved configuration.
9
+ * @returns The cache file path.
10
+ */
11
+ declare function cacheFilePath(config: GringowConfig): string;
12
+
13
+ export { cacheFilePath };
@@ -0,0 +1,6 @@
1
+ // src/data/cache-file-path.ts
2
+ function cacheFilePath(config) {
3
+ return `${config.cache.dir}/${config.cache.file}`;
4
+ }
5
+
6
+ export { cacheFilePath };
@@ -0,0 +1,35 @@
1
+ import { GringowCache } from '../types/index.mjs';
2
+ import 'zod';
3
+ import '../schemas/config.shcema.mjs';
4
+
5
+ /**
6
+ * Runs a cache task after every task queued before it.
7
+ * A failed task rejects only its own caller; the queue keeps running.
8
+ *
9
+ * @param task - The task to run inside the queue.
10
+ * @returns The task result.
11
+ */
12
+ declare function enqueueCacheTask<T>(task: () => Promise<T>): Promise<T>;
13
+ /**
14
+ * Writes the whole cache to disk. Call it only inside a queued task.
15
+ *
16
+ * @param cache - The cache to write.
17
+ */
18
+ declare function writeCacheFile(cache: GringowCache): Promise<void>;
19
+ /**
20
+ * Parses raw cache contents. Invalid JSON logs a warning and becomes an empty cache,
21
+ * so the next write replaces the corrupted file with valid JSON.
22
+ *
23
+ * @param rawCache - The raw cache file contents.
24
+ * @returns The parsed cache, or an empty cache.
25
+ */
26
+ declare function parseCacheOrEmpty(rawCache: string): Promise<GringowCache>;
27
+ /**
28
+ * Reads the cache from disk, or an empty cache when the file does not exist or has invalid JSON.
29
+ * Call it only inside a queued task.
30
+ *
31
+ * @returns The parsed cache.
32
+ */
33
+ declare function readCacheOrEmpty(): Promise<GringowCache>;
34
+
35
+ export { enqueueCacheTask, parseCacheOrEmpty, readCacheOrEmpty, writeCacheFile };