lingo.dev 0.79.5 → 0.80.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/build/cli.mjs CHANGED
@@ -158,7 +158,8 @@ function createAuthenticator(params) {
158
158
  return null;
159
159
  }
160
160
  return {
161
- email: payload.email
161
+ email: payload.email,
162
+ id: payload.id
162
163
  };
163
164
  }
164
165
  return null;
@@ -988,7 +989,6 @@ var show_default = new Command5().command("show").description("Prints out the cu
988
989
 
989
990
  // src/cli/cmd/i18n.ts
990
991
  import { bucketTypeSchema, localeCodeSchema, resolveOverriddenLocale as resolveOverriddenLocale3 } from "@lingo.dev/_spec";
991
- import { LingoDotDevEngine } from "@lingo.dev/_sdk";
992
992
  import { Command as Command6 } from "interactive-commander";
993
993
  import Z4 from "zod";
994
994
  import _20 from "lodash";
@@ -2742,7 +2742,8 @@ function variableExtractLoader(params) {
2742
2742
  return createLoader({
2743
2743
  pull: async (locale, input2) => {
2744
2744
  const result = {};
2745
- for (const [key, value] of Object.entries(input2)) {
2745
+ const inputValues = _16.omitBy(input2, _16.isEmpty);
2746
+ for (const [key, value] of Object.entries(inputValues)) {
2746
2747
  const matches = value.match(specifierPattern) || [];
2747
2748
  result[key] = result[key] || {
2748
2749
  value,
@@ -3057,8 +3058,8 @@ function createBucketLoader(bucketType, bucketPathPattern, options) {
3057
3058
  createPoLoader(),
3058
3059
  createFlatLoader(),
3059
3060
  createSyncLoader(),
3060
- createUnlocalizableLoader(options.isCacheRestore, options.returnUnlocalizedKeys),
3061
- createVariableLoader({ type: "python" })
3061
+ createVariableLoader({ type: "python" }),
3062
+ createUnlocalizableLoader(options.isCacheRestore, options.returnUnlocalizedKeys)
3062
3063
  );
3063
3064
  case "properties":
3064
3065
  return composeLoaders(
@@ -3090,8 +3091,8 @@ function createBucketLoader(bucketType, bucketPathPattern, options) {
3090
3091
  createXcodeXcstringsLoader(options.defaultLocale),
3091
3092
  createFlatLoader(),
3092
3093
  createSyncLoader(),
3093
- createUnlocalizableLoader(options.isCacheRestore, options.returnUnlocalizedKeys),
3094
- createVariableLoader({ type: "ieee" })
3094
+ createVariableLoader({ type: "ieee" }),
3095
+ createUnlocalizableLoader(options.isCacheRestore, options.returnUnlocalizedKeys)
3095
3096
  );
3096
3097
  case "yaml":
3097
3098
  return composeLoaders(
@@ -3320,6 +3321,152 @@ function _tryParseJSON(line) {
3320
3321
  }
3321
3322
  }
3322
3323
 
3324
+ // src/cli/processor/lingo.ts
3325
+ import { LingoDotDevEngine } from "@lingo.dev/_sdk";
3326
+ function createLingoLocalizer(params) {
3327
+ return async (input2, onProgress) => {
3328
+ const lingo = new LingoDotDevEngine({
3329
+ apiKey: params.apiKey,
3330
+ apiUrl: params.apiUrl
3331
+ });
3332
+ const result = await lingo.localizeObject(
3333
+ input2.processableData,
3334
+ {
3335
+ sourceLocale: input2.sourceLocale,
3336
+ targetLocale: input2.targetLocale,
3337
+ reference: {
3338
+ [input2.sourceLocale]: input2.sourceData,
3339
+ [input2.targetLocale]: input2.targetData
3340
+ }
3341
+ },
3342
+ onProgress
3343
+ );
3344
+ return result;
3345
+ };
3346
+ }
3347
+
3348
+ // src/cli/processor/openai.ts
3349
+ import { generateText } from "ai";
3350
+ function createBasicTranslator(model, systemPrompt) {
3351
+ return async (input2, onProgress) => {
3352
+ if (!process.env.OPENAI_API_KEY) {
3353
+ throw new Error("OPENAI_API_KEY is not set");
3354
+ }
3355
+ const response = await generateText({
3356
+ model,
3357
+ messages: [
3358
+ {
3359
+ role: "system",
3360
+ content: JSON.stringify({
3361
+ role: "system",
3362
+ content: systemPrompt.replaceAll("{source}", input2.sourceLocale).replaceAll("{target}", input2.targetLocale)
3363
+ })
3364
+ },
3365
+ {
3366
+ role: "user",
3367
+ content: JSON.stringify({
3368
+ sourceLocale: "en",
3369
+ targetLocale: "es",
3370
+ data: {
3371
+ message: "Hello, world!"
3372
+ }
3373
+ })
3374
+ },
3375
+ {
3376
+ role: "assistant",
3377
+ content: JSON.stringify({
3378
+ sourceLocale: "en",
3379
+ targetLocale: "es",
3380
+ data: {
3381
+ message: "Hola, mundo!"
3382
+ }
3383
+ })
3384
+ },
3385
+ {
3386
+ role: "user",
3387
+ content: JSON.stringify({
3388
+ sourceLocale: "en",
3389
+ targetLocale: "es",
3390
+ data: input2.processableData
3391
+ })
3392
+ }
3393
+ ]
3394
+ });
3395
+ const result = JSON.parse(response.text);
3396
+ return result;
3397
+ };
3398
+ }
3399
+
3400
+ // src/cli/processor/index.ts
3401
+ import { createOpenAI } from "@ai-sdk/openai";
3402
+ import { createAnthropic } from "@ai-sdk/anthropic";
3403
+ function createProcessor(provider, params) {
3404
+ if (!provider || provider.id === "lingo") {
3405
+ const result = createLingoLocalizer(params);
3406
+ return result;
3407
+ } else {
3408
+ const model = getPureModelProvider(provider);
3409
+ const result = createBasicTranslator(model, provider.prompt);
3410
+ return result;
3411
+ }
3412
+ }
3413
+ function getPureModelProvider(provider) {
3414
+ switch (provider?.id) {
3415
+ case "openai":
3416
+ if (!process.env.OPENAI_API_KEY) {
3417
+ throw new Error("OPENAI_API_KEY is not set.");
3418
+ }
3419
+ return createOpenAI({
3420
+ apiKey: process.env.OPENAI_API_KEY,
3421
+ baseURL: provider.baseUrl
3422
+ })(provider.model);
3423
+ case "anthropic":
3424
+ if (!process.env.ANTHROPIC_API_KEY) {
3425
+ throw new Error("ANTHROPIC_API_KEY is not set.");
3426
+ }
3427
+ return createAnthropic({
3428
+ apiKey: process.env.ANTHROPIC_API_KEY
3429
+ })(provider.model);
3430
+ default:
3431
+ throw new Error(`Unsupported provider: ${provider?.id}`);
3432
+ }
3433
+ }
3434
+
3435
+ // src/cli/utils/exp-backoff.ts
3436
+ function withExponentialBackoff(fn, maxAttempts = 3, baseDelay = 1e3) {
3437
+ return async (...args) => {
3438
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
3439
+ try {
3440
+ return await fn(...args);
3441
+ } catch (error) {
3442
+ if (attempt === maxAttempts - 1) throw error;
3443
+ const delay = baseDelay * Math.pow(2, attempt);
3444
+ await new Promise((resolve) => setTimeout(resolve, delay));
3445
+ }
3446
+ }
3447
+ throw new Error("Unreachable code");
3448
+ };
3449
+ }
3450
+
3451
+ // src/cli/utils/observability.ts
3452
+ import { PostHog } from "posthog-node";
3453
+ async function trackEvent(distinctId, event, properties) {
3454
+ if (process.env.DO_NOT_TRACK) {
3455
+ return;
3456
+ }
3457
+ const posthog = new PostHog("phc_eR0iSoQufBxNY36k0f0T15UvHJdTfHlh8rJcxsfhfXk", {
3458
+ host: "https://eu.i.posthog.com",
3459
+ flushAt: 1,
3460
+ flushInterval: 0
3461
+ });
3462
+ await posthog.capture({
3463
+ distinctId,
3464
+ event,
3465
+ properties
3466
+ });
3467
+ await posthog.shutdown();
3468
+ }
3469
+
3323
3470
  // src/cli/cmd/i18n.ts
3324
3471
  var i18n_default = new Command6().command("i18n").description("Run Localization engine").helpOption("-h, --help", "Show help").option("--locale <locale>", "Locale to process", (val, prev) => prev ? [...prev, val] : [val]).option("--bucket <bucket>", "Bucket to process", (val, prev) => prev ? [...prev, val] : [val]).option(
3325
3472
  "--key <key>",
@@ -3344,6 +3491,7 @@ var i18n_default = new Command6().command("i18n").description("Run Localization
3344
3491
  ]);
3345
3492
  }
3346
3493
  let hasErrors = false;
3494
+ let authId = null;
3347
3495
  try {
3348
3496
  ora.start("Loading configuration...");
3349
3497
  const i18nConfig = getConfig();
@@ -3354,7 +3502,12 @@ var i18n_default = new Command6().command("i18n").description("Run Localization
3354
3502
  ora.succeed("Localization configuration is valid");
3355
3503
  ora.start("Connecting to Lingo.dev Localization Engine...");
3356
3504
  const auth = await validateAuth(settings);
3505
+ authId = auth.id;
3357
3506
  ora.succeed(`Authenticated as ${auth.email}`);
3507
+ trackEvent(authId, "cmd.i18n.start", {
3508
+ i18nConfig,
3509
+ flags
3510
+ });
3358
3511
  let buckets = getBuckets(i18nConfig);
3359
3512
  if (flags.bucket?.length) {
3360
3513
  buckets = buckets.filter((bucket) => flags.bucket.includes(bucket.type));
@@ -3538,11 +3691,12 @@ var i18n_default = new Command6().command("i18n").description("Run Localization
3538
3691
  bucketOra.start(
3539
3692
  `[${sourceLocale} -> ${targetLocale}] [${Object.keys(processableData).length} entries] (0%) AI localization in progress...`
3540
3693
  );
3541
- const localizationEngine = createLocalizationEngineConnection({
3694
+ let processPayload = createProcessor(i18nConfig.provider, {
3542
3695
  apiKey: settings.auth.apiKey,
3543
3696
  apiUrl: settings.auth.apiUrl
3544
3697
  });
3545
- const processedTargetData = await localizationEngine.process(
3698
+ processPayload = withExponentialBackoff(processPayload, 3, 1e3);
3699
+ const processedTargetData = await processPayload(
3546
3700
  {
3547
3701
  sourceLocale,
3548
3702
  sourceData,
@@ -3616,11 +3770,19 @@ var i18n_default = new Command6().command("i18n").description("Run Localization
3616
3770
  if (flags.verbose) {
3617
3771
  ora.info("Cache file deleted.");
3618
3772
  }
3773
+ trackEvent(auth.id, "cmd.i18n.success", {
3774
+ i18nConfig,
3775
+ flags
3776
+ });
3619
3777
  } else {
3620
3778
  ora.warn("Localization completed with errors.");
3621
3779
  }
3622
3780
  } catch (error) {
3623
3781
  ora.fail(error.message);
3782
+ trackEvent(authId || "unknown", "cmd.i18n.error", {
3783
+ flags,
3784
+ error
3785
+ });
3624
3786
  process.exit(1);
3625
3787
  }
3626
3788
  });
@@ -3630,43 +3792,6 @@ function calculateDataDelta(args) {
3630
3792
  const result = _20.chain(args.sourceData).pickBy((value, key) => newKeys.includes(key) || updatedKeys.includes(key)).value();
3631
3793
  return result;
3632
3794
  }
3633
- async function retryWithExponentialBackoff(operation, maxAttempts, baseDelay = 1e3) {
3634
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
3635
- try {
3636
- return await operation();
3637
- } catch (error) {
3638
- if (attempt === maxAttempts - 1) throw error;
3639
- const delay = baseDelay * Math.pow(2, attempt);
3640
- await new Promise((resolve) => setTimeout(resolve, delay));
3641
- }
3642
- }
3643
- throw new Error("Unreachable code");
3644
- }
3645
- function createLocalizationEngineConnection(params) {
3646
- const engine = new LingoDotDevEngine({
3647
- apiKey: params.apiKey,
3648
- apiUrl: params.apiUrl
3649
- });
3650
- return {
3651
- process: async (args, onProgress) => {
3652
- return retryWithExponentialBackoff(
3653
- () => engine.localizeObject(
3654
- args.processableData,
3655
- {
3656
- sourceLocale: args.sourceLocale,
3657
- targetLocale: args.targetLocale,
3658
- reference: {
3659
- [args.sourceLocale]: args.sourceData,
3660
- [args.targetLocale]: args.targetData
3661
- }
3662
- },
3663
- onProgress
3664
- ),
3665
- params.maxRetries ?? 3
3666
- );
3667
- }
3668
- };
3669
- }
3670
3795
  function parseFlags(options) {
3671
3796
  return Z4.object({
3672
3797
  apiKey: Z4.string().optional(),
@@ -4620,7 +4745,7 @@ var ci_default = new Command10().command("ci").description("Run Lingo.dev CI/CD
4620
4745
  // package.json
4621
4746
  var package_default = {
4622
4747
  name: "lingo.dev",
4623
- version: "0.79.5",
4748
+ version: "0.80.0",
4624
4749
  description: "Lingo.dev CLI",
4625
4750
  private: false,
4626
4751
  publishConfig: {
@@ -4677,6 +4802,8 @@ var package_default = {
4677
4802
  author: "",
4678
4803
  license: "Apache-2.0",
4679
4804
  dependencies: {
4805
+ "@ai-sdk/anthropic": "^1.2.6",
4806
+ "@ai-sdk/openai": "^1.3.7",
4680
4807
  "@datocms/cma-client-node": "^3.4.0",
4681
4808
  "@gitbeaker/rest": "^39.34.3",
4682
4809
  "@inquirer/prompts": "^7.2.3",
@@ -4684,6 +4811,7 @@ var package_default = {
4684
4811
  "@lingo.dev/_spec": "workspace:*",
4685
4812
  "@modelcontextprotocol/sdk": "^1.5.0",
4686
4813
  "@paralleldrive/cuid2": "^2.2.2",
4814
+ ai: "^4.3.2",
4687
4815
  bitbucket: "^2.12.0",
4688
4816
  chalk: "^5.4.1",
4689
4817
  cors: "^2.8.5",
@@ -4720,6 +4848,7 @@ var package_default = {
4720
4848
  "p-limit": "^6.2.0",
4721
4849
  "php-array-reader": "^2.1.2",
4722
4850
  plist: "^3.1.0",
4851
+ "posthog-node": "^4.11.2",
4723
4852
  prettier: "^3.4.2",
4724
4853
  "properties-parser": "^0.6.0",
4725
4854
  slugify: "^1.6.6",