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