@lingo.dev/compiler 0.1.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.
- package/LICENSE.md +201 -0
- package/README.md +192 -0
- package/build/_virtual/rolldown_runtime.cjs +29 -0
- package/build/_virtual/rolldown_runtime.mjs +7 -0
- package/build/index.cjs +0 -0
- package/build/index.d.cts +2 -0
- package/build/index.d.mts +2 -0
- package/build/index.mjs +1 -0
- package/build/metadata/manager.cjs +131 -0
- package/build/metadata/manager.mjs +123 -0
- package/build/metadata/manager.mjs.map +1 -0
- package/build/plugin/build-translator.cjs +198 -0
- package/build/plugin/build-translator.mjs +196 -0
- package/build/plugin/build-translator.mjs.map +1 -0
- package/build/plugin/cleanup.cjs +20 -0
- package/build/plugin/cleanup.mjs +20 -0
- package/build/plugin/cleanup.mjs.map +1 -0
- package/build/plugin/next-compiler-loader.cjs +41 -0
- package/build/plugin/next-compiler-loader.d.cts +12 -0
- package/build/plugin/next-compiler-loader.d.cts.map +1 -0
- package/build/plugin/next-compiler-loader.d.mts +13 -0
- package/build/plugin/next-compiler-loader.d.mts.map +1 -0
- package/build/plugin/next-compiler-loader.mjs +42 -0
- package/build/plugin/next-compiler-loader.mjs.map +1 -0
- package/build/plugin/next-config-loader.cjs +13 -0
- package/build/plugin/next-config-loader.d.cts +8 -0
- package/build/plugin/next-config-loader.d.cts.map +1 -0
- package/build/plugin/next-config-loader.d.mts +9 -0
- package/build/plugin/next-config-loader.d.mts.map +1 -0
- package/build/plugin/next-config-loader.mjs +14 -0
- package/build/plugin/next-config-loader.mjs.map +1 -0
- package/build/plugin/next-locale-client-loader.cjs +9 -0
- package/build/plugin/next-locale-client-loader.d.cts +8 -0
- package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-client-loader.d.mts +9 -0
- package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-client-loader.mjs +10 -0
- package/build/plugin/next-locale-client-loader.mjs.map +1 -0
- package/build/plugin/next-locale-server-loader.cjs +9 -0
- package/build/plugin/next-locale-server-loader.d.cts +8 -0
- package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-server-loader.d.mts +9 -0
- package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-server-loader.mjs +10 -0
- package/build/plugin/next-locale-server-loader.mjs.map +1 -0
- package/build/plugin/next.cjs +220 -0
- package/build/plugin/next.d.cts +9 -0
- package/build/plugin/next.d.cts.map +1 -0
- package/build/plugin/next.d.mts +9 -0
- package/build/plugin/next.d.mts.map +1 -0
- package/build/plugin/next.mjs +222 -0
- package/build/plugin/next.mjs.map +1 -0
- package/build/plugin/transform/babel-compat.cjs +13 -0
- package/build/plugin/transform/babel-compat.mjs +10 -0
- package/build/plugin/transform/babel-compat.mjs.map +1 -0
- package/build/plugin/transform/index.cjs +44 -0
- package/build/plugin/transform/index.mjs +42 -0
- package/build/plugin/transform/index.mjs.map +1 -0
- package/build/plugin/transform/metadata.cjs +142 -0
- package/build/plugin/transform/metadata.mjs +141 -0
- package/build/plugin/transform/metadata.mjs.map +1 -0
- package/build/plugin/transform/parse-override.cjs +145 -0
- package/build/plugin/transform/parse-override.mjs +144 -0
- package/build/plugin/transform/parse-override.mjs.map +1 -0
- package/build/plugin/transform/process-file.cjs +391 -0
- package/build/plugin/transform/process-file.mjs +390 -0
- package/build/plugin/transform/process-file.mjs.map +1 -0
- package/build/plugin/transform/use-i18n.cjs +8 -0
- package/build/plugin/transform/use-i18n.mjs +7 -0
- package/build/plugin/transform/use-i18n.mjs.map +1 -0
- package/build/plugin/transform/utils.cjs +205 -0
- package/build/plugin/transform/utils.mjs +192 -0
- package/build/plugin/transform/utils.mjs.map +1 -0
- package/build/plugin/unplugin.cjs +188 -0
- package/build/plugin/unplugin.d.cts +8 -0
- package/build/plugin/unplugin.d.cts.map +1 -0
- package/build/plugin/unplugin.d.mts +8 -0
- package/build/plugin/unplugin.d.mts.map +1 -0
- package/build/plugin/unplugin.mjs +186 -0
- package/build/plugin/unplugin.mjs.map +1 -0
- package/build/plugin/vite.cjs +28 -0
- package/build/plugin/vite.d.cts +9 -0
- package/build/plugin/vite.d.cts.map +1 -0
- package/build/plugin/vite.d.mts +9 -0
- package/build/plugin/vite.d.mts.map +1 -0
- package/build/plugin/vite.mjs +29 -0
- package/build/plugin/vite.mjs.map +1 -0
- package/build/plugin/webpack.cjs +27 -0
- package/build/plugin/webpack.d.cts +8 -0
- package/build/plugin/webpack.d.cts.map +1 -0
- package/build/plugin/webpack.d.mts +8 -0
- package/build/plugin/webpack.d.mts.map +1 -0
- package/build/plugin/webpack.mjs +28 -0
- package/build/plugin/webpack.mjs.map +1 -0
- package/build/react/client/index.cjs +9 -0
- package/build/react/client/index.d.cts +5 -0
- package/build/react/client/index.d.mts +5 -0
- package/build/react/client/index.mjs +6 -0
- package/build/react/client/useTranslation.cjs +71 -0
- package/build/react/client/useTranslation.d.cts +42 -0
- package/build/react/client/useTranslation.d.cts.map +1 -0
- package/build/react/client/useTranslation.d.mts +42 -0
- package/build/react/client/useTranslation.d.mts.map +1 -0
- package/build/react/client/useTranslation.mjs +71 -0
- package/build/react/client/useTranslation.mjs.map +1 -0
- package/build/react/next/client.cjs +25 -0
- package/build/react/next/client.d.cts +9 -0
- package/build/react/next/client.d.cts.map +1 -0
- package/build/react/next/client.d.mts +9 -0
- package/build/react/next/client.d.mts.map +1 -0
- package/build/react/next/client.mjs +24 -0
- package/build/react/next/client.mjs.map +1 -0
- package/build/react/next/cookie-locale-resolver.cjs +29 -0
- package/build/react/next/cookie-locale-resolver.d.cts +33 -0
- package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
- package/build/react/next/cookie-locale-resolver.d.mts +33 -0
- package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
- package/build/react/next/cookie-locale-resolver.mjs +29 -0
- package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
- package/build/react/next/server.cjs +21 -0
- package/build/react/next/server.d.cts +13 -0
- package/build/react/next/server.d.cts.map +1 -0
- package/build/react/next/server.d.mts +14 -0
- package/build/react/next/server.d.mts.map +1 -0
- package/build/react/next/server.mjs +20 -0
- package/build/react/next/server.mjs.map +1 -0
- package/build/react/server/ServerLingoProvider.cjs +19 -0
- package/build/react/server/ServerLingoProvider.d.cts +12 -0
- package/build/react/server/ServerLingoProvider.d.cts.map +1 -0
- package/build/react/server/ServerLingoProvider.d.mts +12 -0
- package/build/react/server/ServerLingoProvider.d.mts.map +1 -0
- package/build/react/server/ServerLingoProvider.mjs +19 -0
- package/build/react/server/ServerLingoProvider.mjs.map +1 -0
- package/build/react/server/index.cjs +7 -0
- package/build/react/server/index.d.cts +4 -0
- package/build/react/server/index.d.mts +4 -0
- package/build/react/server/index.mjs +5 -0
- package/build/react/server/useTranslation.cjs +60 -0
- package/build/react/server/useTranslation.d.cts +36 -0
- package/build/react/server/useTranslation.d.cts.map +1 -0
- package/build/react/server/useTranslation.d.mts +36 -0
- package/build/react/server/useTranslation.d.mts.map +1 -0
- package/build/react/server/useTranslation.mjs +60 -0
- package/build/react/server/useTranslation.mjs.map +1 -0
- package/build/react/server-only/index.cjs +42 -0
- package/build/react/server-only/index.d.cts +38 -0
- package/build/react/server-only/index.d.cts.map +1 -0
- package/build/react/server-only/index.d.mts +38 -0
- package/build/react/server-only/index.d.mts.map +1 -0
- package/build/react/server-only/index.mjs +42 -0
- package/build/react/server-only/index.mjs.map +1 -0
- package/build/react/server-only/translations.cjs +85 -0
- package/build/react/server-only/translations.mjs +85 -0
- package/build/react/server-only/translations.mjs.map +1 -0
- package/build/react/shared/LingoContext.cjs +14 -0
- package/build/react/shared/LingoContext.d.cts +41 -0
- package/build/react/shared/LingoContext.d.cts.map +1 -0
- package/build/react/shared/LingoContext.d.mts +41 -0
- package/build/react/shared/LingoContext.d.mts.map +1 -0
- package/build/react/shared/LingoContext.mjs +13 -0
- package/build/react/shared/LingoContext.mjs.map +1 -0
- package/build/react/shared/LingoProvider.cjs +274 -0
- package/build/react/shared/LingoProvider.d.cts +76 -0
- package/build/react/shared/LingoProvider.d.cts.map +1 -0
- package/build/react/shared/LingoProvider.d.mts +76 -0
- package/build/react/shared/LingoProvider.d.mts.map +1 -0
- package/build/react/shared/LingoProvider.mjs +274 -0
- package/build/react/shared/LingoProvider.mjs.map +1 -0
- package/build/react/shared/LocaleSwitcher.cjs +61 -0
- package/build/react/shared/LocaleSwitcher.d.cts +71 -0
- package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
- package/build/react/shared/LocaleSwitcher.d.mts +71 -0
- package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
- package/build/react/shared/LocaleSwitcher.mjs +61 -0
- package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
- package/build/react/shared/render-rich-text.cjs +55 -0
- package/build/react/shared/render-rich-text.d.cts +17 -0
- package/build/react/shared/render-rich-text.d.cts.map +1 -0
- package/build/react/shared/render-rich-text.d.mts +17 -0
- package/build/react/shared/render-rich-text.d.mts.map +1 -0
- package/build/react/shared/render-rich-text.mjs +54 -0
- package/build/react/shared/render-rich-text.mjs.map +1 -0
- package/build/react/shared/utils.cjs +34 -0
- package/build/react/shared/utils.mjs +35 -0
- package/build/react/shared/utils.mjs.map +1 -0
- package/build/react/types.d.cts +16 -0
- package/build/react/types.d.cts.map +1 -0
- package/build/react/types.d.mts +16 -0
- package/build/react/types.d.mts.map +1 -0
- package/build/translation-server/logger.cjs +37 -0
- package/build/translation-server/logger.mjs +37 -0
- package/build/translation-server/logger.mjs.map +1 -0
- package/build/translation-server/translation-server.cjs +547 -0
- package/build/translation-server/translation-server.mjs +544 -0
- package/build/translation-server/translation-server.mjs.map +1 -0
- package/build/translation-server/ws-events.cjs +15 -0
- package/build/translation-server/ws-events.mjs +15 -0
- package/build/translation-server/ws-events.mjs.map +1 -0
- package/build/translators/api.cjs +12 -0
- package/build/translators/api.mjs +12 -0
- package/build/translators/api.mjs.map +1 -0
- package/build/translators/cache-factory.cjs +26 -0
- package/build/translators/cache-factory.mjs +27 -0
- package/build/translators/cache-factory.mjs.map +1 -0
- package/build/translators/lingo/model-factory.cjs +179 -0
- package/build/translators/lingo/model-factory.mjs +174 -0
- package/build/translators/lingo/model-factory.mjs.map +1 -0
- package/build/translators/lingo/prompt.cjs +43 -0
- package/build/translators/lingo/prompt.mjs +43 -0
- package/build/translators/lingo/prompt.mjs.map +1 -0
- package/build/translators/lingo/service.cjs +152 -0
- package/build/translators/lingo/service.mjs +152 -0
- package/build/translators/lingo/service.mjs.map +1 -0
- package/build/translators/lingo/shots.cjs +28 -0
- package/build/translators/lingo/shots.mjs +28 -0
- package/build/translators/lingo/shots.mjs.map +1 -0
- package/build/translators/local-cache.cjs +115 -0
- package/build/translators/local-cache.mjs +113 -0
- package/build/translators/local-cache.mjs.map +1 -0
- package/build/translators/parse-xml.cjs +109 -0
- package/build/translators/parse-xml.mjs +108 -0
- package/build/translators/parse-xml.mjs.map +1 -0
- package/build/translators/pluralization/icu-validator.cjs +36 -0
- package/build/translators/pluralization/icu-validator.mjs +36 -0
- package/build/translators/pluralization/icu-validator.mjs.map +1 -0
- package/build/translators/pluralization/pattern-detector.cjs +25 -0
- package/build/translators/pluralization/pattern-detector.mjs +25 -0
- package/build/translators/pluralization/pattern-detector.mjs.map +1 -0
- package/build/translators/pluralization/prompt.cjs +98 -0
- package/build/translators/pluralization/prompt.mjs +98 -0
- package/build/translators/pluralization/prompt.mjs.map +1 -0
- package/build/translators/pluralization/service.cjs +247 -0
- package/build/translators/pluralization/service.mjs +247 -0
- package/build/translators/pluralization/service.mjs.map +1 -0
- package/build/translators/pluralization/shots.cjs +53 -0
- package/build/translators/pluralization/shots.mjs +53 -0
- package/build/translators/pluralization/shots.mjs.map +1 -0
- package/build/translators/pluralization/types.d.cts +17 -0
- package/build/translators/pluralization/types.d.cts.map +1 -0
- package/build/translators/pluralization/types.d.mts +17 -0
- package/build/translators/pluralization/types.d.mts.map +1 -0
- package/build/translators/pseudotranslator/index.cjs +129 -0
- package/build/translators/pseudotranslator/index.mjs +129 -0
- package/build/translators/pseudotranslator/index.mjs.map +1 -0
- package/build/translators/translation-service.cjs +182 -0
- package/build/translators/translation-service.mjs +183 -0
- package/build/translators/translation-service.mjs.map +1 -0
- package/build/translators/translator-factory.cjs +49 -0
- package/build/translators/translator-factory.mjs +50 -0
- package/build/translators/translator-factory.mjs.map +1 -0
- package/build/types.d.cts +161 -0
- package/build/types.d.cts.map +1 -0
- package/build/types.d.mts +161 -0
- package/build/types.d.mts.map +1 -0
- package/build/utils/config-factory.cjs +58 -0
- package/build/utils/config-factory.mjs +58 -0
- package/build/utils/config-factory.mjs.map +1 -0
- package/build/utils/hash.cjs +17 -0
- package/build/utils/hash.mjs +16 -0
- package/build/utils/hash.mjs.map +1 -0
- package/build/utils/is-valid-locale.cjs +14 -0
- package/build/utils/is-valid-locale.mjs +14 -0
- package/build/utils/is-valid-locale.mjs.map +1 -0
- package/build/utils/logger.cjs +51 -0
- package/build/utils/logger.mjs +50 -0
- package/build/utils/logger.mjs.map +1 -0
- package/build/utils/path-helpers.cjs +49 -0
- package/build/utils/path-helpers.mjs +47 -0
- package/build/utils/path-helpers.mjs.map +1 -0
- package/build/utils/timeout.cjs +42 -0
- package/build/utils/timeout.mjs +41 -0
- package/build/utils/timeout.mjs.map +1 -0
- package/build/virtual/code-generator.cjs +54 -0
- package/build/virtual/code-generator.mjs +53 -0
- package/build/virtual/code-generator.mjs.map +1 -0
- package/build/virtual/config.cjs +10 -0
- package/build/virtual/config.d.cts +9 -0
- package/build/virtual/config.d.cts.map +1 -0
- package/build/virtual/config.d.mts +9 -0
- package/build/virtual/config.d.mts.map +1 -0
- package/build/virtual/config.mjs +8 -0
- package/build/virtual/config.mjs.map +1 -0
- package/build/virtual/locale/client.cjs +23 -0
- package/build/virtual/locale/client.d.cts +19 -0
- package/build/virtual/locale/client.d.cts.map +1 -0
- package/build/virtual/locale/client.d.mts +19 -0
- package/build/virtual/locale/client.d.mts.map +1 -0
- package/build/virtual/locale/client.mjs +22 -0
- package/build/virtual/locale/client.mjs.map +1 -0
- package/build/virtual/locale/server.cjs +13 -0
- package/build/virtual/locale/server.d.cts +13 -0
- package/build/virtual/locale/server.d.cts.map +1 -0
- package/build/virtual/locale/server.d.mts +13 -0
- package/build/virtual/locale/server.d.mts.map +1 -0
- package/build/virtual/locale/server.mjs +13 -0
- package/build/virtual/locale/server.mjs.map +1 -0
- package/build/widget/lingo-dev-widget.cjs +228 -0
- package/build/widget/lingo-dev-widget.mjs +229 -0
- package/build/widget/lingo-dev-widget.mjs.map +1 -0
- package/package.json +189 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.mjs","names":[],"sources":["../../src/translators/api.ts"],"sourcesContent":["import type { LocaleCode } from \"lingo.dev/spec\";\n\nexport type TranslatableEntry = { text: string; context: Record<string, any> };\n\nexport interface Translator<Config> {\n config: Config;\n\n translate: (\n locale: LocaleCode,\n entriesMap: Record<string, TranslatableEntry>,\n ) => Promise<Record<string, string>>;\n}\n\n/**\n * Dictionary schema for translation\n * Simple flat structure with direct access to translations\n */\nexport interface DictionarySchema {\n version: number;\n locale: LocaleCode;\n entries: Record<string, string>;\n}\n\nexport function dictionaryFrom(\n locale: LocaleCode,\n entries: DictionarySchema[\"entries\"],\n) {\n return {\n // TODO (AleksandrSl 14/12/2025): We do not use version anywhere.\n // We should either get rid of it, or start cheking it, e.g. if we updated the hash function we should nuke the caches. However, in this case we can tie version to the hash somewhat automatically by using hash function name\n version: 0.1,\n locale,\n entries,\n };\n}\n"],"mappings":";AAuBA,SAAgB,eACd,QACA,SACA;AACA,QAAO;EAGL,SAAS;EACT;EACA;EACD"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const require_logger = require('../utils/logger.cjs');
|
|
2
|
+
const require_local_cache = require('./local-cache.cjs');
|
|
3
|
+
const require_path_helpers = require('../utils/path-helpers.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/translators/cache-factory.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a cache instance based on the config
|
|
8
|
+
*
|
|
9
|
+
* @param config - LingoConfig with cacheType and lingoDir
|
|
10
|
+
* @returns TranslationCache instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const cache = createCache(config);
|
|
15
|
+
* const translations = await cache.get("de");
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function createCache(config) {
|
|
19
|
+
switch (config.cacheType) {
|
|
20
|
+
case "local": return new require_local_cache.LocalTranslationCache({ cacheDir: require_path_helpers.getCacheDir(config) }, require_logger.logger);
|
|
21
|
+
default: throw new Error(`Unknown cache type: ${config.cacheType}. Only "local" is currently supported.`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.createCache = createCache;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { logger } from "../utils/logger.mjs";
|
|
2
|
+
import { LocalTranslationCache } from "./local-cache.mjs";
|
|
3
|
+
import { getCacheDir } from "../utils/path-helpers.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/translators/cache-factory.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a cache instance based on the config
|
|
8
|
+
*
|
|
9
|
+
* @param config - LingoConfig with cacheType and lingoDir
|
|
10
|
+
* @returns TranslationCache instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const cache = createCache(config);
|
|
15
|
+
* const translations = await cache.get("de");
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function createCache(config) {
|
|
19
|
+
switch (config.cacheType) {
|
|
20
|
+
case "local": return new LocalTranslationCache({ cacheDir: getCacheDir(config) }, logger);
|
|
21
|
+
default: throw new Error(`Unknown cache type: ${config.cacheType}. Only "local" is currently supported.`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { createCache };
|
|
27
|
+
//# sourceMappingURL=cache-factory.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-factory.mjs","names":[],"sources":["../../src/translators/cache-factory.ts"],"sourcesContent":["/**\n * Cache factory for creating cache instances from config\n */\n\nimport type { LingoConfig, PathConfig } from \"../types\";\nimport type { TranslationCache } from \"./cache\";\nimport { LocalTranslationCache } from \"./local-cache\";\nimport { logger } from \"../utils/logger\";\nimport { getCacheDir } from \"../utils/path-helpers\";\n\n/**\n * Create a cache instance based on the config\n *\n * @param config - LingoConfig with cacheType and lingoDir\n * @returns TranslationCache instance\n *\n * @example\n * ```typescript\n * const cache = createCache(config);\n * const translations = await cache.get(\"de\");\n * ```\n */\nexport function createCache(\n config: Pick<LingoConfig, \"cacheType\"> & PathConfig,\n): TranslationCache {\n switch (config.cacheType) {\n case \"local\":\n return new LocalTranslationCache(\n {\n cacheDir: getCacheDir(config),\n },\n logger,\n );\n\n default:\n // This should never happen due to TypeScript types, but provides a safeguard\n throw new Error(\n `Unknown cache type: ${config.cacheType}. Only \"local\" is currently supported.`,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsBA,SAAgB,YACd,QACkB;AAClB,SAAQ,OAAO,WAAf;EACE,KAAK,QACH,QAAO,IAAI,sBACT,EACE,UAAU,YAAY,OAAO,EAC9B,EACD,OACD;EAEH,QAEE,OAAM,IAAI,MACR,uBAAuB,OAAO,UAAU,wCACzC"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let path = require("path");
|
|
3
|
+
path = require_rolldown_runtime.__toESM(path);
|
|
4
|
+
let _ai_sdk_groq = require("@ai-sdk/groq");
|
|
5
|
+
let _ai_sdk_google = require("@ai-sdk/google");
|
|
6
|
+
let _openrouter_ai_sdk_provider = require("@openrouter/ai-sdk-provider");
|
|
7
|
+
let ai_sdk_ollama = require("ai-sdk-ollama");
|
|
8
|
+
let _ai_sdk_mistral = require("@ai-sdk/mistral");
|
|
9
|
+
let dotenv = require("dotenv");
|
|
10
|
+
dotenv = require_rolldown_runtime.__toESM(dotenv);
|
|
11
|
+
|
|
12
|
+
//#region src/translators/lingo/model-factory.ts
|
|
13
|
+
/**
|
|
14
|
+
* Shared utilities for creating AI model instances
|
|
15
|
+
*/
|
|
16
|
+
function getKeyFromEnv(envVarName) {
|
|
17
|
+
if (process.env[envVarName]) return process.env[envVarName];
|
|
18
|
+
const projectRoot = process.cwd();
|
|
19
|
+
return dotenv.config({ path: [
|
|
20
|
+
path.resolve(projectRoot, ".env"),
|
|
21
|
+
path.resolve(projectRoot, ".env.local"),
|
|
22
|
+
path.resolve(projectRoot, ".env.development")
|
|
23
|
+
] })?.parsed?.[envVarName];
|
|
24
|
+
}
|
|
25
|
+
const providerDetails = {
|
|
26
|
+
groq: {
|
|
27
|
+
name: "Groq",
|
|
28
|
+
apiKeyEnvVar: "GROQ_API_KEY",
|
|
29
|
+
apiKeyConfigKey: "llm.groqApiKey",
|
|
30
|
+
getKeyLink: "https://groq.com",
|
|
31
|
+
docsLink: "https://console.groq.com/docs/errors"
|
|
32
|
+
},
|
|
33
|
+
google: {
|
|
34
|
+
name: "Google",
|
|
35
|
+
apiKeyEnvVar: "GOOGLE_API_KEY",
|
|
36
|
+
apiKeyConfigKey: "llm.googleApiKey",
|
|
37
|
+
getKeyLink: "https://ai.google.dev/",
|
|
38
|
+
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting"
|
|
39
|
+
},
|
|
40
|
+
openai: {
|
|
41
|
+
name: "OpenAI",
|
|
42
|
+
apiKeyEnvVar: "OPENAI_API_KEY",
|
|
43
|
+
apiKeyConfigKey: "llm.openaiApiKey",
|
|
44
|
+
getKeyLink: "https://platform.openai.com/account/api-keys",
|
|
45
|
+
docsLink: "https://platform.openai.com/docs"
|
|
46
|
+
},
|
|
47
|
+
anthropic: {
|
|
48
|
+
name: "Anthropic",
|
|
49
|
+
apiKeyEnvVar: "ANTHROPIC_API_KEY",
|
|
50
|
+
apiKeyConfigKey: "llm.anthropicApiKey",
|
|
51
|
+
getKeyLink: "https://console.anthropic.com/get-api-key",
|
|
52
|
+
docsLink: "https://console.anthropic.com/docs"
|
|
53
|
+
},
|
|
54
|
+
openrouter: {
|
|
55
|
+
name: "OpenRouter",
|
|
56
|
+
apiKeyEnvVar: "OPENROUTER_API_KEY",
|
|
57
|
+
apiKeyConfigKey: "llm.openrouterApiKey",
|
|
58
|
+
getKeyLink: "https://openrouter.ai",
|
|
59
|
+
docsLink: "https://openrouter.ai/docs"
|
|
60
|
+
},
|
|
61
|
+
ollama: {
|
|
62
|
+
name: "Ollama",
|
|
63
|
+
apiKeyEnvVar: void 0,
|
|
64
|
+
apiKeyConfigKey: void 0,
|
|
65
|
+
getKeyLink: "https://ollama.com/download",
|
|
66
|
+
docsLink: "https://github.com/ollama/ollama/tree/main/docs"
|
|
67
|
+
},
|
|
68
|
+
mistral: {
|
|
69
|
+
name: "Mistral",
|
|
70
|
+
apiKeyEnvVar: "MISTRAL_API_KEY",
|
|
71
|
+
apiKeyConfigKey: "llm.mistralApiKey",
|
|
72
|
+
getKeyLink: "https://console.mistral.ai",
|
|
73
|
+
docsLink: "https://docs.mistral.ai"
|
|
74
|
+
},
|
|
75
|
+
"lingo.dev": {
|
|
76
|
+
name: "Lingo.dev",
|
|
77
|
+
apiKeyEnvVar: "LINGODOTDEV_API_KEY",
|
|
78
|
+
apiKeyConfigKey: "auth.apiKey",
|
|
79
|
+
getKeyLink: "https://lingo.dev",
|
|
80
|
+
docsLink: "https://lingo.dev/docs"
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Get provider and model for a specific locale pair
|
|
85
|
+
*/
|
|
86
|
+
function getLocaleModel(localeModels, sourceLocale, targetLocale) {
|
|
87
|
+
const modelKey = [
|
|
88
|
+
`${sourceLocale}:${targetLocale}`,
|
|
89
|
+
`*:${targetLocale}`,
|
|
90
|
+
`${sourceLocale}:*`,
|
|
91
|
+
"*:*"
|
|
92
|
+
].find((key) => key in localeModels);
|
|
93
|
+
if (!modelKey) return;
|
|
94
|
+
const value = localeModels[modelKey];
|
|
95
|
+
if (!value) return;
|
|
96
|
+
return parseModelString(value);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Parse provider and model from model string
|
|
100
|
+
* Format: "provider:model" (e.g., "groq:llama3-8b-8192")
|
|
101
|
+
*
|
|
102
|
+
* @param modelString Model string to parse
|
|
103
|
+
* @returns Object with provider and model
|
|
104
|
+
* @throws Error if format is invalid
|
|
105
|
+
*/
|
|
106
|
+
function parseModelString(modelString) {
|
|
107
|
+
const [provider, name] = modelString.split(":", 2);
|
|
108
|
+
if (!provider || !name) return;
|
|
109
|
+
return {
|
|
110
|
+
provider,
|
|
111
|
+
name
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Validate and fetch all necessary API keys for the given configuration
|
|
116
|
+
* This should be called once at initialization time
|
|
117
|
+
*
|
|
118
|
+
* @param config Model configuration ("lingo.dev" or locale-pair mapping)
|
|
119
|
+
* @returns Validated API keys (provider ID -> API key)
|
|
120
|
+
* @throws Error if required keys are missing
|
|
121
|
+
*/
|
|
122
|
+
function validateAndGetApiKeys(config) {
|
|
123
|
+
const keys = {};
|
|
124
|
+
const missingKeys = [];
|
|
125
|
+
let providersToValidate;
|
|
126
|
+
if (config === "lingo.dev") providersToValidate = ["lingo.dev"];
|
|
127
|
+
else {
|
|
128
|
+
const providerSet = /* @__PURE__ */ new Set();
|
|
129
|
+
Object.values(config).forEach((modelString) => {
|
|
130
|
+
const model = parseModelString(modelString);
|
|
131
|
+
if (model) providerSet.add(model.provider);
|
|
132
|
+
});
|
|
133
|
+
providersToValidate = Array.from(providerSet);
|
|
134
|
+
}
|
|
135
|
+
for (const provider of providersToValidate) {
|
|
136
|
+
const providerConfig = providerDetails[provider];
|
|
137
|
+
if (!providerConfig) throw new Error(`⚠️ Unknown provider "${provider}". Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
138
|
+
if (!providerConfig.apiKeyEnvVar) continue;
|
|
139
|
+
const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);
|
|
140
|
+
if (key) keys[provider] = key;
|
|
141
|
+
else missingKeys.push({
|
|
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.`);
|
|
149
|
+
}
|
|
150
|
+
return keys;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Create AI model instance from provider and model ID
|
|
154
|
+
*
|
|
155
|
+
* @param model Provider name (groq, google, openrouter, ollama, mistral) and model identifier
|
|
156
|
+
* @param validatedKeys Pre-validated API keys from validateAndFetchApiKeys()
|
|
157
|
+
* @returns LanguageModel instance
|
|
158
|
+
* @throws Error if provider is not supported or API key is missing
|
|
159
|
+
*/
|
|
160
|
+
function createAiModel(model, validatedKeys) {
|
|
161
|
+
const providerConfig = providerDetails[model.provider];
|
|
162
|
+
if (!providerConfig) throw new Error(`⚠️ Provider "${model.provider}" is not supported. Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
163
|
+
const apiKey = providerConfig.apiKeyEnvVar ? validatedKeys[model.provider] : void 0;
|
|
164
|
+
if (providerConfig.apiKeyEnvVar && !apiKey) throw new Error(`⚠️ ${providerConfig.name} API key not found. Please set ${providerConfig.apiKeyEnvVar} environment variable.\n\nThis should not happen if validateAndFetchApiKeys() was called. Please restart the service.`);
|
|
165
|
+
switch (model.provider) {
|
|
166
|
+
case "groq": return (0, _ai_sdk_groq.createGroq)({ apiKey })(model.name);
|
|
167
|
+
case "google": return (0, _ai_sdk_google.createGoogleGenerativeAI)({ apiKey })(model.name);
|
|
168
|
+
case "openrouter": return (0, _openrouter_ai_sdk_provider.createOpenRouter)({ apiKey })(model.name);
|
|
169
|
+
case "ollama": return (0, ai_sdk_ollama.ollama)(model.name);
|
|
170
|
+
case "mistral": return (0, _ai_sdk_mistral.createMistral)({ apiKey })(model.name);
|
|
171
|
+
default: throw new Error(`⚠️ Provider "${model.provider}" is not implemented`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
exports.createAiModel = createAiModel;
|
|
177
|
+
exports.getLocaleModel = getLocaleModel;
|
|
178
|
+
exports.parseModelString = parseModelString;
|
|
179
|
+
exports.validateAndGetApiKeys = validateAndGetApiKeys;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import * as path$1 from "path";
|
|
2
|
+
import { createGroq } from "@ai-sdk/groq";
|
|
3
|
+
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
4
|
+
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
5
|
+
import { ollama } from "ai-sdk-ollama";
|
|
6
|
+
import { createMistral } from "@ai-sdk/mistral";
|
|
7
|
+
import * as dotenv from "dotenv";
|
|
8
|
+
|
|
9
|
+
//#region src/translators/lingo/model-factory.ts
|
|
10
|
+
/**
|
|
11
|
+
* Shared utilities for creating AI model instances
|
|
12
|
+
*/
|
|
13
|
+
function getKeyFromEnv(envVarName) {
|
|
14
|
+
if (process.env[envVarName]) return process.env[envVarName];
|
|
15
|
+
const projectRoot = process.cwd();
|
|
16
|
+
return dotenv.config({ path: [
|
|
17
|
+
path$1.resolve(projectRoot, ".env"),
|
|
18
|
+
path$1.resolve(projectRoot, ".env.local"),
|
|
19
|
+
path$1.resolve(projectRoot, ".env.development")
|
|
20
|
+
] })?.parsed?.[envVarName];
|
|
21
|
+
}
|
|
22
|
+
const providerDetails = {
|
|
23
|
+
groq: {
|
|
24
|
+
name: "Groq",
|
|
25
|
+
apiKeyEnvVar: "GROQ_API_KEY",
|
|
26
|
+
apiKeyConfigKey: "llm.groqApiKey",
|
|
27
|
+
getKeyLink: "https://groq.com",
|
|
28
|
+
docsLink: "https://console.groq.com/docs/errors"
|
|
29
|
+
},
|
|
30
|
+
google: {
|
|
31
|
+
name: "Google",
|
|
32
|
+
apiKeyEnvVar: "GOOGLE_API_KEY",
|
|
33
|
+
apiKeyConfigKey: "llm.googleApiKey",
|
|
34
|
+
getKeyLink: "https://ai.google.dev/",
|
|
35
|
+
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting"
|
|
36
|
+
},
|
|
37
|
+
openai: {
|
|
38
|
+
name: "OpenAI",
|
|
39
|
+
apiKeyEnvVar: "OPENAI_API_KEY",
|
|
40
|
+
apiKeyConfigKey: "llm.openaiApiKey",
|
|
41
|
+
getKeyLink: "https://platform.openai.com/account/api-keys",
|
|
42
|
+
docsLink: "https://platform.openai.com/docs"
|
|
43
|
+
},
|
|
44
|
+
anthropic: {
|
|
45
|
+
name: "Anthropic",
|
|
46
|
+
apiKeyEnvVar: "ANTHROPIC_API_KEY",
|
|
47
|
+
apiKeyConfigKey: "llm.anthropicApiKey",
|
|
48
|
+
getKeyLink: "https://console.anthropic.com/get-api-key",
|
|
49
|
+
docsLink: "https://console.anthropic.com/docs"
|
|
50
|
+
},
|
|
51
|
+
openrouter: {
|
|
52
|
+
name: "OpenRouter",
|
|
53
|
+
apiKeyEnvVar: "OPENROUTER_API_KEY",
|
|
54
|
+
apiKeyConfigKey: "llm.openrouterApiKey",
|
|
55
|
+
getKeyLink: "https://openrouter.ai",
|
|
56
|
+
docsLink: "https://openrouter.ai/docs"
|
|
57
|
+
},
|
|
58
|
+
ollama: {
|
|
59
|
+
name: "Ollama",
|
|
60
|
+
apiKeyEnvVar: void 0,
|
|
61
|
+
apiKeyConfigKey: void 0,
|
|
62
|
+
getKeyLink: "https://ollama.com/download",
|
|
63
|
+
docsLink: "https://github.com/ollama/ollama/tree/main/docs"
|
|
64
|
+
},
|
|
65
|
+
mistral: {
|
|
66
|
+
name: "Mistral",
|
|
67
|
+
apiKeyEnvVar: "MISTRAL_API_KEY",
|
|
68
|
+
apiKeyConfigKey: "llm.mistralApiKey",
|
|
69
|
+
getKeyLink: "https://console.mistral.ai",
|
|
70
|
+
docsLink: "https://docs.mistral.ai"
|
|
71
|
+
},
|
|
72
|
+
"lingo.dev": {
|
|
73
|
+
name: "Lingo.dev",
|
|
74
|
+
apiKeyEnvVar: "LINGODOTDEV_API_KEY",
|
|
75
|
+
apiKeyConfigKey: "auth.apiKey",
|
|
76
|
+
getKeyLink: "https://lingo.dev",
|
|
77
|
+
docsLink: "https://lingo.dev/docs"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Get provider and model for a specific locale pair
|
|
82
|
+
*/
|
|
83
|
+
function getLocaleModel(localeModels, sourceLocale, targetLocale) {
|
|
84
|
+
const modelKey = [
|
|
85
|
+
`${sourceLocale}:${targetLocale}`,
|
|
86
|
+
`*:${targetLocale}`,
|
|
87
|
+
`${sourceLocale}:*`,
|
|
88
|
+
"*:*"
|
|
89
|
+
].find((key) => key in localeModels);
|
|
90
|
+
if (!modelKey) return;
|
|
91
|
+
const value = localeModels[modelKey];
|
|
92
|
+
if (!value) return;
|
|
93
|
+
return parseModelString(value);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse provider and model from model string
|
|
97
|
+
* Format: "provider:model" (e.g., "groq:llama3-8b-8192")
|
|
98
|
+
*
|
|
99
|
+
* @param modelString Model string to parse
|
|
100
|
+
* @returns Object with provider and model
|
|
101
|
+
* @throws Error if format is invalid
|
|
102
|
+
*/
|
|
103
|
+
function parseModelString(modelString) {
|
|
104
|
+
const [provider, name] = modelString.split(":", 2);
|
|
105
|
+
if (!provider || !name) return;
|
|
106
|
+
return {
|
|
107
|
+
provider,
|
|
108
|
+
name
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Validate and fetch all necessary API keys for the given configuration
|
|
113
|
+
* This should be called once at initialization time
|
|
114
|
+
*
|
|
115
|
+
* @param config Model configuration ("lingo.dev" or locale-pair mapping)
|
|
116
|
+
* @returns Validated API keys (provider ID -> API key)
|
|
117
|
+
* @throws Error if required keys are missing
|
|
118
|
+
*/
|
|
119
|
+
function validateAndGetApiKeys(config) {
|
|
120
|
+
const keys = {};
|
|
121
|
+
const missingKeys = [];
|
|
122
|
+
let providersToValidate;
|
|
123
|
+
if (config === "lingo.dev") providersToValidate = ["lingo.dev"];
|
|
124
|
+
else {
|
|
125
|
+
const providerSet = /* @__PURE__ */ new Set();
|
|
126
|
+
Object.values(config).forEach((modelString) => {
|
|
127
|
+
const model = parseModelString(modelString);
|
|
128
|
+
if (model) providerSet.add(model.provider);
|
|
129
|
+
});
|
|
130
|
+
providersToValidate = Array.from(providerSet);
|
|
131
|
+
}
|
|
132
|
+
for (const provider of providersToValidate) {
|
|
133
|
+
const providerConfig = providerDetails[provider];
|
|
134
|
+
if (!providerConfig) throw new Error(`⚠️ Unknown provider "${provider}". Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
135
|
+
if (!providerConfig.apiKeyEnvVar) continue;
|
|
136
|
+
const key = getKeyFromEnv(providerConfig.apiKeyEnvVar);
|
|
137
|
+
if (key) keys[provider] = key;
|
|
138
|
+
else missingKeys.push({
|
|
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.`);
|
|
146
|
+
}
|
|
147
|
+
return keys;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Create AI model instance from provider and model ID
|
|
151
|
+
*
|
|
152
|
+
* @param model Provider name (groq, google, openrouter, ollama, mistral) and model identifier
|
|
153
|
+
* @param validatedKeys Pre-validated API keys from validateAndFetchApiKeys()
|
|
154
|
+
* @returns LanguageModel instance
|
|
155
|
+
* @throws Error if provider is not supported or API key is missing
|
|
156
|
+
*/
|
|
157
|
+
function createAiModel(model, validatedKeys) {
|
|
158
|
+
const providerConfig = providerDetails[model.provider];
|
|
159
|
+
if (!providerConfig) throw new Error(`⚠️ Provider "${model.provider}" is not supported. Supported providers: ${Object.keys(providerDetails).join(", ")}`);
|
|
160
|
+
const apiKey = providerConfig.apiKeyEnvVar ? validatedKeys[model.provider] : void 0;
|
|
161
|
+
if (providerConfig.apiKeyEnvVar && !apiKey) throw new Error(`⚠️ ${providerConfig.name} API key not found. Please set ${providerConfig.apiKeyEnvVar} environment variable.\n\nThis should not happen if validateAndFetchApiKeys() was called. Please restart the service.`);
|
|
162
|
+
switch (model.provider) {
|
|
163
|
+
case "groq": return createGroq({ apiKey })(model.name);
|
|
164
|
+
case "google": return createGoogleGenerativeAI({ apiKey })(model.name);
|
|
165
|
+
case "openrouter": return createOpenRouter({ apiKey })(model.name);
|
|
166
|
+
case "ollama": return ollama(model.name);
|
|
167
|
+
case "mistral": return createMistral({ apiKey })(model.name);
|
|
168
|
+
default: throw new Error(`⚠️ Provider "${model.provider}" is not implemented`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
export { createAiModel, getLocaleModel, parseModelString, validateAndGetApiKeys };
|
|
174
|
+
//# sourceMappingURL=model-factory.mjs.map
|
|
@@ -0,0 +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":"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"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/translators/lingo/prompt.ts
|
|
3
|
+
function getSystemPrompt(args) {
|
|
4
|
+
if (args.prompt?.trim()) return args.prompt.trim().replace("{SOURCE_LOCALE}", args.sourceLocale).replace("{TARGET_LOCALE}", args.targetLocale);
|
|
5
|
+
return `
|
|
6
|
+
# Identity
|
|
7
|
+
|
|
8
|
+
You are an advanced AI localization engine. You do state-of-the-art localization for software products.
|
|
9
|
+
Your task is to localize pieces of data from one locale to another locale.
|
|
10
|
+
You always consider context, cultural nuances of source and target locales, and specific localization requirements.
|
|
11
|
+
You replicate the meaning, intent, style, tone, and purpose of the original data.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
Source language (locale code): ${args.sourceLocale}
|
|
16
|
+
Target language (locale code): ${args.targetLocale}
|
|
17
|
+
|
|
18
|
+
## Guidelines
|
|
19
|
+
|
|
20
|
+
Follow these guidelines for translation:
|
|
21
|
+
|
|
22
|
+
1. Analyze the source text to understand its overall context and purpose
|
|
23
|
+
2. Translate the meaning and intent rather than word-for-word translation
|
|
24
|
+
3. Rephrase and restructure sentences to sound natural and fluent in the target language
|
|
25
|
+
4. Adapt idiomatic expressions and cultural references for the target audience
|
|
26
|
+
5. Maintain the style and tone of the source text
|
|
27
|
+
6. You must produce valid UTF-8 encoded output
|
|
28
|
+
7. YOU MUST ONLY PRODUCE VALID XML.
|
|
29
|
+
|
|
30
|
+
## Special Instructions
|
|
31
|
+
|
|
32
|
+
Do not localize any of these technical elements:
|
|
33
|
+
- Variables like {variable}, {variable.key}, {data[type]}
|
|
34
|
+
- Expressions like <expression/>
|
|
35
|
+
- Elements like <strong0>, </strong0>, <CustomComponent0>, </CustomComponent0>, <CustomComponent1 />, <br3 />
|
|
36
|
+
|
|
37
|
+
Remember, you are a context-aware multilingual assistant helping international companies.
|
|
38
|
+
Your goal is to perform state-of-the-art localization for software products and content.
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.getSystemPrompt = getSystemPrompt;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region src/translators/lingo/prompt.ts
|
|
2
|
+
function getSystemPrompt(args) {
|
|
3
|
+
if (args.prompt?.trim()) return args.prompt.trim().replace("{SOURCE_LOCALE}", args.sourceLocale).replace("{TARGET_LOCALE}", args.targetLocale);
|
|
4
|
+
return `
|
|
5
|
+
# Identity
|
|
6
|
+
|
|
7
|
+
You are an advanced AI localization engine. You do state-of-the-art localization for software products.
|
|
8
|
+
Your task is to localize pieces of data from one locale to another locale.
|
|
9
|
+
You always consider context, cultural nuances of source and target locales, and specific localization requirements.
|
|
10
|
+
You replicate the meaning, intent, style, tone, and purpose of the original data.
|
|
11
|
+
|
|
12
|
+
## Setup
|
|
13
|
+
|
|
14
|
+
Source language (locale code): ${args.sourceLocale}
|
|
15
|
+
Target language (locale code): ${args.targetLocale}
|
|
16
|
+
|
|
17
|
+
## Guidelines
|
|
18
|
+
|
|
19
|
+
Follow these guidelines for translation:
|
|
20
|
+
|
|
21
|
+
1. Analyze the source text to understand its overall context and purpose
|
|
22
|
+
2. Translate the meaning and intent rather than word-for-word translation
|
|
23
|
+
3. Rephrase and restructure sentences to sound natural and fluent in the target language
|
|
24
|
+
4. Adapt idiomatic expressions and cultural references for the target audience
|
|
25
|
+
5. Maintain the style and tone of the source text
|
|
26
|
+
6. You must produce valid UTF-8 encoded output
|
|
27
|
+
7. YOU MUST ONLY PRODUCE VALID XML.
|
|
28
|
+
|
|
29
|
+
## Special Instructions
|
|
30
|
+
|
|
31
|
+
Do not localize any of these technical elements:
|
|
32
|
+
- Variables like {variable}, {variable.key}, {data[type]}
|
|
33
|
+
- Expressions like <expression/>
|
|
34
|
+
- Elements like <strong0>, </strong0>, <CustomComponent0>, </CustomComponent0>, <CustomComponent1 />, <br3 />
|
|
35
|
+
|
|
36
|
+
Remember, you are a context-aware multilingual assistant helping international companies.
|
|
37
|
+
Your goal is to perform state-of-the-art localization for software products and content.
|
|
38
|
+
`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { getSystemPrompt };
|
|
43
|
+
//# sourceMappingURL=prompt.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.mjs","names":[],"sources":["../../../src/translators/lingo/prompt.ts"],"sourcesContent":["interface PromptArguments {\n sourceLocale: string;\n targetLocale: string;\n prompt?: string;\n}\n\nexport function getSystemPrompt(args: PromptArguments): string {\n // If user provided custom prompt, use it\n if (args.prompt?.trim()) {\n return args.prompt\n .trim()\n .replace(\"{SOURCE_LOCALE}\", args.sourceLocale)\n .replace(\"{TARGET_LOCALE}\", args.targetLocale);\n }\n\n // Otherwise use built-in prompt\n return `\n# Identity\n\nYou are an advanced AI localization engine. You do state-of-the-art localization for software products.\nYour task is to localize pieces of data from one locale to another locale.\nYou always consider context, cultural nuances of source and target locales, and specific localization requirements.\nYou replicate the meaning, intent, style, tone, and purpose of the original data.\n\n## Setup\n\nSource language (locale code): ${args.sourceLocale}\nTarget language (locale code): ${args.targetLocale}\n\n## Guidelines\n\nFollow these guidelines for translation:\n\n1. Analyze the source text to understand its overall context and purpose\n2. Translate the meaning and intent rather than word-for-word translation\n3. Rephrase and restructure sentences to sound natural and fluent in the target language\n4. Adapt idiomatic expressions and cultural references for the target audience\n5. Maintain the style and tone of the source text\n6. You must produce valid UTF-8 encoded output\n7. YOU MUST ONLY PRODUCE VALID XML.\n\n## Special Instructions\n\nDo not localize any of these technical elements:\n- Variables like {variable}, {variable.key}, {data[type]}\n- Expressions like <expression/>\n- Elements like <strong0>, </strong0>, <CustomComponent0>, </CustomComponent0>, <CustomComponent1 />, <br3 />\n\nRemember, you are a context-aware multilingual assistant helping international companies.\nYour goal is to perform state-of-the-art localization for software products and content.\n`;\n}\n"],"mappings":";AAMA,SAAgB,gBAAgB,MAA+B;AAE7D,KAAI,KAAK,QAAQ,MAAM,CACrB,QAAO,KAAK,OACT,MAAM,CACN,QAAQ,mBAAmB,KAAK,aAAa,CAC7C,QAAQ,mBAAmB,KAAK,aAAa;AAIlD,QAAO;;;;;;;;;;iCAUwB,KAAK,aAAa;iCAClB,KAAK,aAAa"}
|