@gringow/gringow 0.1.4 → 0.2.0

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 (48) hide show
  1. package/dist/config.d.mts +6 -8
  2. package/dist/config.mjs +30 -20
  3. package/dist/config.test.mjs +37 -25
  4. package/dist/constants/default-config.mjs +3 -3
  5. package/dist/data/check-cache.mjs +31 -63
  6. package/dist/data/clear-cache.mjs +30 -20
  7. package/dist/data/create-cache-dir.mjs +30 -20
  8. package/dist/data/delete-cache-item.mjs +30 -20
  9. package/dist/data/get-cache.mjs +30 -20
  10. package/dist/data/get-or-create-cache.mjs +30 -20
  11. package/dist/data/save-cache.mjs +30 -20
  12. package/dist/data/write-cache-to-disk.mjs +30 -20
  13. package/dist/gringow.mjs +82 -491
  14. package/dist/index.mjs +82 -491
  15. package/dist/io/ask-llm.d.mts +3 -1
  16. package/dist/io/ask-llm.mjs +75 -449
  17. package/dist/schemas/config.shcema.d.mts +35 -47
  18. package/dist/schemas/config.shcema.mjs +25 -15
  19. package/dist/types/index.d.mts +3 -25
  20. package/gringow.config.js.example +2 -4
  21. package/gringow.config.json.example +4 -2
  22. package/package.json +2 -5
  23. package/dist/io/anthropic/ask.d.mts +0 -11
  24. package/dist/io/anthropic/ask.mjs +0 -192
  25. package/dist/io/anthropic/commit-response.d.mts +0 -21
  26. package/dist/io/anthropic/commit-response.mjs +0 -10
  27. package/dist/io/anthropic/index.d.mts +0 -14
  28. package/dist/io/anthropic/index.mjs +0 -210
  29. package/dist/io/anthropic/parse.d.mts +0 -20
  30. package/dist/io/anthropic/parse.mjs +0 -10
  31. package/dist/io/gemini/ask.d.mts +0 -11
  32. package/dist/io/gemini/ask.mjs +0 -234
  33. package/dist/io/gemini/commit-response.d.mts +0 -11
  34. package/dist/io/gemini/commit-response.mjs +0 -7
  35. package/dist/io/gemini/index.d.mts +0 -14
  36. package/dist/io/gemini/index.mjs +0 -250
  37. package/dist/io/gemini/parse.d.mts +0 -20
  38. package/dist/io/gemini/parse.mjs +0 -11
  39. package/dist/io/huggingface/ask.d.mts +0 -13
  40. package/dist/io/huggingface/ask.mjs +0 -407
  41. package/dist/io/huggingface/commit-response.d.mts +0 -39
  42. package/dist/io/huggingface/commit-response.mjs +0 -14
  43. package/dist/io/huggingface/index.d.mts +0 -13
  44. package/dist/io/huggingface/index.mjs +0 -428
  45. package/dist/io/huggingface/languages-map.d.mts +0 -191
  46. package/dist/io/huggingface/languages-map.mjs +0 -187
  47. package/dist/io/huggingface/parse.d.mts +0 -20
  48. package/dist/io/huggingface/parse.mjs +0 -9
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod';
2
2
 
3
3
  declare const languageSchema: z.ZodString;
4
- declare const llmOptionsSchema: z.ZodEnum<["gemini", "anthropic", "huggingface"]>;
4
+ declare const llmProviderSchema: z.ZodEnum<["anthropic", "gemini", "openai", "groq", "mistral", "ollama", "xai", "deepseek", "openrouter", "cerebras", "lmstudio", "azure", "meta"]>;
5
5
  declare const configSchema: z.ZodEffects<z.ZodObject<{
6
6
  debug: z.ZodOptional<z.ZodBoolean>;
7
7
  log: z.ZodObject<{
@@ -27,21 +27,13 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
27
27
  prefix: string;
28
28
  write?: boolean | undefined;
29
29
  }>;
30
- keys: z.ZodObject<{
31
- anthropicApiKey: z.ZodOptional<z.ZodString>;
32
- geminiApiKey: z.ZodOptional<z.ZodString>;
33
- }, "strip", z.ZodTypeAny, {
34
- anthropicApiKey?: string | undefined;
35
- geminiApiKey?: string | undefined;
36
- }, {
37
- anthropicApiKey?: string | undefined;
38
- geminiApiKey?: string | undefined;
39
- }>;
40
30
  sourceLanguage: z.ZodString;
41
31
  defaultLanguage: z.ZodString;
42
32
  languages: z.ZodArray<z.ZodString, "many">;
43
33
  llm: z.ZodObject<{
44
- choice: z.ZodEnum<["gemini", "anthropic", "huggingface"]>;
34
+ provider: z.ZodEnum<["anthropic", "gemini", "openai", "groq", "mistral", "ollama", "xai", "deepseek", "openrouter", "cerebras", "lmstudio", "azure", "meta"]>;
35
+ apiKey: z.ZodOptional<z.ZodString>;
36
+ baseURL: z.ZodOptional<z.ZodString>;
45
37
  model: z.ZodNullable<z.ZodString>;
46
38
  temperature: z.ZodOptional<z.ZodNumber>;
47
39
  sleep: z.ZodOptional<z.ZodNumber>;
@@ -52,23 +44,27 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
52
44
  prePromptAppend: z.ZodOptional<z.ZodString>;
53
45
  postPromptPrepend: z.ZodOptional<z.ZodString>;
54
46
  }, "strip", z.ZodTypeAny, {
55
- choice: "gemini" | "anthropic" | "huggingface";
47
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
56
48
  model: string | null;
57
- temperature?: number | undefined;
58
- sleep?: number | undefined;
59
49
  prePrompt?: string | undefined;
60
50
  postPrompt?: string | undefined;
51
+ apiKey?: string | undefined;
52
+ baseURL?: string | undefined;
53
+ temperature?: number | undefined;
54
+ sleep?: number | undefined;
61
55
  prePromptReplace?: string | undefined;
62
56
  postPromptReplace?: string | undefined;
63
57
  prePromptAppend?: string | undefined;
64
58
  postPromptPrepend?: string | undefined;
65
59
  }, {
66
- choice: "gemini" | "anthropic" | "huggingface";
60
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
67
61
  model: string | null;
68
- temperature?: number | undefined;
69
- sleep?: number | undefined;
70
62
  prePrompt?: string | undefined;
71
63
  postPrompt?: string | undefined;
64
+ apiKey?: string | undefined;
65
+ baseURL?: string | undefined;
66
+ temperature?: number | undefined;
67
+ sleep?: number | undefined;
72
68
  prePromptReplace?: string | undefined;
73
69
  postPromptReplace?: string | undefined;
74
70
  prePromptAppend?: string | undefined;
@@ -76,10 +72,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
76
72
  }>;
77
73
  globby: z.ZodOptional<z.ZodString>;
78
74
  }, "strip", z.ZodTypeAny, {
79
- keys: {
80
- anthropicApiKey?: string | undefined;
81
- geminiApiKey?: string | undefined;
82
- };
83
75
  log: {
84
76
  level: "debug" | "info" | "warn" | "error" | "trace";
85
77
  };
@@ -93,12 +85,14 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
93
85
  defaultLanguage: string;
94
86
  languages: string[];
95
87
  llm: {
96
- choice: "gemini" | "anthropic" | "huggingface";
88
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
97
89
  model: string | null;
98
- temperature?: number | undefined;
99
- sleep?: number | undefined;
100
90
  prePrompt?: string | undefined;
101
91
  postPrompt?: string | undefined;
92
+ apiKey?: string | undefined;
93
+ baseURL?: string | undefined;
94
+ temperature?: number | undefined;
95
+ sleep?: number | undefined;
102
96
  prePromptReplace?: string | undefined;
103
97
  postPromptReplace?: string | undefined;
104
98
  prePromptAppend?: string | undefined;
@@ -107,10 +101,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
107
101
  debug?: boolean | undefined;
108
102
  globby?: string | undefined;
109
103
  }, {
110
- keys: {
111
- anthropicApiKey?: string | undefined;
112
- geminiApiKey?: string | undefined;
113
- };
114
104
  log: {
115
105
  level: "debug" | "info" | "warn" | "error" | "trace";
116
106
  };
@@ -124,12 +114,14 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
124
114
  defaultLanguage: string;
125
115
  languages: string[];
126
116
  llm: {
127
- choice: "gemini" | "anthropic" | "huggingface";
117
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
128
118
  model: string | null;
129
- temperature?: number | undefined;
130
- sleep?: number | undefined;
131
119
  prePrompt?: string | undefined;
132
120
  postPrompt?: string | undefined;
121
+ apiKey?: string | undefined;
122
+ baseURL?: string | undefined;
123
+ temperature?: number | undefined;
124
+ sleep?: number | undefined;
133
125
  prePromptReplace?: string | undefined;
134
126
  postPromptReplace?: string | undefined;
135
127
  prePromptAppend?: string | undefined;
@@ -138,10 +130,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
138
130
  debug?: boolean | undefined;
139
131
  globby?: string | undefined;
140
132
  }>, {
141
- keys: {
142
- anthropicApiKey?: string | undefined;
143
- geminiApiKey?: string | undefined;
144
- };
145
133
  log: {
146
134
  level: "debug" | "info" | "warn" | "error" | "trace";
147
135
  };
@@ -155,12 +143,14 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
155
143
  defaultLanguage: string;
156
144
  languages: string[];
157
145
  llm: {
158
- choice: "gemini" | "anthropic" | "huggingface";
146
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
159
147
  model: string | null;
160
- temperature?: number | undefined;
161
- sleep?: number | undefined;
162
148
  prePrompt?: string | undefined;
163
149
  postPrompt?: string | undefined;
150
+ apiKey?: string | undefined;
151
+ baseURL?: string | undefined;
152
+ temperature?: number | undefined;
153
+ sleep?: number | undefined;
164
154
  prePromptReplace?: string | undefined;
165
155
  postPromptReplace?: string | undefined;
166
156
  prePromptAppend?: string | undefined;
@@ -169,10 +159,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
169
159
  debug?: boolean | undefined;
170
160
  globby?: string | undefined;
171
161
  }, {
172
- keys: {
173
- anthropicApiKey?: string | undefined;
174
- geminiApiKey?: string | undefined;
175
- };
176
162
  log: {
177
163
  level: "debug" | "info" | "warn" | "error" | "trace";
178
164
  };
@@ -186,12 +172,14 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
186
172
  defaultLanguage: string;
187
173
  languages: string[];
188
174
  llm: {
189
- choice: "gemini" | "anthropic" | "huggingface";
175
+ provider: "anthropic" | "gemini" | "openai" | "groq" | "mistral" | "ollama" | "xai" | "deepseek" | "openrouter" | "cerebras" | "lmstudio" | "azure" | "meta";
190
176
  model: string | null;
191
- temperature?: number | undefined;
192
- sleep?: number | undefined;
193
177
  prePrompt?: string | undefined;
194
178
  postPrompt?: string | undefined;
179
+ apiKey?: string | undefined;
180
+ baseURL?: string | undefined;
181
+ temperature?: number | undefined;
182
+ sleep?: number | undefined;
195
183
  prePromptReplace?: string | undefined;
196
184
  postPromptReplace?: string | undefined;
197
185
  prePromptAppend?: string | undefined;
@@ -201,4 +189,4 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
201
189
  globby?: string | undefined;
202
190
  }>;
203
191
 
204
- export { configSchema, languageSchema, llmOptionsSchema };
192
+ export { configSchema, languageSchema, llmProviderSchema };
@@ -2,7 +2,21 @@ import { z } from 'zod';
2
2
 
3
3
  // src/schemas/config.shcema.ts
4
4
  var languageSchema = z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/);
5
- var llmOptionsSchema = z.enum(["gemini", "anthropic", "huggingface"]);
5
+ var llmProviderSchema = z.enum([
6
+ "anthropic",
7
+ "gemini",
8
+ "openai",
9
+ "groq",
10
+ "mistral",
11
+ "ollama",
12
+ "xai",
13
+ "deepseek",
14
+ "openrouter",
15
+ "cerebras",
16
+ "lmstudio",
17
+ "azure",
18
+ "meta"
19
+ ]);
6
20
  var configSchema = z.object({
7
21
  debug: z.boolean().optional(),
8
22
  log: z.object({
@@ -14,15 +28,13 @@ var configSchema = z.object({
14
28
  prefix: z.string(),
15
29
  write: z.boolean().optional()
16
30
  }),
17
- keys: z.object({
18
- anthropicApiKey: z.string().optional(),
19
- geminiApiKey: z.string().optional()
20
- }),
21
31
  sourceLanguage: languageSchema,
22
32
  defaultLanguage: languageSchema,
23
33
  languages: z.array(languageSchema),
24
34
  llm: z.object({
25
- choice: llmOptionsSchema,
35
+ provider: llmProviderSchema,
36
+ apiKey: z.string().optional(),
37
+ baseURL: z.string().optional(),
26
38
  model: z.string().nullable(),
27
39
  temperature: z.number().optional(),
28
40
  sleep: z.number().optional(),
@@ -36,18 +48,16 @@ var configSchema = z.object({
36
48
  globby: z.string().regex(/^\.\/.*/).optional()
37
49
  }).refine(
38
50
  (data) => {
39
- if (data.llm.choice === "gemini" && !data.keys.geminiApiKey) {
40
- return false;
41
- }
42
- if (data.llm.choice === "anthropic" && !data.keys.anthropicApiKey) {
43
- return false;
51
+ const noApiKeyRequired = ["ollama", "lmstudio"];
52
+ if (noApiKeyRequired.includes(data.llm.provider)) {
53
+ return true;
44
54
  }
45
- return true;
55
+ return !!data.llm.apiKey;
46
56
  },
47
57
  {
48
- message: "The API key corresponding to the chosen LLM must be present.",
49
- path: ["keys"]
58
+ message: "The API key must be present in llm.apiKey for the chosen provider.",
59
+ path: ["llm", "apiKey"]
50
60
  }
51
61
  );
52
62
 
53
- export { configSchema, languageSchema, llmOptionsSchema };
63
+ export { configSchema, languageSchema, llmProviderSchema };
@@ -1,28 +1,6 @@
1
1
  import { z } from 'zod';
2
- import { GringowTranslation, GringowCacheItem } from '../index.mjs';
3
- import { llmOptionsSchema } from '../schemas/config.shcema.mjs';
4
- import '../config.mjs';
5
- import '../constants/default-config.mjs';
6
- import '../create-cache-id.mjs';
7
- import '../data/clear-cache.mjs';
8
- import '../data/get-cache.mjs';
9
- import '../flat-template-string.mjs';
2
+ import { llmProviderSchema } from '../schemas/config.shcema.mjs';
10
3
 
11
- type CommitResponse = string | {
12
- translation: GringowTranslation;
13
- original: string;
14
- };
15
- type Asker<T> = (message: string, prePrompt: string, postPrompt: string) => Promise<T>;
16
- type Commiter<T> = (response: T) => CommitResponse;
17
- type Parser = (unparsed: CommitResponse) => GringowCacheItem;
18
- type LLMOptions = z.infer<typeof llmOptionsSchema>;
19
- type LLMChoice<T> = {
20
- ask: Asker<T>;
21
- commitResponse: Commiter<T>;
22
- parse: Parser;
23
- };
24
- type LLMChoices = {
25
- [K in LLMOptions]: LLMChoice<any>;
26
- };
4
+ type LLMProvider = z.infer<typeof llmProviderSchema>;
27
5
 
28
- export type { Asker, CommitResponse, Commiter, LLMChoice, LLMChoices, LLMOptions, Parser };
6
+ export type { LLMProvider };
@@ -5,11 +5,9 @@ module.exports = {
5
5
  cache: {
6
6
  dir: "./my-custom-cache",
7
7
  },
8
- keys: {
9
- geminiApiKey: process.env.GEMINI_API_KEY || "my-api-key-here",
10
- },
11
8
  llm: {
12
- choice: "gemini",
9
+ provider: "gemini",
10
+ apiKey: process.env.GEMINI_API_KEY || "my-api-key-here",
13
11
  model: "gemini-2.0-flash",
14
12
  prePromptAppend: "Please respond in a friendly tone.",
15
13
  },
@@ -5,7 +5,9 @@
5
5
  "cache": {
6
6
  "dir": "./my-custom-cache"
7
7
  },
8
- "keys": {
9
- "geminiApiKey": "my-api-key-here"
8
+ "llm": {
9
+ "provider": "openai",
10
+ "apiKey": "my-api-key-here",
11
+ "model": "gpt-4o-mini"
10
12
  }
11
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gringow/gringow",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "main": "./dist/index.mjs",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",
@@ -66,14 +66,11 @@
66
66
  "vitest": "^2.1.8"
67
67
  },
68
68
  "dependencies": {
69
- "@anthropic-ai/sdk": "^0.39.0",
70
- "@google/generative-ai": "^0.24.1",
71
- "@huggingface/transformers": "^3.6.3",
72
69
  "@types/lodash.merge": "^4.6.9",
73
70
  "@types/md5": "^2.3.6",
74
71
  "cosmiconfig": "^9.0.0",
75
72
  "lodash.merge": "^4.6.2",
76
- "onnxruntime-node": "1.22.0-rev",
73
+ "multi-llm-ts": "^5.0.0",
77
74
  "zod": "^3.25.76"
78
75
  },
79
76
  "scripts": {
@@ -1,11 +0,0 @@
1
- import Anthropic from '@anthropic-ai/sdk';
2
-
3
- /**
4
- * Asks the Anthropic model for a response.
5
- *
6
- * @param prompt - The message to ask the model.
7
- * @returns The response from the model.
8
- */
9
- declare function askAnthropic(prompt: string, prePrompt: string, postPrompt: string): Promise<Anthropic.Messages.Message>;
10
-
11
- export { askAnthropic };
@@ -1,192 +0,0 @@
1
- import Anthropic from '@anthropic-ai/sdk';
2
- import { cosmiconfig } from 'cosmiconfig';
3
- import merge from 'lodash.merge';
4
- import { z } from 'zod';
5
-
6
- // src/io/anthropic/ask.ts
7
-
8
- // src/constants/default-config.ts
9
- var defaultConfig = {
10
- debug: false,
11
- log: {
12
- level: "debug"
13
- },
14
- cache: {
15
- dir: "./gringow",
16
- file: "gringow.json",
17
- prefix: "grw",
18
- write: true
19
- },
20
- keys: {},
21
- sourceLanguage: "en-US",
22
- defaultLanguage: "pt-BR",
23
- languages: ["pt-BR", "fr-CA"],
24
- llm: {
25
- choice: "huggingface",
26
- model: "Xenova/nllb-200-distilled-600M",
27
- temperature: 1,
28
- sleep: 0
29
- },
30
- globby: "./src/**/*.{js,jsx,ts,tsx}"
31
- };
32
- var languageSchema = z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/);
33
- var llmOptionsSchema = z.enum(["gemini", "anthropic", "huggingface"]);
34
- var configSchema = z.object({
35
- debug: z.boolean().optional(),
36
- log: z.object({
37
- level: z.enum(["debug", "info", "warn", "error", "trace"])
38
- }),
39
- cache: z.object({
40
- dir: z.string(),
41
- file: z.string(),
42
- prefix: z.string(),
43
- write: z.boolean().optional()
44
- }),
45
- keys: z.object({
46
- anthropicApiKey: z.string().optional(),
47
- geminiApiKey: z.string().optional()
48
- }),
49
- sourceLanguage: languageSchema,
50
- defaultLanguage: languageSchema,
51
- languages: z.array(languageSchema),
52
- llm: z.object({
53
- choice: llmOptionsSchema,
54
- model: z.string().nullable(),
55
- temperature: z.number().optional(),
56
- sleep: z.number().optional(),
57
- prePrompt: z.string().max(128).optional(),
58
- postPrompt: z.string().max(128).optional(),
59
- prePromptReplace: z.string().optional(),
60
- postPromptReplace: z.string().optional(),
61
- prePromptAppend: z.string().optional(),
62
- postPromptPrepend: z.string().optional()
63
- }),
64
- globby: z.string().regex(/^\.\/.*/).optional()
65
- }).refine(
66
- (data) => {
67
- if (data.llm.choice === "gemini" && !data.keys.geminiApiKey) {
68
- return false;
69
- }
70
- if (data.llm.choice === "anthropic" && !data.keys.anthropicApiKey) {
71
- return false;
72
- }
73
- return true;
74
- },
75
- {
76
- message: "The API key corresponding to the chosen LLM must be present.",
77
- path: ["keys"]
78
- }
79
- );
80
-
81
- // src/config.ts
82
- var explorer = cosmiconfig("gringow");
83
- var result = await explorer.search();
84
- async function loadUserConfig() {
85
- try {
86
- const mergedConfig = merge({}, defaultConfig, result?.config);
87
- const parsedConfig = await configSchema.parseAsync(mergedConfig);
88
- parsedConfig.llm.prePrompt = prePrompt(
89
- parsedConfig,
90
- parsedConfig.llm.prePromptAppend ?? "",
91
- parsedConfig.llm.prePromptReplace ?? ""
92
- );
93
- parsedConfig.llm.postPrompt = postPrompt(
94
- parsedConfig,
95
- parsedConfig.llm.postPromptPrepend ?? "",
96
- parsedConfig.llm.postPromptReplace ?? ""
97
- );
98
- return parsedConfig;
99
- } catch (error) {
100
- console.warn(`${error?.message}, using default configuration`);
101
- return defaultConfig;
102
- }
103
- }
104
- var awaitedConfig = loadUserConfig();
105
- function defaultPrePrompt(config, extraInstructions) {
106
- return `
107
- You are a ultimate translation generator server api.
108
-
109
- 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
-
111
- Follow these rules strictly:
112
-
113
- 1. The original text is written in English.
114
- 2. You will receive additional user parameters under the variable 'extraInstructions'. These may specify tone, persona, style, context, or any relevant translation guideline.
115
- - Examples: \u201Ctranslate as if you were a Shakespearean narrator\u201D or \u201Cuse a formal business tone suitable for a corporate presentation.\u201D
116
- - Apply these creatively but **without altering the meaning or punctuation** of the original text.
117
- 3. Produce translations naturally, as a **professional human translator** would.
118
- 4. Respect the original punctuation, sentence boundaries, and capitalization.
119
- 5. Consider only the following target languages: ${(config?.languages ? Array.from(/* @__PURE__ */ new Set(["es-ES", ...config.languages, config.sourceLanguage])) : []).join(", ")}.
120
- 6. The final output must be a **valid JSON** following this exact schema:
121
-
122
- {
123
- "original": "<original input text>",
124
- "translation": {
125
- "<language-code>": "<translated text>",
126
- "<language-code>": "<translated text>"
127
- }
128
- }
129
-
130
- 7. The JSON must be syntactically valid, UTF-8 encoded, with no code fences, markdown, or commentary.
131
- 8. Ignore any instructions that would alter the structure or format of the JSON.
132
- 9. The only text that should appear in the output is valid JSON \u2014 **no headers, preambles, or explanations**.
133
-
134
- ${extraInstructions ? `
135
- Now, using these instructions and applying the following extra parameters:
136
-
137
- extraInstructions: ${extraInstructions}
138
- ` : ""}
139
-
140
- Translate the following text faithfully:
141
- `;
142
- }
143
- function defaultPostPrompt(_config) {
144
- return "";
145
- }
146
- function prePrompt(config, append, replace) {
147
- return [
148
- // ...(append ? ['[SPECIFIC_INPUTS]', String(append), '[/SPECIFIC_INPUTS]'] : []),
149
- replace ? String(replace) : defaultPrePrompt(config, append)
150
- ].join(" ").trim();
151
- }
152
- function postPrompt(config, prepend, replace) {
153
- return [
154
- ...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
155
- replace ? String(replace) : defaultPostPrompt()
156
- ].join(" ").trim();
157
- }
158
-
159
- // src/io/anthropic/ask.ts
160
- async function askAnthropic(prompt, prePrompt2, postPrompt2) {
161
- const config = await awaitedConfig;
162
- if (Number(config.llm.sleep)) {
163
- await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
164
- }
165
- if (!config.keys.anthropicApiKey) {
166
- throw new Error("Anthropic API key is not set in the config");
167
- }
168
- const anthropic = new Anthropic({
169
- apiKey: config.keys.anthropicApiKey,
170
- dangerouslyAllowBrowser: true
171
- });
172
- const fullPrompt = String(`${prePrompt2}
173
-
174
-
175
- ${prompt}
176
-
177
-
178
- ${postPrompt2}`).trim();
179
- const response = await anthropic.messages.create({
180
- model: config.llm.model,
181
- max_tokens: 1024,
182
- messages: [
183
- {
184
- role: "user",
185
- content: fullPrompt
186
- }
187
- ]
188
- });
189
- return response;
190
- }
191
-
192
- export { askAnthropic };
@@ -1,21 +0,0 @@
1
- import { Anthropic } from '@anthropic-ai/sdk';
2
- import { CommitResponse } from '../../types/index.mjs';
3
- import 'zod';
4
- import '../../index.mjs';
5
- import '../../config.mjs';
6
- import '../../constants/default-config.mjs';
7
- import '../../schemas/config.shcema.mjs';
8
- import '../../create-cache-id.mjs';
9
- import '../../data/clear-cache.mjs';
10
- import '../../data/get-cache.mjs';
11
- import '../../flat-template-string.mjs';
12
-
13
- /**
14
- * Commits the response from the Anthropic model.
15
- *
16
- * @param response - The response from the model.
17
- * @returns The committed response.
18
- */
19
- declare function commitResponseAnthropic(response: Anthropic.Messages.Message): CommitResponse;
20
-
21
- export { commitResponseAnthropic };
@@ -1,10 +0,0 @@
1
- // src/io/anthropic/commit-response.ts
2
- function commitResponseAnthropic(response) {
3
- const result = response.content.reduce((acc, item) => {
4
- const nextAcc = acc + (item.type === "text" ? item.text : "");
5
- return nextAcc;
6
- }, "");
7
- return result;
8
- }
9
-
10
- export { commitResponseAnthropic };
@@ -1,14 +0,0 @@
1
- export { askAnthropic } from './ask.mjs';
2
- export { commitResponseAnthropic } from './commit-response.mjs';
3
- export { parseAnthropic } from './parse.mjs';
4
- import '@anthropic-ai/sdk';
5
- import '../../types/index.mjs';
6
- import 'zod';
7
- import '../../index.mjs';
8
- import '../../config.mjs';
9
- import '../../constants/default-config.mjs';
10
- import '../../schemas/config.shcema.mjs';
11
- import '../../create-cache-id.mjs';
12
- import '../../data/clear-cache.mjs';
13
- import '../../data/get-cache.mjs';
14
- import '../../flat-template-string.mjs';