@lingo.dev/compiler 0.1.1 → 0.1.3
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.
- package/README.md +273 -2
- package/build/metadata/manager.mjs +12 -12
- package/build/metadata/manager.mjs.map +1 -1
- package/build/plugin/build-translator.cjs +3 -3
- package/build/plugin/build-translator.mjs +6 -6
- package/build/plugin/build-translator.mjs.map +1 -1
- package/build/plugin/next.cjs +3 -3
- package/build/plugin/next.d.cts.map +1 -1
- package/build/plugin/next.d.mts.map +1 -1
- package/build/plugin/next.mjs +3 -3
- package/build/plugin/next.mjs.map +1 -1
- package/build/plugin/unplugin.cjs +3 -2
- package/build/plugin/unplugin.d.cts.map +1 -1
- package/build/plugin/unplugin.d.mts.map +1 -1
- package/build/plugin/unplugin.mjs +3 -2
- package/build/plugin/unplugin.mjs.map +1 -1
- package/build/react/server/ServerLingoProvider.d.cts +2 -2
- package/build/react/shared/LocaleSwitcher.d.cts +2 -2
- package/build/translation-server/translation-server.cjs +7 -17
- package/build/translation-server/translation-server.mjs +7 -17
- package/build/translation-server/translation-server.mjs.map +1 -1
- package/build/translators/cache-factory.mjs.map +1 -1
- package/build/translators/lingo/model-factory.cjs +5 -10
- package/build/translators/lingo/model-factory.mjs +5 -10
- package/build/translators/lingo/model-factory.mjs.map +1 -1
- package/build/translators/lingo/provider-details.cjs +69 -0
- package/build/translators/lingo/provider-details.mjs +69 -0
- package/build/translators/lingo/provider-details.mjs.map +1 -0
- package/build/translators/lingo/{service.cjs → translator.cjs} +11 -13
- package/build/translators/lingo/{service.mjs → translator.mjs} +12 -14
- package/build/translators/lingo/translator.mjs.map +1 -0
- package/build/translators/local-cache.mjs +8 -8
- package/build/translators/local-cache.mjs.map +1 -1
- package/build/translators/memory-cache.cjs +47 -0
- package/build/translators/memory-cache.mjs +47 -0
- package/build/translators/memory-cache.mjs.map +1 -0
- package/build/translators/pluralization/service.cjs +19 -44
- package/build/translators/pluralization/service.mjs +19 -44
- package/build/translators/pluralization/service.mjs.map +1 -1
- package/build/translators/pseudotranslator/index.cjs +2 -10
- package/build/translators/pseudotranslator/index.mjs +2 -10
- package/build/translators/pseudotranslator/index.mjs.map +1 -1
- package/build/translators/translation-service.cjs +55 -57
- package/build/translators/translation-service.mjs +55 -57
- package/build/translators/translation-service.mjs.map +1 -1
- package/package.json +7 -7
- package/build/translators/lingo/service.mjs.map +0 -1
- package/build/translators/translator-factory.cjs +0 -49
- package/build/translators/translator-factory.mjs +0 -50
- package/build/translators/translator-factory.mjs.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_provider_details = require('./provider-details.cjs');
|
|
2
3
|
let path = require("path");
|
|
3
4
|
path = require_rolldown_runtime.__toESM(path);
|
|
4
5
|
let _ai_sdk_groq = require("@ai-sdk/groq");
|
|
@@ -121,7 +122,7 @@ function parseModelString(modelString) {
|
|
|
121
122
|
*/
|
|
122
123
|
function validateAndGetApiKeys(config) {
|
|
123
124
|
const keys = {};
|
|
124
|
-
const
|
|
125
|
+
const missingProviders = [];
|
|
125
126
|
let providersToValidate;
|
|
126
127
|
if (config === "lingo.dev") providersToValidate = ["lingo.dev"];
|
|
127
128
|
else {
|
|
@@ -134,19 +135,13 @@ function validateAndGetApiKeys(config) {
|
|
|
134
135
|
}
|
|
135
136
|
for (const provider of providersToValidate) {
|
|
136
137
|
const providerConfig = providerDetails[provider];
|
|
137
|
-
if (!providerConfig) throw new Error(`⚠️
|
|
138
|
+
if (!providerConfig) throw new Error(`⚠️ Unknown provider "${provider}". Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
138
139
|
if (!providerConfig.apiKeyEnvVar) continue;
|
|
139
140
|
const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);
|
|
140
141
|
if (key) keys[provider] = key;
|
|
141
|
-
else
|
|
142
|
-
provider: providerConfig.name,
|
|
143
|
-
envVar: providerConfig.apiKeyEnvVar
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
if (missingKeys.length > 0) {
|
|
147
|
-
const errorLines = missingKeys.map(({ provider, envVar }) => ` - ${provider}: ${envVar}`);
|
|
148
|
-
throw new Error(`⚠️ Missing API keys for configured providers:\n${errorLines.join("\n")}\n\nPlease set the required environment variables.`);
|
|
142
|
+
else missingProviders.push(provider);
|
|
149
143
|
}
|
|
144
|
+
if (missingProviders.length > 0) throw new Error(require_provider_details.formatNoApiKeysError(missingProviders));
|
|
150
145
|
return keys;
|
|
151
146
|
}
|
|
152
147
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { formatNoApiKeysError } from "./provider-details.mjs";
|
|
1
2
|
import * as path$1 from "path";
|
|
2
3
|
import { createGroq } from "@ai-sdk/groq";
|
|
3
4
|
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
@@ -118,7 +119,7 @@ function parseModelString(modelString) {
|
|
|
118
119
|
*/
|
|
119
120
|
function validateAndGetApiKeys(config) {
|
|
120
121
|
const keys = {};
|
|
121
|
-
const
|
|
122
|
+
const missingProviders = [];
|
|
122
123
|
let providersToValidate;
|
|
123
124
|
if (config === "lingo.dev") providersToValidate = ["lingo.dev"];
|
|
124
125
|
else {
|
|
@@ -131,19 +132,13 @@ function validateAndGetApiKeys(config) {
|
|
|
131
132
|
}
|
|
132
133
|
for (const provider of providersToValidate) {
|
|
133
134
|
const providerConfig = providerDetails[provider];
|
|
134
|
-
if (!providerConfig) throw new Error(`⚠️
|
|
135
|
+
if (!providerConfig) throw new Error(`⚠️ Unknown provider "${provider}". Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
135
136
|
if (!providerConfig.apiKeyEnvVar) continue;
|
|
136
137
|
const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);
|
|
137
138
|
if (key) keys[provider] = key;
|
|
138
|
-
else
|
|
139
|
-
provider: providerConfig.name,
|
|
140
|
-
envVar: providerConfig.apiKeyEnvVar
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
if (missingKeys.length > 0) {
|
|
144
|
-
const errorLines = missingKeys.map(({ provider, envVar }) => ` - ${provider}: ${envVar}`);
|
|
145
|
-
throw new Error(`⚠️ Missing API keys for configured providers:\n${errorLines.join("\n")}\n\nPlease set the required environment variables.`);
|
|
139
|
+
else missingProviders.push(provider);
|
|
146
140
|
}
|
|
141
|
+
if (missingProviders.length > 0) throw new Error(formatNoApiKeysError(missingProviders));
|
|
147
142
|
return keys;
|
|
148
143
|
}
|
|
149
144
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-factory.mjs","names":["path","providerDetails: Record<string, ProviderConfig>","keys: ValidatedApiKeys","missingKeys: Array<{ provider: string; envVar: string }>","providersToValidate: string[]"],"sources":["../../../src/translators/lingo/model-factory.ts"],"sourcesContent":["/**\n * Shared utilities for creating AI model instances\n */\n\nimport { createGroq } from \"@ai-sdk/groq\";\nimport { createGoogleGenerativeAI } from \"@ai-sdk/google\";\nimport { createOpenRouter } from \"@openrouter/ai-sdk-provider\";\nimport { ollama } from \"ai-sdk-ollama\";\nimport { createMistral } from \"@ai-sdk/mistral\";\nimport type { LanguageModel } from \"ai\";\nimport * as dotenv from \"dotenv\";\nimport * as path from \"path\";\n\nexport type LocaleModel = {\n provider: string;\n name: string;\n};\n\nexport function getKeyFromEnv(envVarName: string): string | undefined {\n if (process.env[envVarName]) {\n return process.env[envVarName];\n }\n\n const projectRoot = process.cwd();\n\n const result = dotenv.config({\n path: [\n path.resolve(projectRoot, \".env\"),\n path.resolve(projectRoot, \".env.local\"),\n path.resolve(projectRoot, \".env.development\"),\n ],\n });\n\n return result?.parsed?.[envVarName];\n}\n\n/**\n * Pre-validated API keys for all providers\n * Keys are fetched and validated once at initialization\n */\nexport type ValidatedApiKeys = Record<string, string>;\n\n/**\n * Provider configuration including env var names and requirements\n */\ntype ProviderConfig = {\n name: string; // Display name (e.g., \"Groq\", \"Google\")\n apiKeyEnvVar?: string; // Environment variable name (e.g., \"GROQ_API_KEY\")\n apiKeyConfigKey?: string; // Config key if applicable (e.g., \"llm.groqApiKey\")\n getKeyLink: string; // Link to get API key\n docsLink: string; // Link to API docs for troubleshooting\n};\n\nexport const providerDetails: Record<string, ProviderConfig> = {\n groq: {\n name: \"Groq\",\n apiKeyEnvVar: \"GROQ_API_KEY\",\n apiKeyConfigKey: \"llm.groqApiKey\",\n getKeyLink: \"https://groq.com\",\n docsLink: \"https://console.groq.com/docs/errors\",\n },\n google: {\n name: \"Google\",\n apiKeyEnvVar: \"GOOGLE_API_KEY\",\n apiKeyConfigKey: \"llm.googleApiKey\",\n getKeyLink: \"https://ai.google.dev/\",\n docsLink: \"https://ai.google.dev/gemini-api/docs/troubleshooting\",\n },\n openai: {\n name: \"OpenAI\",\n apiKeyEnvVar: \"OPENAI_API_KEY\",\n apiKeyConfigKey: \"llm.openaiApiKey\",\n getKeyLink: \"https://platform.openai.com/account/api-keys\",\n docsLink: \"https://platform.openai.com/docs\",\n },\n anthropic: {\n name: \"Anthropic\",\n apiKeyEnvVar: \"ANTHROPIC_API_KEY\",\n apiKeyConfigKey: \"llm.anthropicApiKey\",\n getKeyLink: \"https://console.anthropic.com/get-api-key\",\n docsLink: \"https://console.anthropic.com/docs\",\n },\n openrouter: {\n name: \"OpenRouter\",\n apiKeyEnvVar: \"OPENROUTER_API_KEY\",\n apiKeyConfigKey: \"llm.openrouterApiKey\",\n getKeyLink: \"https://openrouter.ai\",\n docsLink: \"https://openrouter.ai/docs\",\n },\n ollama: {\n name: \"Ollama\",\n apiKeyEnvVar: undefined, // Ollama doesn't require an API key\n apiKeyConfigKey: undefined, // Ollama doesn't require an API key\n getKeyLink: \"https://ollama.com/download\",\n docsLink: \"https://github.com/ollama/ollama/tree/main/docs\",\n },\n mistral: {\n name: \"Mistral\",\n apiKeyEnvVar: \"MISTRAL_API_KEY\",\n apiKeyConfigKey: \"llm.mistralApiKey\",\n getKeyLink: \"https://console.mistral.ai\",\n docsLink: \"https://docs.mistral.ai\",\n },\n \"lingo.dev\": {\n name: \"Lingo.dev\",\n apiKeyEnvVar: \"LINGODOTDEV_API_KEY\",\n apiKeyConfigKey: \"auth.apiKey\",\n getKeyLink: \"https://lingo.dev\",\n docsLink: \"https://lingo.dev/docs\",\n },\n};\n\n/**\n * Get provider and model for a specific locale pair\n */\nexport function getLocaleModel(\n localeModels: Record<string, string>,\n sourceLocale: string,\n targetLocale: string,\n): LocaleModel | undefined {\n const localeKeys = [\n `${sourceLocale}:${targetLocale}`,\n `*:${targetLocale}`,\n `${sourceLocale}:*`,\n \"*:*\",\n ];\n\n const modelKey = localeKeys.find((key) => key in localeModels);\n if (!modelKey) {\n return undefined;\n }\n\n const value = localeModels[modelKey];\n if (!value) {\n return undefined;\n }\n\n return parseModelString(value);\n}\n\n/**\n * Parse provider and model from model string\n * Format: \"provider:model\" (e.g., \"groq:llama3-8b-8192\")\n *\n * @param modelString Model string to parse\n * @returns Object with provider and model\n * @throws Error if format is invalid\n */\nexport function parseModelString(modelString: string): LocaleModel | undefined {\n // Split on first colon only\n const [provider, name] = modelString.split(\":\", 2);\n\n if (!provider || !name) {\n return undefined;\n }\n\n return { provider, name };\n}\n\n/**\n * Validate and fetch all necessary API keys for the given configuration\n * This should be called once at initialization time\n *\n * @param config Model configuration (\"lingo.dev\" or locale-pair mapping)\n * @returns Validated API keys (provider ID -> API key)\n * @throws Error if required keys are missing\n */\nexport function validateAndGetApiKeys(\n config: \"lingo.dev\" | Record<string, string>,\n): ValidatedApiKeys {\n const keys: ValidatedApiKeys = {};\n const missingKeys: Array<{ provider: string; envVar: string }> = [];\n\n // Determine which providers are configured\n let providersToValidate: string[];\n\n if (config === \"lingo.dev\") {\n // Only need lingo.dev provider\n providersToValidate = [\"lingo.dev\"];\n } else {\n // Extract unique providers from model strings\n const providerSet = new Set<string>();\n Object.values(config).forEach((modelString) => {\n const model = parseModelString(modelString);\n if (model) {\n providerSet.add(model.provider);\n }\n });\n providersToValidate = Array.from(providerSet);\n }\n\n // Validate and fetch keys for each provider\n for (const provider of providersToValidate) {\n const providerConfig = providerDetails[provider];\n\n if (!providerConfig) {\n throw new Error(\n `⚠️ Unknown provider \"${provider}\". Supported providers: ${Object.keys(providerDetails).join(\", \")}`,\n );\n }\n\n // Skip providers that don't require keys (like Ollama)\n if (!providerConfig.apiKeyEnvVar) {\n continue;\n }\n\n const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);\n if (key) {\n keys[provider] = key;\n } else {\n missingKeys.push({\n provider: providerConfig.name,\n envVar: providerConfig.apiKeyEnvVar,\n });\n }\n }\n\n // If any keys are missing, throw with detailed error\n if (missingKeys.length > 0) {\n const errorLines = missingKeys.map(\n ({ provider, envVar }) => ` - ${provider}: ${envVar}`,\n );\n throw new Error(\n `⚠️ Missing API keys for configured providers:\\n${errorLines.join(\"\\n\")}\\n\\nPlease set the required environment variables.`,\n );\n }\n\n return keys;\n}\n\n/**\n * Create AI model instance from provider and model ID\n *\n * @param model Provider name (groq, google, openrouter, ollama, mistral) and model identifier\n * @param validatedKeys Pre-validated API keys from validateAndFetchApiKeys()\n * @returns LanguageModel instance\n * @throws Error if provider is not supported or API key is missing\n */\nexport function createAiModel(\n model: LocaleModel,\n validatedKeys: ValidatedApiKeys,\n): LanguageModel {\n const providerConfig = providerDetails[model.provider];\n\n if (!providerConfig) {\n throw new Error(\n `⚠️ Provider \"${model.provider}\" is not supported. Supported providers: ${Object.keys(providerDetails).join(\", \")}`,\n );\n }\n\n // Get API key if required\n const apiKey = providerConfig.apiKeyEnvVar\n ? validatedKeys[model.provider]\n : undefined;\n\n // Verify key is present for providers that require it\n if (providerConfig.apiKeyEnvVar && !apiKey) {\n throw new Error(\n `⚠️ ${providerConfig.name} API key not found. Please set ${providerConfig.apiKeyEnvVar} environment variable.\\n\\n` +\n `This should not happen if validateAndFetchApiKeys() was called. Please restart the service.`,\n );\n }\n\n // Create the appropriate model instance\n switch (model.provider) {\n case \"groq\":\n return createGroq({ apiKey: apiKey! })(model.name);\n\n case \"google\":\n return createGoogleGenerativeAI({ apiKey: apiKey! })(model.name);\n\n case \"openrouter\":\n return createOpenRouter({ apiKey: apiKey! })(model.name);\n\n case \"ollama\":\n return ollama(model.name);\n\n case \"mistral\":\n return createMistral({ apiKey: apiKey! })(model.name);\n\n default:\n // This should be unreachable due to check above\n throw new Error(`⚠️ Provider \"${model.provider}\" is not implemented`);\n }\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAgB,cAAc,YAAwC;AACpE,KAAI,QAAQ,IAAI,YACd,QAAO,QAAQ,IAAI;CAGrB,MAAM,cAAc,QAAQ,KAAK;AAUjC,QARe,OAAO,OAAO,EAC3B,MAAM;EACJA,OAAK,QAAQ,aAAa,OAAO;EACjCA,OAAK,QAAQ,aAAa,aAAa;EACvCA,OAAK,QAAQ,aAAa,mBAAmB;EAC9C,EACF,CAAC,EAEa,SAAS;;AAoB1B,MAAaC,kBAAkD;CAC7D,MAAM;EACJ,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,WAAW;EACT,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,YAAY;EACV,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,SAAS;EACP,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,aAAa;EACX,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACF;;;;AAKD,SAAgB,eACd,cACA,cACA,cACyB;CA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AAGH,QAAO;;;;;;;;;;AAWT,SAAgB,cACd,OACA,eACe;CACf,MAAM,iBAAiB,gBAAgB,MAAM;AAE7C,KAAI,CAAC,eACH,OAAM,IAAI,MACR,iBAAiB,MAAM,SAAS,2CAA2C,OAAO,KAAK,gBAAgB,CAAC,KAAK,KAAK,GACnH;CAIH,MAAM,SAAS,eAAe,eAC1B,cAAc,MAAM,YACpB;AAGJ,KAAI,eAAe,gBAAgB,CAAC,OAClC,OAAM,IAAI,MACR,OAAO,eAAe,KAAK,iCAAiC,eAAe,aAAa,uHAEzF;AAIH,SAAQ,MAAM,UAAd;EACE,KAAK,OACH,QAAO,WAAW,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAEpD,KAAK,SACH,QAAO,yBAAyB,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAElE,KAAK,aACH,QAAO,iBAAiB,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAE1D,KAAK,SACH,QAAO,OAAO,MAAM,KAAK;EAE3B,KAAK,UACH,QAAO,cAAc,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAEvD,QAEE,OAAM,IAAI,MAAM,iBAAiB,MAAM,SAAS,sBAAsB"}
|
|
1
|
+
{"version":3,"file":"model-factory.mjs","names":["path","providerDetails: Record<string, ProviderConfig>","keys: ValidatedApiKeys","missingProviders: string[]","providersToValidate: string[]"],"sources":["../../../src/translators/lingo/model-factory.ts"],"sourcesContent":["/**\n * Shared utilities for creating AI model instances\n */\n\nimport { createGroq } from \"@ai-sdk/groq\";\nimport { createGoogleGenerativeAI } from \"@ai-sdk/google\";\nimport { createOpenRouter } from \"@openrouter/ai-sdk-provider\";\nimport { ollama } from \"ai-sdk-ollama\";\nimport { createMistral } from \"@ai-sdk/mistral\";\nimport type { LanguageModel } from \"ai\";\nimport * as dotenv from \"dotenv\";\nimport * as path from \"path\";\nimport { formatNoApiKeysError } from \"./provider-details\";\n\nexport type LocaleModel = {\n provider: string;\n name: string;\n};\n\nexport function getKeyFromEnv(envVarName: string): string | undefined {\n if (process.env[envVarName]) {\n return process.env[envVarName];\n }\n\n const projectRoot = process.cwd();\n\n const result = dotenv.config({\n path: [\n path.resolve(projectRoot, \".env\"),\n path.resolve(projectRoot, \".env.local\"),\n path.resolve(projectRoot, \".env.development\"),\n ],\n });\n\n return result?.parsed?.[envVarName];\n}\n\n/**\n * Pre-validated API keys for all providers\n * Keys are fetched and validated once at initialization\n */\nexport type ValidatedApiKeys = Record<string, string>;\n\n/**\n * Provider configuration including env var names and requirements\n */\ntype ProviderConfig = {\n name: string; // Display name (e.g., \"Groq\", \"Google\")\n apiKeyEnvVar?: string; // Environment variable name (e.g., \"GROQ_API_KEY\")\n apiKeyConfigKey?: string; // Config key if applicable (e.g., \"llm.groqApiKey\")\n getKeyLink: string; // Link to get API key\n docsLink: string; // Link to API docs for troubleshooting\n};\n\nexport const providerDetails: Record<string, ProviderConfig> = {\n groq: {\n name: \"Groq\",\n apiKeyEnvVar: \"GROQ_API_KEY\",\n apiKeyConfigKey: \"llm.groqApiKey\",\n getKeyLink: \"https://groq.com\",\n docsLink: \"https://console.groq.com/docs/errors\",\n },\n google: {\n name: \"Google\",\n apiKeyEnvVar: \"GOOGLE_API_KEY\",\n apiKeyConfigKey: \"llm.googleApiKey\",\n getKeyLink: \"https://ai.google.dev/\",\n docsLink: \"https://ai.google.dev/gemini-api/docs/troubleshooting\",\n },\n openai: {\n name: \"OpenAI\",\n apiKeyEnvVar: \"OPENAI_API_KEY\",\n apiKeyConfigKey: \"llm.openaiApiKey\",\n getKeyLink: \"https://platform.openai.com/account/api-keys\",\n docsLink: \"https://platform.openai.com/docs\",\n },\n anthropic: {\n name: \"Anthropic\",\n apiKeyEnvVar: \"ANTHROPIC_API_KEY\",\n apiKeyConfigKey: \"llm.anthropicApiKey\",\n getKeyLink: \"https://console.anthropic.com/get-api-key\",\n docsLink: \"https://console.anthropic.com/docs\",\n },\n openrouter: {\n name: \"OpenRouter\",\n apiKeyEnvVar: \"OPENROUTER_API_KEY\",\n apiKeyConfigKey: \"llm.openrouterApiKey\",\n getKeyLink: \"https://openrouter.ai\",\n docsLink: \"https://openrouter.ai/docs\",\n },\n ollama: {\n name: \"Ollama\",\n apiKeyEnvVar: undefined, // Ollama doesn't require an API key\n apiKeyConfigKey: undefined, // Ollama doesn't require an API key\n getKeyLink: \"https://ollama.com/download\",\n docsLink: \"https://github.com/ollama/ollama/tree/main/docs\",\n },\n mistral: {\n name: \"Mistral\",\n apiKeyEnvVar: \"MISTRAL_API_KEY\",\n apiKeyConfigKey: \"llm.mistralApiKey\",\n getKeyLink: \"https://console.mistral.ai\",\n docsLink: \"https://docs.mistral.ai\",\n },\n \"lingo.dev\": {\n name: \"Lingo.dev\",\n apiKeyEnvVar: \"LINGODOTDEV_API_KEY\",\n apiKeyConfigKey: \"auth.apiKey\",\n getKeyLink: \"https://lingo.dev\",\n docsLink: \"https://lingo.dev/docs\",\n },\n};\n\n/**\n * Get provider and model for a specific locale pair\n */\nexport function getLocaleModel(\n localeModels: Record<string, string>,\n sourceLocale: string,\n targetLocale: string,\n): LocaleModel | undefined {\n const localeKeys = [\n `${sourceLocale}:${targetLocale}`,\n `*:${targetLocale}`,\n `${sourceLocale}:*`,\n \"*:*\",\n ];\n\n const modelKey = localeKeys.find((key) => key in localeModels);\n if (!modelKey) {\n return undefined;\n }\n\n const value = localeModels[modelKey];\n if (!value) {\n return undefined;\n }\n\n return parseModelString(value);\n}\n\n/**\n * Parse provider and model from model string\n * Format: \"provider:model\" (e.g., \"groq:llama3-8b-8192\")\n *\n * @param modelString Model string to parse\n * @returns Object with provider and model\n * @throws Error if format is invalid\n */\nexport function parseModelString(modelString: string): LocaleModel | undefined {\n // Split on first colon only\n const [provider, name] = modelString.split(\":\", 2);\n\n if (!provider || !name) {\n return undefined;\n }\n\n return { provider, name };\n}\n\n/**\n * Validate and fetch all necessary API keys for the given configuration\n * This should be called once at initialization time\n *\n * @param config Model configuration (\"lingo.dev\" or locale-pair mapping)\n * @returns Validated API keys (provider ID -> API key)\n * @throws Error if required keys are missing\n */\nexport function validateAndGetApiKeys(\n config: \"lingo.dev\" | Record<string, string>,\n): ValidatedApiKeys {\n const keys: ValidatedApiKeys = {};\n const missingProviders: string[] = [];\n\n // Determine which providers are configured\n let providersToValidate: string[];\n\n if (config === \"lingo.dev\") {\n // Only need lingo.dev provider\n providersToValidate = [\"lingo.dev\"];\n } else {\n // Extract unique providers from model strings\n const providerSet = new Set<string>();\n Object.values(config).forEach((modelString) => {\n const model = parseModelString(modelString);\n if (model) {\n providerSet.add(model.provider);\n }\n });\n providersToValidate = Array.from(providerSet);\n }\n\n // Validate and fetch keys for each provider\n for (const provider of providersToValidate) {\n const providerConfig = providerDetails[provider];\n\n if (!providerConfig) {\n throw new Error(\n `⚠️ Unknown provider \"${provider}\". Supported providers: ${Object.keys(providerDetails).join(\", \")}`,\n );\n }\n\n // Skip providers that don't require keys (like Ollama)\n if (!providerConfig.apiKeyEnvVar) {\n continue;\n }\n\n const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);\n if (key) {\n keys[provider] = key;\n } else {\n missingProviders.push(provider);\n }\n }\n\n // If any keys are missing, throw with detailed error\n if (missingProviders.length > 0) {\n throw new Error(formatNoApiKeysError(missingProviders));\n }\n\n return keys;\n}\n\n/**\n * Create AI model instance from provider and model ID\n *\n * @param model Provider name (groq, google, openrouter, ollama, mistral) and model identifier\n * @param validatedKeys Pre-validated API keys from validateAndFetchApiKeys()\n * @returns LanguageModel instance\n * @throws Error if provider is not supported or API key is missing\n */\nexport function createAiModel(\n model: LocaleModel,\n validatedKeys: ValidatedApiKeys,\n): LanguageModel {\n const providerConfig = providerDetails[model.provider];\n\n if (!providerConfig) {\n throw new Error(\n `⚠️ Provider \"${model.provider}\" is not supported. Supported providers: ${Object.keys(providerDetails).join(\", \")}`,\n );\n }\n\n // Get API key if required\n const apiKey = providerConfig.apiKeyEnvVar\n ? validatedKeys[model.provider]\n : undefined;\n\n // TODO (AleksandrSl 25/12/2025): Do we really need to make a second check? Maybe creation should be combined with validation.\n // Verify key is present for providers that require it\n if (providerConfig.apiKeyEnvVar && !apiKey) {\n throw new Error(\n `⚠️ ${providerConfig.name} API key not found. Please set ${providerConfig.apiKeyEnvVar} environment variable.\\n\\n` +\n `This should not happen if validateAndFetchApiKeys() was called. Please restart the service.`,\n );\n }\n\n // Create the appropriate model instance\n switch (model.provider) {\n case \"groq\":\n return createGroq({ apiKey: apiKey! })(model.name);\n\n case \"google\":\n return createGoogleGenerativeAI({ apiKey: apiKey! })(model.name);\n\n case \"openrouter\":\n return createOpenRouter({ apiKey: apiKey! })(model.name);\n\n case \"ollama\":\n return ollama(model.name);\n\n case \"mistral\":\n return createMistral({ apiKey: apiKey! })(model.name);\n\n default:\n // This should be unreachable due to check above\n throw new Error(`⚠️ Provider \"${model.provider}\" is not implemented`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,cAAc,YAAwC;AACpE,KAAI,QAAQ,IAAI,YACd,QAAO,QAAQ,IAAI;CAGrB,MAAM,cAAc,QAAQ,KAAK;AAUjC,QARe,OAAO,OAAO,EAC3B,MAAM;EACJA,OAAK,QAAQ,aAAa,OAAO;EACjCA,OAAK,QAAQ,aAAa,aAAa;EACvCA,OAAK,QAAQ,aAAa,mBAAmB;EAC9C,EACF,CAAC,EAEa,SAAS;;AAoB1B,MAAaC,kBAAkD;CAC7D,MAAM;EACJ,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,WAAW;EACT,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,YAAY;EACV,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UA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eAAe,aAAa;AACtD,MAAI,IACF,MAAK,YAAY;MAEjB,kBAAiB,KAAK,SAAS;;AAKnC,KAAI,iBAAiB,SAAS,EAC5B,OAAM,IAAI,MAAM,qBAAqB,iBAAiB,CAAC;AAGzD,QAAO;;;;;;;;;;AAWT,SAAgB,cACd,OACA,eACe;CACf,MAAM,iBAAiB,gBAAgB,MAAM;AAE7C,KAAI,CAAC,eACH,OAAM,IAAI,MACR,iBAAiB,MAAM,SAAS,2CAA2C,OAAO,KAAK,gBAAgB,CAAC,KAAK,KAAK,GACnH;CAIH,MAAM,SAAS,eAAe,eAC1B,cAAc,MAAM,YACpB;AAIJ,KAAI,eAAe,gBAAgB,CAAC,OAClC,OAAM,IAAI,MACR,OAAO,eAAe,KAAK,iCAAiC,eAAe,aAAa,uHAEzF;AAIH,SAAQ,MAAM,UAAd;EACE,KAAK,OACH,QAAO,WAAW,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAEpD,KAAK,SACH,QAAO,yBAAyB,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAElE,KAAK,aACH,QAAO,iBAAiB,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAE1D,KAAK,SACH,QAAO,OAAO,MAAM,KAAK;EAE3B,KAAK,UACH,QAAO,cAAc,EAAU,QAAS,CAAC,CAAC,MAAM,KAAK;EAEvD,QAEE,OAAM,IAAI,MAAM,iBAAiB,MAAM,SAAS,sBAAsB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/translators/lingo/provider-details.ts
|
|
3
|
+
const providerDetails = {
|
|
4
|
+
groq: {
|
|
5
|
+
name: "Groq",
|
|
6
|
+
apiKeyEnvVar: "GROQ_API_KEY",
|
|
7
|
+
apiKeyConfigKey: "llm.groqApiKey",
|
|
8
|
+
getKeyLink: "https://groq.com",
|
|
9
|
+
docsLink: "https://console.groq.com/docs/errors"
|
|
10
|
+
},
|
|
11
|
+
google: {
|
|
12
|
+
name: "Google",
|
|
13
|
+
apiKeyEnvVar: "GOOGLE_API_KEY",
|
|
14
|
+
apiKeyConfigKey: "llm.googleApiKey",
|
|
15
|
+
getKeyLink: "https://ai.google.dev/",
|
|
16
|
+
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting"
|
|
17
|
+
},
|
|
18
|
+
openrouter: {
|
|
19
|
+
name: "OpenRouter",
|
|
20
|
+
apiKeyEnvVar: "OPENROUTER_API_KEY",
|
|
21
|
+
apiKeyConfigKey: "llm.openrouterApiKey",
|
|
22
|
+
getKeyLink: "https://openrouter.ai",
|
|
23
|
+
docsLink: "https://openrouter.ai/docs"
|
|
24
|
+
},
|
|
25
|
+
ollama: {
|
|
26
|
+
name: "Ollama",
|
|
27
|
+
apiKeyEnvVar: void 0,
|
|
28
|
+
apiKeyConfigKey: void 0,
|
|
29
|
+
getKeyLink: "https://ollama.com/download",
|
|
30
|
+
docsLink: "https://github.com/ollama/ollama/tree/main/docs"
|
|
31
|
+
},
|
|
32
|
+
mistral: {
|
|
33
|
+
name: "Mistral",
|
|
34
|
+
apiKeyEnvVar: "MISTRAL_API_KEY",
|
|
35
|
+
apiKeyConfigKey: "llm.mistralApiKey",
|
|
36
|
+
getKeyLink: "https://console.mistral.ai",
|
|
37
|
+
docsLink: "https://docs.mistral.ai"
|
|
38
|
+
},
|
|
39
|
+
"lingo.dev": {
|
|
40
|
+
name: "Lingo.dev",
|
|
41
|
+
apiKeyEnvVar: "LINGODOTDEV_API_KEY",
|
|
42
|
+
apiKeyConfigKey: "auth.apiKey",
|
|
43
|
+
getKeyLink: "https://lingo.dev",
|
|
44
|
+
docsLink: "https://lingo.dev/docs"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Format error message when API keys are missing for configured providers
|
|
49
|
+
* @param missingProviders List of providers that are missing API keys
|
|
50
|
+
* @param allProviders Optional: list of all configured providers for context
|
|
51
|
+
*/
|
|
52
|
+
function formatNoApiKeysError(missingProviders, allProviders) {
|
|
53
|
+
const lines = [];
|
|
54
|
+
if (missingProviders.length === 0) return "Translation API keys validated successfully.";
|
|
55
|
+
if (allProviders && allProviders.length > missingProviders.length) lines.push(`Missing API keys for ${missingProviders.length} of ${allProviders.length} configured providers.`);
|
|
56
|
+
else lines.push(`Missing API keys for configured translation providers.`);
|
|
57
|
+
lines.push(`Missing API keys for:`);
|
|
58
|
+
for (const providerId of missingProviders) {
|
|
59
|
+
const details = providerDetails[providerId];
|
|
60
|
+
if (details) if (details.apiKeyEnvVar) lines.push(` • ${details.name}: ${details.apiKeyEnvVar} → ${details.getKeyLink}`);
|
|
61
|
+
else lines.push(` • ${details.name}: ${details.getKeyLink}`);
|
|
62
|
+
else lines.push(` • ${providerId}: (unknown provider)`);
|
|
63
|
+
}
|
|
64
|
+
lines.push(``, `👉 Set the required API keys:`, ` 1. Add to .env file (recommended)`, ` 2. Or export in terminal: export API_KEY_NAME=<your-key>`, ``);
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
exports.formatNoApiKeysError = formatNoApiKeysError;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region src/translators/lingo/provider-details.ts
|
|
2
|
+
const providerDetails = {
|
|
3
|
+
groq: {
|
|
4
|
+
name: "Groq",
|
|
5
|
+
apiKeyEnvVar: "GROQ_API_KEY",
|
|
6
|
+
apiKeyConfigKey: "llm.groqApiKey",
|
|
7
|
+
getKeyLink: "https://groq.com",
|
|
8
|
+
docsLink: "https://console.groq.com/docs/errors"
|
|
9
|
+
},
|
|
10
|
+
google: {
|
|
11
|
+
name: "Google",
|
|
12
|
+
apiKeyEnvVar: "GOOGLE_API_KEY",
|
|
13
|
+
apiKeyConfigKey: "llm.googleApiKey",
|
|
14
|
+
getKeyLink: "https://ai.google.dev/",
|
|
15
|
+
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting"
|
|
16
|
+
},
|
|
17
|
+
openrouter: {
|
|
18
|
+
name: "OpenRouter",
|
|
19
|
+
apiKeyEnvVar: "OPENROUTER_API_KEY",
|
|
20
|
+
apiKeyConfigKey: "llm.openrouterApiKey",
|
|
21
|
+
getKeyLink: "https://openrouter.ai",
|
|
22
|
+
docsLink: "https://openrouter.ai/docs"
|
|
23
|
+
},
|
|
24
|
+
ollama: {
|
|
25
|
+
name: "Ollama",
|
|
26
|
+
apiKeyEnvVar: void 0,
|
|
27
|
+
apiKeyConfigKey: void 0,
|
|
28
|
+
getKeyLink: "https://ollama.com/download",
|
|
29
|
+
docsLink: "https://github.com/ollama/ollama/tree/main/docs"
|
|
30
|
+
},
|
|
31
|
+
mistral: {
|
|
32
|
+
name: "Mistral",
|
|
33
|
+
apiKeyEnvVar: "MISTRAL_API_KEY",
|
|
34
|
+
apiKeyConfigKey: "llm.mistralApiKey",
|
|
35
|
+
getKeyLink: "https://console.mistral.ai",
|
|
36
|
+
docsLink: "https://docs.mistral.ai"
|
|
37
|
+
},
|
|
38
|
+
"lingo.dev": {
|
|
39
|
+
name: "Lingo.dev",
|
|
40
|
+
apiKeyEnvVar: "LINGODOTDEV_API_KEY",
|
|
41
|
+
apiKeyConfigKey: "auth.apiKey",
|
|
42
|
+
getKeyLink: "https://lingo.dev",
|
|
43
|
+
docsLink: "https://lingo.dev/docs"
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Format error message when API keys are missing for configured providers
|
|
48
|
+
* @param missingProviders List of providers that are missing API keys
|
|
49
|
+
* @param allProviders Optional: list of all configured providers for context
|
|
50
|
+
*/
|
|
51
|
+
function formatNoApiKeysError(missingProviders, allProviders) {
|
|
52
|
+
const lines = [];
|
|
53
|
+
if (missingProviders.length === 0) return "Translation API keys validated successfully.";
|
|
54
|
+
if (allProviders && allProviders.length > missingProviders.length) lines.push(`Missing API keys for ${missingProviders.length} of ${allProviders.length} configured providers.`);
|
|
55
|
+
else lines.push(`Missing API keys for configured translation providers.`);
|
|
56
|
+
lines.push(`Missing API keys for:`);
|
|
57
|
+
for (const providerId of missingProviders) {
|
|
58
|
+
const details = providerDetails[providerId];
|
|
59
|
+
if (details) if (details.apiKeyEnvVar) lines.push(` • ${details.name}: ${details.apiKeyEnvVar} → ${details.getKeyLink}`);
|
|
60
|
+
else lines.push(` • ${details.name}: ${details.getKeyLink}`);
|
|
61
|
+
else lines.push(` • ${providerId}: (unknown provider)`);
|
|
62
|
+
}
|
|
63
|
+
lines.push(``, `👉 Set the required API keys:`, ` 1. Add to .env file (recommended)`, ` 2. Or export in terminal: export API_KEY_NAME=<your-key>`, ``);
|
|
64
|
+
return lines.join("\n");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { formatNoApiKeysError };
|
|
69
|
+
//# sourceMappingURL=provider-details.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-details.mjs","names":["providerDetails: Record<string, ProviderDetails>","lines: string[]"],"sources":["../../../src/translators/lingo/provider-details.ts"],"sourcesContent":["/**\n * Provider details for error messages and documentation links\n */\n\nexport interface ProviderDetails {\n name: string; // Display name (e.g., \"Groq\", \"Google\")\n apiKeyEnvVar?: string; // Environment variable name (e.g., \"GROQ_API_KEY\")\n apiKeyConfigKey?: string; // Config key if applicable (e.g., \"llm.groqApiKey\")\n getKeyLink: string; // Link to get API key\n docsLink: string; // Link to API docs for troubleshooting\n}\n\nexport const providerDetails: Record<string, ProviderDetails> = {\n groq: {\n name: \"Groq\",\n apiKeyEnvVar: \"GROQ_API_KEY\",\n apiKeyConfigKey: \"llm.groqApiKey\",\n getKeyLink: \"https://groq.com\",\n docsLink: \"https://console.groq.com/docs/errors\",\n },\n google: {\n name: \"Google\",\n apiKeyEnvVar: \"GOOGLE_API_KEY\",\n apiKeyConfigKey: \"llm.googleApiKey\",\n getKeyLink: \"https://ai.google.dev/\",\n docsLink: \"https://ai.google.dev/gemini-api/docs/troubleshooting\",\n },\n openrouter: {\n name: \"OpenRouter\",\n apiKeyEnvVar: \"OPENROUTER_API_KEY\",\n apiKeyConfigKey: \"llm.openrouterApiKey\",\n getKeyLink: \"https://openrouter.ai\",\n docsLink: \"https://openrouter.ai/docs\",\n },\n ollama: {\n name: \"Ollama\",\n apiKeyEnvVar: undefined,\n apiKeyConfigKey: undefined,\n getKeyLink: \"https://ollama.com/download\",\n docsLink: \"https://github.com/ollama/ollama/tree/main/docs\",\n },\n mistral: {\n name: \"Mistral\",\n apiKeyEnvVar: \"MISTRAL_API_KEY\",\n apiKeyConfigKey: \"llm.mistralApiKey\",\n getKeyLink: \"https://console.mistral.ai\",\n docsLink: \"https://docs.mistral.ai\",\n },\n \"lingo.dev\": {\n name: \"Lingo.dev\",\n apiKeyEnvVar: \"LINGODOTDEV_API_KEY\",\n apiKeyConfigKey: \"auth.apiKey\",\n getKeyLink: \"https://lingo.dev\",\n docsLink: \"https://lingo.dev/docs\",\n },\n};\n\n/**\n * Format error message when API keys are missing for configured providers\n * @param missingProviders List of providers that are missing API keys\n * @param allProviders Optional: list of all configured providers for context\n */\nexport function formatNoApiKeysError(\n missingProviders: string[],\n allProviders?: string[],\n): string {\n const lines: string[] = [];\n\n if (missingProviders.length === 0) {\n // No missing providers (shouldn't happen, but handle it)\n return \"Translation API keys validated successfully.\";\n }\n\n // Header\n if (allProviders && allProviders.length > missingProviders.length) {\n lines.push(\n `Missing API keys for ${missingProviders.length} of ${allProviders.length} configured providers.`,\n );\n } else {\n lines.push(`Missing API keys for configured translation providers.`);\n }\n\n // List missing providers with their environment variables and links\n lines.push(`Missing API keys for:`);\n for (const providerId of missingProviders) {\n const details = providerDetails[providerId];\n if (details) {\n if (details.apiKeyEnvVar) {\n lines.push(\n ` • ${details.name}: ${details.apiKeyEnvVar} → ${details.getKeyLink}`,\n );\n } else {\n lines.push(` • ${details.name}: ${details.getKeyLink}`);\n }\n } else {\n lines.push(` • ${providerId}: (unknown provider)`);\n }\n }\n\n lines.push(\n ``,\n `👉 Set the required API keys:`,\n ` 1. Add to .env file (recommended)`,\n ` 2. Or export in terminal: export API_KEY_NAME=<your-key>`,\n ``,\n );\n\n return lines.join(\"\\n\");\n}\n"],"mappings":";AAYA,MAAaA,kBAAmD;CAC9D,MAAM;EACJ,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,YAAY;EACV,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,QAAQ;EACN,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,SAAS;EACP,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACD,aAAa;EACX,MAAM;EACN,cAAc;EACd,iBAAiB;EACjB,YAAY;EACZ,UAAU;EACX;CACF;;;;;;AAOD,SAAgB,qBACd,kBACA,cACQ;CACR,MAAMC,QAAkB,EAAE;AAE1B,KAAI,iBAAiB,WAAW,EAE9B,QAAO;AAIT,KAAI,gBAAgB,aAAa,SAAS,iBAAiB,OACzD,OAAM,KACJ,wBAAwB,iBAAiB,OAAO,MAAM,aAAa,OAAO,wBAC3E;KAED,OAAM,KAAK,yDAAyD;AAItE,OAAM,KAAK,wBAAwB;AACnC,MAAK,MAAM,cAAc,kBAAkB;EACzC,MAAM,UAAU,gBAAgB;AAChC,MAAI,QACF,KAAI,QAAQ,aACV,OAAM,KACJ,QAAQ,QAAQ,KAAK,IAAI,QAAQ,aAAa,OAAO,QAAQ,aAC9D;MAED,OAAM,KAAK,QAAQ,QAAQ,KAAK,IAAI,QAAQ,aAAa;MAG3D,OAAM,KAAK,QAAQ,WAAW,sBAAsB;;AAIxD,OAAM,KACJ,IACA,iCACA,wCACA,+DACA,GACD;AAED,QAAO,MAAM,KAAK,KAAK"}
|
|
@@ -8,11 +8,11 @@ const require_timeout = require('../../utils/timeout.cjs');
|
|
|
8
8
|
let ai = require("ai");
|
|
9
9
|
let lingo_dev_sdk = require("lingo.dev/sdk");
|
|
10
10
|
|
|
11
|
-
//#region src/translators/lingo/
|
|
11
|
+
//#region src/translators/lingo/translator.ts
|
|
12
12
|
/**
|
|
13
13
|
* Lingo translator using AI models
|
|
14
14
|
*/
|
|
15
|
-
var
|
|
15
|
+
var LingoTranslator = class {
|
|
16
16
|
validatedKeys;
|
|
17
17
|
constructor(config, logger) {
|
|
18
18
|
this.config = config;
|
|
@@ -25,31 +25,29 @@ var Service = class {
|
|
|
25
25
|
* Translate multiple entries
|
|
26
26
|
*/
|
|
27
27
|
async translate(locale, entriesMap) {
|
|
28
|
-
this.logger.debug(`
|
|
28
|
+
this.logger.debug(`translate() called for ${locale} with ${Object.keys(entriesMap).length} entries`);
|
|
29
29
|
const sourceDictionary = require_api.dictionaryFrom(this.config.sourceLocale, Object.fromEntries(Object.entries(entriesMap).map(([hash, entry]) => [hash, entry.text])));
|
|
30
|
-
this.logger.debug(`
|
|
30
|
+
this.logger.debug(`Created source dictionary with ${Object.keys(sourceDictionary.entries).length} entries`);
|
|
31
31
|
return (await this.translateDictionary(sourceDictionary, locale)).entries || {};
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Translate a complete dictionary
|
|
35
35
|
*/
|
|
36
36
|
async translateDictionary(sourceDictionary, targetLocale) {
|
|
37
|
-
this.logger.debug(`[TRACE-LINGO] Chunking dictionary with ${Object.keys(sourceDictionary.entries).length} entries`);
|
|
38
37
|
const chunks = this.chunkDictionary(sourceDictionary);
|
|
39
|
-
this.logger.debug(`
|
|
38
|
+
this.logger.debug(`Split dictionary with ${Object.keys(sourceDictionary.entries).length} into ${chunks.length} chunks`);
|
|
40
39
|
const translatedChunks = [];
|
|
41
40
|
for (let i = 0; i < chunks.length; i++) {
|
|
42
41
|
const chunk = chunks[i];
|
|
43
|
-
this.logger.debug(`
|
|
42
|
+
this.logger.debug(`Translating chunk ${i + 1}/${chunks.length} with ${Object.keys(chunk.entries).length} entries`);
|
|
44
43
|
const chunkStartTime = performance.now();
|
|
45
44
|
const translatedChunk = await this.translateChunk(chunk, targetLocale);
|
|
46
45
|
const chunkEndTime = performance.now();
|
|
47
|
-
this.logger.debug(`
|
|
46
|
+
this.logger.debug(`Chunk ${i + 1}/${chunks.length} completed in ${(chunkEndTime - chunkStartTime).toFixed(2)}ms`);
|
|
48
47
|
translatedChunks.push(translatedChunk);
|
|
49
48
|
}
|
|
50
|
-
this.logger.debug(`[TRACE-LINGO] All chunks translated, merging results`);
|
|
51
49
|
const result = this.mergeDictionaries(translatedChunks);
|
|
52
|
-
this.logger.debug(`
|
|
50
|
+
this.logger.debug(`Merge completed, final dictionary has ${Object.keys(result.entries).length} entries`);
|
|
53
51
|
return result;
|
|
54
52
|
}
|
|
55
53
|
/**
|
|
@@ -66,7 +64,7 @@ var Service = class {
|
|
|
66
64
|
async translateWithLingoDotDev(sourceDictionary, targetLocale) {
|
|
67
65
|
const apiKey = this.validatedKeys["lingo.dev"];
|
|
68
66
|
if (!apiKey) throw new Error("Internal error: Lingo.dev API key not found after validation. Please restart the service.");
|
|
69
|
-
this.logger.
|
|
67
|
+
this.logger.debug(`Using Lingo.dev Engine to localize from "${this.config.sourceLocale}" to "${targetLocale}"`);
|
|
70
68
|
const engine = new lingo_dev_sdk.LingoDotDevEngine({ apiKey });
|
|
71
69
|
try {
|
|
72
70
|
return await require_timeout.withTimeout(engine.localizeObject(sourceDictionary, {
|
|
@@ -85,7 +83,7 @@ var Service = class {
|
|
|
85
83
|
async translateWithLLM(sourceDictionary, targetLocale) {
|
|
86
84
|
const localeModel = require_model_factory.getLocaleModel(this.config.models, this.config.sourceLocale, targetLocale);
|
|
87
85
|
if (!localeModel) throw new Error(`No model configured for translation from ${this.config.sourceLocale} to ${targetLocale}`);
|
|
88
|
-
this.logger.
|
|
86
|
+
this.logger.debug(`Using LLM ("${localeModel.provider}":"${localeModel.name}") to translate from "${this.config.sourceLocale}" to "${targetLocale}"`);
|
|
89
87
|
const aiModel = require_model_factory.createAiModel(localeModel, this.validatedKeys);
|
|
90
88
|
try {
|
|
91
89
|
return require_parse_xml.parseXmlFromResponseText((await require_timeout.withTimeout((0, ai.generateText)({
|
|
@@ -149,4 +147,4 @@ var Service = class {
|
|
|
149
147
|
};
|
|
150
148
|
|
|
151
149
|
//#endregion
|
|
152
|
-
exports.
|
|
150
|
+
exports.LingoTranslator = LingoTranslator;
|
|
@@ -7,11 +7,11 @@ import { DEFAULT_TIMEOUTS, withTimeout } from "../../utils/timeout.mjs";
|
|
|
7
7
|
import { generateText } from "ai";
|
|
8
8
|
import { LingoDotDevEngine } from "lingo.dev/sdk";
|
|
9
9
|
|
|
10
|
-
//#region src/translators/lingo/
|
|
10
|
+
//#region src/translators/lingo/translator.ts
|
|
11
11
|
/**
|
|
12
12
|
* Lingo translator using AI models
|
|
13
13
|
*/
|
|
14
|
-
var
|
|
14
|
+
var LingoTranslator = class {
|
|
15
15
|
validatedKeys;
|
|
16
16
|
constructor(config, logger) {
|
|
17
17
|
this.config = config;
|
|
@@ -24,31 +24,29 @@ var Service = class {
|
|
|
24
24
|
* Translate multiple entries
|
|
25
25
|
*/
|
|
26
26
|
async translate(locale, entriesMap) {
|
|
27
|
-
this.logger.debug(`
|
|
27
|
+
this.logger.debug(`translate() called for ${locale} with ${Object.keys(entriesMap).length} entries`);
|
|
28
28
|
const sourceDictionary = dictionaryFrom(this.config.sourceLocale, Object.fromEntries(Object.entries(entriesMap).map(([hash, entry]) => [hash, entry.text])));
|
|
29
|
-
this.logger.debug(`
|
|
29
|
+
this.logger.debug(`Created source dictionary with ${Object.keys(sourceDictionary.entries).length} entries`);
|
|
30
30
|
return (await this.translateDictionary(sourceDictionary, locale)).entries || {};
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Translate a complete dictionary
|
|
34
34
|
*/
|
|
35
35
|
async translateDictionary(sourceDictionary, targetLocale) {
|
|
36
|
-
this.logger.debug(`[TRACE-LINGO] Chunking dictionary with ${Object.keys(sourceDictionary.entries).length} entries`);
|
|
37
36
|
const chunks = this.chunkDictionary(sourceDictionary);
|
|
38
|
-
this.logger.debug(`
|
|
37
|
+
this.logger.debug(`Split dictionary with ${Object.keys(sourceDictionary.entries).length} into ${chunks.length} chunks`);
|
|
39
38
|
const translatedChunks = [];
|
|
40
39
|
for (let i = 0; i < chunks.length; i++) {
|
|
41
40
|
const chunk = chunks[i];
|
|
42
|
-
this.logger.debug(`
|
|
41
|
+
this.logger.debug(`Translating chunk ${i + 1}/${chunks.length} with ${Object.keys(chunk.entries).length} entries`);
|
|
43
42
|
const chunkStartTime = performance.now();
|
|
44
43
|
const translatedChunk = await this.translateChunk(chunk, targetLocale);
|
|
45
44
|
const chunkEndTime = performance.now();
|
|
46
|
-
this.logger.debug(`
|
|
45
|
+
this.logger.debug(`Chunk ${i + 1}/${chunks.length} completed in ${(chunkEndTime - chunkStartTime).toFixed(2)}ms`);
|
|
47
46
|
translatedChunks.push(translatedChunk);
|
|
48
47
|
}
|
|
49
|
-
this.logger.debug(`[TRACE-LINGO] All chunks translated, merging results`);
|
|
50
48
|
const result = this.mergeDictionaries(translatedChunks);
|
|
51
|
-
this.logger.debug(`
|
|
49
|
+
this.logger.debug(`Merge completed, final dictionary has ${Object.keys(result.entries).length} entries`);
|
|
52
50
|
return result;
|
|
53
51
|
}
|
|
54
52
|
/**
|
|
@@ -65,7 +63,7 @@ var Service = class {
|
|
|
65
63
|
async translateWithLingoDotDev(sourceDictionary, targetLocale) {
|
|
66
64
|
const apiKey = this.validatedKeys["lingo.dev"];
|
|
67
65
|
if (!apiKey) throw new Error("Internal error: Lingo.dev API key not found after validation. Please restart the service.");
|
|
68
|
-
this.logger.
|
|
66
|
+
this.logger.debug(`Using Lingo.dev Engine to localize from "${this.config.sourceLocale}" to "${targetLocale}"`);
|
|
69
67
|
const engine = new LingoDotDevEngine({ apiKey });
|
|
70
68
|
try {
|
|
71
69
|
return await withTimeout(engine.localizeObject(sourceDictionary, {
|
|
@@ -84,7 +82,7 @@ var Service = class {
|
|
|
84
82
|
async translateWithLLM(sourceDictionary, targetLocale) {
|
|
85
83
|
const localeModel = getLocaleModel(this.config.models, this.config.sourceLocale, targetLocale);
|
|
86
84
|
if (!localeModel) throw new Error(`No model configured for translation from ${this.config.sourceLocale} to ${targetLocale}`);
|
|
87
|
-
this.logger.
|
|
85
|
+
this.logger.debug(`Using LLM ("${localeModel.provider}":"${localeModel.name}") to translate from "${this.config.sourceLocale}" to "${targetLocale}"`);
|
|
88
86
|
const aiModel = createAiModel(localeModel, this.validatedKeys);
|
|
89
87
|
try {
|
|
90
88
|
return parseXmlFromResponseText((await withTimeout(generateText({
|
|
@@ -148,5 +146,5 @@ var Service = class {
|
|
|
148
146
|
};
|
|
149
147
|
|
|
150
148
|
//#endregion
|
|
151
|
-
export {
|
|
152
|
-
//# sourceMappingURL=
|
|
149
|
+
export { LingoTranslator };
|
|
150
|
+
//# sourceMappingURL=translator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translator.mjs","names":["config: LingoTranslatorConfig","logger: Logger","sourceDictionary: DictionarySchema","translatedChunks: DictionarySchema[]","chunks: DictionarySchema[]","mergedEntries: Record<string, string>"],"sources":["../../../src/translators/lingo/translator.ts"],"sourcesContent":["import { generateText } from \"ai\";\nimport { LingoDotDevEngine } from \"lingo.dev/sdk\";\nimport { dictionaryFrom, type DictionarySchema, type TranslatableEntry, type Translator, } from \"../api\";\nimport { getSystemPrompt } from \"./prompt\";\nimport { obj2xml, parseXmlFromResponseText } from \"../parse-xml\";\nimport { shots } from \"./shots\";\nimport { createAiModel, getLocaleModel, validateAndGetApiKeys, type ValidatedApiKeys, } from \"./model-factory\";\nimport { Logger } from \"../../utils/logger\";\nimport { DEFAULT_TIMEOUTS, withTimeout } from \"../../utils/timeout\";\nimport type { LocaleCode } from \"lingo.dev/spec\";\n\n/**\n * Lingo Translator configuration\n */\nexport interface LingoTranslatorConfig {\n models: \"lingo.dev\" | Record<string, string>;\n sourceLocale: LocaleCode;\n prompt?: string;\n}\n\n/**\n * Lingo translator using AI models\n */\nexport class LingoTranslator implements Translator<LingoTranslatorConfig> {\n private readonly validatedKeys: ValidatedApiKeys;\n\n constructor(\n readonly config: LingoTranslatorConfig,\n private logger: Logger,\n ) {\n this.logger.info(\"Validating API keys for translation...\");\n this.validatedKeys = validateAndGetApiKeys(config.models);\n this.logger.info(\"✅ API keys validated successfully\");\n }\n\n /**\n * Translate multiple entries\n */\n async translate(\n locale: LocaleCode,\n entriesMap: Record<string, TranslatableEntry>,\n ): Promise<Record<string, string>> {\n this.logger.debug(\n `translate() called for ${locale} with ${Object.keys(entriesMap).length} entries`,\n );\n\n const sourceDictionary: DictionarySchema = dictionaryFrom(\n this.config.sourceLocale,\n Object.fromEntries(\n Object.entries(entriesMap).map(([hash, entry]) => [hash, entry.text]),\n ),\n );\n\n this.logger.debug(\n `Created source dictionary with ${Object.keys(sourceDictionary.entries).length} entries`,\n );\n const translated = await this.translateDictionary(sourceDictionary, locale);\n\n return translated.entries || {};\n }\n\n /**\n * Translate a complete dictionary\n */\n private async translateDictionary(\n sourceDictionary: DictionarySchema,\n targetLocale: string,\n ): Promise<DictionarySchema> {\n const chunks = this.chunkDictionary(sourceDictionary);\n this.logger.debug(\n `Split dictionary with ${Object.keys(sourceDictionary.entries).length} into ${chunks.length} chunks`,\n );\n\n const translatedChunks: DictionarySchema[] = [];\n\n for (let i = 0; i < chunks.length; i++) {\n const chunk = chunks[i];\n this.logger.debug(\n `Translating chunk ${i + 1}/${chunks.length} with ${Object.keys(chunk.entries).length} entries`,\n );\n const chunkStartTime = performance.now();\n\n const translatedChunk = await this.translateChunk(chunk, targetLocale);\n\n const chunkEndTime = performance.now();\n this.logger.debug(\n `Chunk ${i + 1}/${chunks.length} completed in ${(chunkEndTime - chunkStartTime).toFixed(2)}ms`,\n );\n\n translatedChunks.push(translatedChunk);\n }\n\n const result = this.mergeDictionaries(translatedChunks);\n this.logger.debug(\n `Merge completed, final dictionary has ${Object.keys(result.entries).length} entries`,\n );\n\n return result;\n }\n\n /**\n * Translate a single chunk\n */\n private async translateChunk(\n sourceDictionary: DictionarySchema,\n targetLocale: string,\n ): Promise<DictionarySchema> {\n if (this.config.models === \"lingo.dev\") {\n return this.translateWithLingoDotDev(sourceDictionary, targetLocale);\n } else {\n return this.translateWithLLM(sourceDictionary, targetLocale);\n }\n }\n\n /**\n * Translate using Lingo.dev Engine\n * Times out after 60 seconds to prevent indefinite hangs\n */\n private async translateWithLingoDotDev(\n sourceDictionary: DictionarySchema,\n targetLocale: string,\n ): Promise<DictionarySchema> {\n const apiKey = this.validatedKeys[\"lingo.dev\"];\n if (!apiKey) {\n throw new Error(\n \"Internal error: Lingo.dev API key not found after validation. Please restart the service.\",\n );\n }\n\n this.logger.debug(\n `Using Lingo.dev Engine to localize from \"${this.config.sourceLocale}\" to \"${targetLocale}\"`,\n );\n\n const engine = new LingoDotDevEngine({ apiKey });\n\n try {\n const result = await withTimeout(\n engine.localizeObject(sourceDictionary, {\n sourceLocale: this.config.sourceLocale,\n targetLocale: targetLocale,\n }),\n DEFAULT_TIMEOUTS.AI_API,\n `Lingo.dev API translation to ${targetLocale}`,\n );\n\n return result as DictionarySchema;\n } catch (error) {\n this.logger.error(`translateWithLingoDotDev() failed:`, error);\n throw new Error(\n `Lingo.dev translation failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n }\n\n /**\n * Translate using generic LLM\n * Times out after 60 seconds to prevent indefinite hangs\n */\n private async translateWithLLM(\n sourceDictionary: DictionarySchema,\n targetLocale: string,\n ): Promise<DictionarySchema> {\n const localeModel = getLocaleModel(\n this.config.models as Record<string, string>,\n this.config.sourceLocale,\n targetLocale,\n );\n\n if (!localeModel) {\n throw new Error(\n `No model configured for translation from ${this.config.sourceLocale} to ${targetLocale}`,\n );\n }\n\n this.logger.debug(\n `Using LLM (\"${localeModel.provider}\":\"${localeModel.name}\") to translate from \"${this.config.sourceLocale}\" to \"${targetLocale}\"`,\n );\n\n const aiModel = createAiModel(localeModel, this.validatedKeys);\n\n try {\n const response = await withTimeout(\n generateText({\n model: aiModel,\n messages: [\n {\n role: \"system\",\n content: getSystemPrompt({\n sourceLocale: this.config.sourceLocale,\n targetLocale,\n prompt: this.config.prompt,\n }),\n },\n // Add few-shot examples\n ...shots.flatMap((shotsTuple) => [\n {\n role: \"user\" as const,\n content: obj2xml(shotsTuple[0]),\n },\n {\n role: \"assistant\" as const,\n content: obj2xml(shotsTuple[1]),\n },\n ]),\n {\n role: \"user\",\n content: obj2xml(sourceDictionary),\n },\n ],\n }),\n DEFAULT_TIMEOUTS.AI_API,\n `${localeModel.provider} LLM translation to ${targetLocale}`,\n );\n\n return parseXmlFromResponseText<DictionarySchema>(response.text);\n } catch (error) {\n throw new Error(\n `LLM translation failed with ${localeModel.provider}: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n }\n\n /**\n * Split dictionary into chunks for processing\n */\n private chunkDictionary(dictionary: DictionarySchema): DictionarySchema[] {\n const MAX_ENTRIES_PER_CHUNK = 100;\n const { entries, ...rest } = dictionary;\n const chunks: DictionarySchema[] = [];\n\n const entryPairs = Object.entries(entries);\n\n // Split entries into chunks of MAX_ENTRIES_PER_CHUNK\n for (let i = 0; i < entryPairs.length; i += MAX_ENTRIES_PER_CHUNK) {\n const chunkEntries = entryPairs.slice(i, i + MAX_ENTRIES_PER_CHUNK);\n chunks.push({\n ...rest,\n entries: Object.fromEntries(chunkEntries),\n });\n }\n\n return chunks;\n }\n\n /**\n * Merge multiple dictionaries into one\n */\n private mergeDictionaries(\n dictionaries: DictionarySchema[],\n ): DictionarySchema {\n if (dictionaries.length === 0) {\n return dictionaryFrom(this.config.sourceLocale, {});\n }\n\n // Merge all entries from all dictionaries\n const mergedEntries: Record<string, string> = {};\n for (const dict of dictionaries) {\n Object.assign(mergedEntries, dict.entries);\n }\n\n return {\n version: dictionaries[0].version,\n locale: dictionaries[0].locale,\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js";
|
|
3
|
-
import * as fs from "fs/promises";
|
|
3
|
+
import * as fs$1 from "fs/promises";
|
|
4
4
|
import * as path$1 from "path";
|
|
5
5
|
|
|
6
6
|
//#region src/translators/local-cache.ts
|
|
@@ -27,7 +27,7 @@ var LocalTranslationCache = class {
|
|
|
27
27
|
async getDictionary(locale) {
|
|
28
28
|
try {
|
|
29
29
|
const cachePath = this.getCachePath(locale);
|
|
30
|
-
const content = await withTimeout(fs.readFile(cachePath, "utf-8"), DEFAULT_TIMEOUTS.FILE_IO, `Read cache for ${locale}`);
|
|
30
|
+
const content = await withTimeout(fs$1.readFile(cachePath, "utf-8"), DEFAULT_TIMEOUTS.FILE_IO, `Read cache for ${locale}`);
|
|
31
31
|
return JSON.parse(content);
|
|
32
32
|
} catch {
|
|
33
33
|
return null;
|
|
@@ -41,8 +41,8 @@ var LocalTranslationCache = class {
|
|
|
41
41
|
try {
|
|
42
42
|
const cachePath = this.getCachePath(locale);
|
|
43
43
|
const cacheDir = path$1.dirname(cachePath);
|
|
44
|
-
await withTimeout(fs.mkdir(cacheDir, { recursive: true }), DEFAULT_TIMEOUTS.FILE_IO, `Create cache directory for ${locale}`);
|
|
45
|
-
await withTimeout(fs.writeFile(cachePath, JSON.stringify(dictionary, null, 2), "utf-8"), DEFAULT_TIMEOUTS.FILE_IO, `Write cache for ${locale}`);
|
|
44
|
+
await withTimeout(fs$1.mkdir(cacheDir, { recursive: true }), DEFAULT_TIMEOUTS.FILE_IO, `Create cache directory for ${locale}`);
|
|
45
|
+
await withTimeout(fs$1.writeFile(cachePath, JSON.stringify(dictionary, null, 2), "utf-8"), DEFAULT_TIMEOUTS.FILE_IO, `Write cache for ${locale}`);
|
|
46
46
|
} catch (error) {
|
|
47
47
|
this.logger.error(`Failed to write cache for locale ${locale}:`, error);
|
|
48
48
|
throw error;
|
|
@@ -82,7 +82,7 @@ var LocalTranslationCache = class {
|
|
|
82
82
|
async has(locale) {
|
|
83
83
|
try {
|
|
84
84
|
const cachePath = this.getCachePath(locale);
|
|
85
|
-
await fs.access(cachePath);
|
|
85
|
+
await fs$1.access(cachePath);
|
|
86
86
|
return true;
|
|
87
87
|
} catch {
|
|
88
88
|
return false;
|
|
@@ -94,7 +94,7 @@ var LocalTranslationCache = class {
|
|
|
94
94
|
async clear(locale) {
|
|
95
95
|
try {
|
|
96
96
|
const cachePath = this.getCachePath(locale);
|
|
97
|
-
await fs.unlink(cachePath);
|
|
97
|
+
await fs$1.unlink(cachePath);
|
|
98
98
|
} catch {}
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
@@ -102,8 +102,8 @@ var LocalTranslationCache = class {
|
|
|
102
102
|
*/
|
|
103
103
|
async clearAll() {
|
|
104
104
|
try {
|
|
105
|
-
const files = await fs.readdir(this.config.cacheDir);
|
|
106
|
-
await Promise.all(files.filter((file) => file.endsWith(".json")).map((file) => fs.unlink(path$1.join(this.config.cacheDir, file))));
|
|
105
|
+
const files = await fs$1.readdir(this.config.cacheDir);
|
|
106
|
+
await Promise.all(files.filter((file) => file.endsWith(".json")).map((file) => fs$1.unlink(path$1.join(this.config.cacheDir, file))));
|
|
107
107
|
} catch {}
|
|
108
108
|
}
|
|
109
109
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-cache.mjs","names":["logger: Logger","path"],"sources":["../../src/translators/local-cache.ts"],"sourcesContent":["/**\n * Local disk-based translation cache implementation\n */\n\nimport * as fs from \"fs/promises\";\nimport * as path from \"path\";\nimport type { LocalCacheConfig, TranslationCache } from \"./cache\";\nimport { dictionaryFrom, type DictionarySchema } from \"./api\";\nimport { DEFAULT_TIMEOUTS, withTimeout } from \"../utils/timeout\";\nimport type { Logger } from \"../utils/logger\";\nimport type { LocaleCode } from \"lingo.dev/spec\";\n\n/**\n * Local file system cache for translations\n * Stores translations as JSON files in .lingo/cache/\n */\nexport class LocalTranslationCache implements TranslationCache {\n private config: LocalCacheConfig;\n\n constructor(\n config: LocalCacheConfig,\n private logger: Logger,\n ) {\n this.config = config;\n }\n\n private getCachePath(locale: LocaleCode): string {\n return path.join(this.config.cacheDir, `${locale}.json`);\n }\n\n /**\n * Read dictionary file from disk\n * Times out after 10 seconds to prevent indefinite hangs\n */\n async getDictionary(locale: LocaleCode): Promise<DictionarySchema | null> {\n try {\n const cachePath = this.getCachePath(locale);\n const content = await withTimeout(\n fs.readFile(cachePath, \"utf-8\"),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Read cache for ${locale}`,\n );\n return JSON.parse(content);\n } catch {\n return null;\n }\n }\n\n /**\n * Write dictionary file to disk\n * Times out after 10 seconds to prevent indefinite hangs\n */\n private async setDictionary(\n locale: LocaleCode,\n dictionary: DictionarySchema,\n ): Promise<void> {\n try {\n const cachePath = this.getCachePath(locale);\n const cacheDir = path.dirname(cachePath);\n\n // Ensure cache directory exists\n await withTimeout(\n fs.mkdir(cacheDir, { recursive: true }),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Create cache directory for ${locale}`,\n );\n\n // Write cache file\n await withTimeout(\n fs.writeFile(cachePath, JSON.stringify(dictionary, null, 2), \"utf-8\"),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Write cache for ${locale}`,\n );\n } catch (error) {\n this.logger.error(`Failed to write cache for locale ${locale}:`, error);\n throw error;\n }\n }\n\n /**\n * Get cached translations for a locale\n */\n async get(\n locale: LocaleCode,\n hashes?: string[],\n ): Promise<Record<string, string>> {\n const dictionary = await this.getDictionary(locale);\n if (!dictionary) {\n return {};\n }\n if (hashes) {\n return hashes.reduce(\n (acc, hash) => ({ ...acc, [hash]: dictionary.entries[hash] }),\n {},\n );\n }\n return dictionary.entries || {};\n }\n\n /**\n * Update cache with new translations (merge)\n */\n async update(\n locale: LocaleCode,\n translations: Record<string, string>,\n ): Promise<void> {\n const existing = await this.get(locale);\n\n const merged = { ...existing, ...translations };\n\n await this.set(locale, merged);\n }\n\n /**\n * Replace entire cache for a locale\n */\n async set(\n locale: LocaleCode,\n translations: Record<string, string>,\n ): Promise<void> {\n await this.setDictionary(locale, dictionaryFrom(locale, translations));\n }\n\n /**\n * Check if cache exists for a locale\n */\n async has(locale: LocaleCode): Promise<boolean> {\n try {\n const cachePath = this.getCachePath(locale);\n await fs.access(cachePath);\n return true;\n } catch {\n return false;\n }\n }\n\n /**\n * Clear cache for a specific locale\n */\n async clear(locale: LocaleCode): Promise<void> {\n try {\n const cachePath = this.getCachePath(locale);\n await fs.unlink(cachePath);\n } catch {\n // Ignore errors if file doesn't exist\n }\n }\n\n /**\n * Clear all cached translations\n */\n async clearAll(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDir);\n\n await Promise.all(\n files\n .filter((file) => file.endsWith(\".json\"))\n .map((file) => fs.unlink(path.join(this.config.cacheDir, file))),\n );\n } catch {\n // Ignore errors if directory doesn't exist\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAa,wBAAb,MAA+D;CAC7D,AAAQ;CAER,YACE,QACA,AAAQA,QACR;EADQ;AAER,OAAK,SAAS;;CAGhB,AAAQ,aAAa,QAA4B;AAC/C,SAAOC,OAAK,KAAK,KAAK,OAAO,UAAU,GAAG,OAAO,OAAO;;;;;;CAO1D,MAAM,cAAc,QAAsD;AACxE,MAAI;GACF,MAAM,YAAY,KAAK,aAAa,OAAO;GAC3C,MAAM,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"local-cache.mjs","names":["logger: Logger","path","fs"],"sources":["../../src/translators/local-cache.ts"],"sourcesContent":["/**\n * Local disk-based translation cache implementation\n */\n\nimport * as fs from \"fs/promises\";\nimport * as path from \"path\";\nimport type { LocalCacheConfig, TranslationCache } from \"./cache\";\nimport { dictionaryFrom, type DictionarySchema } from \"./api\";\nimport { DEFAULT_TIMEOUTS, withTimeout } from \"../utils/timeout\";\nimport type { Logger } from \"../utils/logger\";\nimport type { LocaleCode } from \"lingo.dev/spec\";\n\n/**\n * Local file system cache for translations\n * Stores translations as JSON files in .lingo/cache/\n */\nexport class LocalTranslationCache implements TranslationCache {\n private config: LocalCacheConfig;\n\n constructor(\n config: LocalCacheConfig,\n private logger: Logger,\n ) {\n this.config = config;\n }\n\n private getCachePath(locale: LocaleCode): string {\n return path.join(this.config.cacheDir, `${locale}.json`);\n }\n\n /**\n * Read dictionary file from disk\n * Times out after 10 seconds to prevent indefinite hangs\n */\n async getDictionary(locale: LocaleCode): Promise<DictionarySchema | null> {\n try {\n const cachePath = this.getCachePath(locale);\n const content = await withTimeout(\n fs.readFile(cachePath, \"utf-8\"),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Read cache for ${locale}`,\n );\n return JSON.parse(content);\n } catch {\n return null;\n }\n }\n\n /**\n * Write dictionary file to disk\n * Times out after 10 seconds to prevent indefinite hangs\n */\n private async setDictionary(\n locale: LocaleCode,\n dictionary: DictionarySchema,\n ): Promise<void> {\n try {\n const cachePath = this.getCachePath(locale);\n const cacheDir = path.dirname(cachePath);\n\n // Ensure cache directory exists\n await withTimeout(\n fs.mkdir(cacheDir, { recursive: true }),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Create cache directory for ${locale}`,\n );\n\n // Write cache file\n await withTimeout(\n fs.writeFile(cachePath, JSON.stringify(dictionary, null, 2), \"utf-8\"),\n DEFAULT_TIMEOUTS.FILE_IO,\n `Write cache for ${locale}`,\n );\n } catch (error) {\n this.logger.error(`Failed to write cache for locale ${locale}:`, error);\n throw error;\n }\n }\n\n /**\n * Get cached translations for a locale\n */\n async get(\n locale: LocaleCode,\n hashes?: string[],\n ): Promise<Record<string, string>> {\n const dictionary = await this.getDictionary(locale);\n if (!dictionary) {\n return {};\n }\n if (hashes) {\n return hashes.reduce(\n (acc, hash) => ({ ...acc, [hash]: dictionary.entries[hash] }),\n {},\n );\n }\n return dictionary.entries || {};\n }\n\n /**\n * Update cache with new translations (merge)\n */\n async update(\n locale: LocaleCode,\n translations: Record<string, string>,\n ): Promise<void> {\n const existing = await this.get(locale);\n\n const merged = { ...existing, ...translations };\n\n await this.set(locale, merged);\n }\n\n /**\n * Replace entire cache for a locale\n */\n async set(\n locale: LocaleCode,\n translations: Record<string, string>,\n ): Promise<void> {\n await this.setDictionary(locale, dictionaryFrom(locale, translations));\n }\n\n /**\n * Check if cache exists for a locale\n */\n async has(locale: LocaleCode): Promise<boolean> {\n try {\n const cachePath = this.getCachePath(locale);\n await fs.access(cachePath);\n return true;\n } catch {\n return false;\n }\n }\n\n /**\n * Clear cache for a specific locale\n */\n async clear(locale: LocaleCode): Promise<void> {\n try {\n const cachePath = this.getCachePath(locale);\n await fs.unlink(cachePath);\n } catch {\n // Ignore errors if file doesn't exist\n }\n }\n\n /**\n * Clear all cached translations\n */\n async clearAll(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDir);\n\n await Promise.all(\n files\n .filter((file) => file.endsWith(\".json\"))\n .map((file) => fs.unlink(path.join(this.config.cacheDir, file))),\n );\n } catch {\n // Ignore errors if directory doesn't exist\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAa,wBAAb,MAA+D;CAC7D,AAAQ;CAER,YACE,QACA,AAAQA,QACR;EADQ;AAER,OAAK,SAAS;;CAGhB,AAAQ,aAAa,QAA4B;AAC/C,SAAOC,OAAK,KAAK,KAAK,OAAO,UAAU,GAAG,OAAO,OAAO;;;;;;CAO1D,MAAM,cAAc,QAAsD;AACxE,MAAI;GACF,MAAM,YAAY,KAAK,aAAa,OAAO;GAC3C,MAAM,UAAU,MAAM,YACpBC,KAAG,SAAS,WAAW,QAAQ,EAC/B,iBAAiB,SACjB,kBAAkB,SACnB;AACD,UAAO,KAAK,MAAM,QAAQ;UACpB;AACN,UAAO;;;;;;;CAQX,MAAc,cACZ,QACA,YACe;AACf,MAAI;GACF,MAAM,YAAY,KAAK,aAAa,OAAO;GAC3C,MAAM,WAAWD,OAAK,QAAQ,UAAU;AAGxC,SAAM,YACJC,KAAG,MAAM,UAAU,EAAE,WAAW,MAAM,CAAC,EACvC,iBAAiB,SACjB,8BAA8B,SAC/B;AAGD,SAAM,YACJA,KAAG,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|
3
|
+
/**
|
|
4
|
+
* In memory translation cache implementation
|
|
5
|
+
*/
|
|
6
|
+
var MemoryTranslationCache = class {
|
|
7
|
+
cache = /* @__PURE__ */ new Map();
|
|
8
|
+
constructor() {}
|
|
9
|
+
async get(locale, hashes) {
|
|
10
|
+
const localeCache = this.cache.get(locale);
|
|
11
|
+
if (!localeCache) return {};
|
|
12
|
+
if (hashes) return hashes.reduce((acc, hash) => ({
|
|
13
|
+
...acc,
|
|
14
|
+
[hash]: localeCache.get(hash)
|
|
15
|
+
}), {});
|
|
16
|
+
return Object.fromEntries(localeCache);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Update cache with new translations (merge)
|
|
20
|
+
*/
|
|
21
|
+
async update(locale, translations) {
|
|
22
|
+
let localeCache = this.cache.get(locale);
|
|
23
|
+
if (!localeCache) {
|
|
24
|
+
localeCache = /* @__PURE__ */ new Map();
|
|
25
|
+
this.cache.set(locale, localeCache);
|
|
26
|
+
}
|
|
27
|
+
for (const [key, value] of Object.entries(translations)) localeCache.set(key, value);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Replace entire cache for a locale
|
|
31
|
+
*/
|
|
32
|
+
async set(locale, translations) {
|
|
33
|
+
this.cache.set(locale, new Map(Object.entries(translations)));
|
|
34
|
+
}
|
|
35
|
+
async has(locale) {
|
|
36
|
+
return this.cache.has(locale);
|
|
37
|
+
}
|
|
38
|
+
async clear(locale) {
|
|
39
|
+
this.cache.delete(locale);
|
|
40
|
+
}
|
|
41
|
+
async clearAll() {
|
|
42
|
+
this.cache.clear();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.MemoryTranslationCache = MemoryTranslationCache;
|