@rimori/client 2.5.31-next.0 → 2.5.31-next.1

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.
@@ -11,6 +11,7 @@ export declare class Translator {
11
11
  private translationUrl;
12
12
  private ai;
13
13
  private aiTranslationCache;
14
+ private aiTranslationPending;
14
15
  constructor(initialLanguage: string, translationUrl: string, ai: AIModule);
15
16
  /**
16
17
  * Initialize translator with user's language
@@ -14,6 +14,7 @@ import { createInstance } from 'i18next';
14
14
  export class Translator {
15
15
  constructor(initialLanguage, translationUrl, ai) {
16
16
  this.aiTranslationCache = new Map();
17
+ this.aiTranslationPending = new Map();
17
18
  this.currentLanguage = initialLanguage;
18
19
  this.initializationState = 'not-inited';
19
20
  this.initializationPromise = null;
@@ -158,29 +159,38 @@ export class Translator {
158
159
  const cached = this.aiTranslationCache.get(text);
159
160
  if (cached)
160
161
  return cached;
161
- try {
162
- // If the current language is English, don't translate
163
- if (!this.ai || this.currentLanguage === 'en')
164
- return text;
165
- const response = yield this.ai.getObject({
166
- prompt: 'global.translator.translate',
167
- variables: {
168
- additionalInstructions: additionalInstructions !== null && additionalInstructions !== void 0 ? additionalInstructions : '',
169
- language: this.currentLanguage,
170
- text,
171
- },
172
- cache: true,
173
- });
174
- const translation = response === null || response === void 0 ? void 0 : response.translation;
175
- if (translation) {
176
- this.aiTranslationCache.set(text, translation);
177
- return translation;
162
+ const pending = this.aiTranslationPending.get(text);
163
+ if (pending)
164
+ return pending;
165
+ if (!this.ai || this.currentLanguage === 'en')
166
+ return text;
167
+ const promise = (() => __awaiter(this, void 0, void 0, function* () {
168
+ try {
169
+ const response = yield this.ai.getObject({
170
+ prompt: 'global.translator.translate',
171
+ variables: {
172
+ additionalInstructions: additionalInstructions !== null && additionalInstructions !== void 0 ? additionalInstructions : '',
173
+ language: this.currentLanguage,
174
+ text,
175
+ },
176
+ cache: true,
177
+ });
178
+ const translation = response === null || response === void 0 ? void 0 : response.translation;
179
+ if (translation) {
180
+ this.aiTranslationCache.set(text, translation);
181
+ return translation;
182
+ }
178
183
  }
179
- }
180
- catch (error) {
181
- console.warn('Failed to translate freeform text:', { text, error });
182
- }
183
- return text;
184
+ catch (error) {
185
+ console.warn('Failed to translate freeform text:', { text, error });
186
+ }
187
+ finally {
188
+ this.aiTranslationPending.delete(text);
189
+ }
190
+ return text;
191
+ }))();
192
+ this.aiTranslationPending.set(text, promise);
193
+ return promise;
184
194
  });
185
195
  }
186
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimori/client",
3
- "version": "2.5.31-next.0",
3
+ "version": "2.5.31-next.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {