@rimori/client 2.5.8-next.2 → 2.5.8-next.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 CHANGED
@@ -119,7 +119,7 @@ Access metadata and settings through `client.plugin`:
119
119
  - `plugin.getSettings(defaults)` / `plugin.setSettings(settings)` – persist configuration.
120
120
  - `plugin.getPluginInfo()` – read active/installed plugin information.
121
121
  - `plugin.getUserInfo()` – obtain user profile details (language, name, guild, etc.).
122
- - `plugin.getTranslator()` – lazily initialize the translator for manual i18n.
122
+ - `plugin.getTranslator()` – lazily initialize the translator for manual i18n. Translation keys must follow the dot notation (`section.subsection.key`); non-key strings are translated via AI and cached.
123
123
 
124
124
  ### Database Access
125
125
 
@@ -183,7 +183,7 @@ The controller handles topic generation, metadata, and completion tracking autom
183
183
  Import additional helpers as needed:
184
184
 
185
185
  - `AudioController` – high-level audio playback/recording utilities for non-React environments.
186
- - `Translator` – encapsulated i18next integration for manual translation flows.
186
+ - `Translator` – encapsulated i18next integration for manual translation flows, with AI fallback for non-key strings.
187
187
  - `difficultyConverter` – convert between textual and numeric difficulty levels.
188
188
  - Type definitions for AI messages, shared content, triggers, accomplishments, and more.
189
189
 
@@ -1,4 +1,7 @@
1
1
  import { ThirdPartyModule, TOptions } from 'i18next';
2
+ import { ObjectRequest } from './ObjectController';
3
+ import { AIModule } from '../plugin/module/AIModule';
4
+ export type AIObjectGenerator = <T>(request: ObjectRequest) => Promise<T>;
2
5
  /**
3
6
  * Translator class for handling internationalization
4
7
  */
@@ -8,7 +11,10 @@ export declare class Translator {
8
11
  private initializationPromise;
9
12
  private i18n;
10
13
  private translationUrl;
11
- constructor(initialLanguage: string, translationUrl: string);
14
+ private ai;
15
+ private aiTranslationCache;
16
+ private aiTranslationInFlight;
17
+ constructor(initialLanguage: string, translationUrl: string, ai: AIModule);
12
18
  /**
13
19
  * Initialize translator with user's language
14
20
  * @param userLanguage - Language code from user info
@@ -23,8 +29,8 @@ export declare class Translator {
23
29
  */
24
30
  private fetchTranslations;
25
31
  /**
26
- * Get translation for a key
27
- * @param key - Translation key
32
+ * Get translation for a key or freeform text. If the key is not a valid translation key, the freeform text is translated using AI and cached.
33
+ * @param key - Translation key or freeform text
28
34
  * @param options - Translation options
29
35
  * @returns Translated string
30
36
  */
@@ -37,4 +43,7 @@ export declare class Translator {
37
43
  * Check if translator is initialized
38
44
  */
39
45
  isReady(): boolean;
46
+ private isTranslationKey;
47
+ private translateFreeformText;
48
+ private fetchAiTranslation;
40
49
  }
@@ -12,11 +12,14 @@ import { createInstance } from 'i18next';
12
12
  * Translator class for handling internationalization
13
13
  */
14
14
  export class Translator {
15
- constructor(initialLanguage, translationUrl) {
15
+ constructor(initialLanguage, translationUrl, ai) {
16
+ this.aiTranslationCache = new Map();
17
+ this.aiTranslationInFlight = new Set();
16
18
  this.currentLanguage = initialLanguage;
17
19
  this.initializationState = 'not-inited';
18
20
  this.initializationPromise = null;
19
21
  this.translationUrl = translationUrl;
22
+ this.ai = ai;
20
23
  }
21
24
  /**
22
25
  * Initialize translator with user's language
@@ -102,12 +105,15 @@ export class Translator {
102
105
  });
103
106
  }
104
107
  /**
105
- * Get translation for a key
106
- * @param key - Translation key
108
+ * Get translation for a key or freeform text. If the key is not a valid translation key, the freeform text is translated using AI and cached.
109
+ * @param key - Translation key or freeform text
107
110
  * @param options - Translation options
108
111
  * @returns Translated string
109
112
  */
110
113
  t(key, options) {
114
+ if (!this.isTranslationKey(key)) {
115
+ return this.translateFreeformText(key);
116
+ }
111
117
  if (!this.i18n) {
112
118
  throw new Error('Translator is not initialized');
113
119
  }
@@ -125,4 +131,45 @@ export class Translator {
125
131
  isReady() {
126
132
  return this.initializationState === 'finished';
127
133
  }
134
+ isTranslationKey(key) {
135
+ return /^[^\s.]+(\.[^\s.]+)+$/.test(key);
136
+ }
137
+ translateFreeformText(text) {
138
+ const cached = this.aiTranslationCache.get(text);
139
+ if (cached)
140
+ return cached;
141
+ if (!this.ai || this.aiTranslationInFlight.has(text)) {
142
+ return text;
143
+ }
144
+ this.aiTranslationInFlight.add(text);
145
+ void this.fetchAiTranslation(text).finally(() => {
146
+ this.aiTranslationInFlight.delete(text);
147
+ });
148
+ return text;
149
+ }
150
+ fetchAiTranslation(text) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ try {
153
+ if (!this.ai)
154
+ return;
155
+ const response = yield this.ai.getObject({
156
+ behaviour: 'You are a translation engine. Return only the translated text.',
157
+ instructions: `Translate the following text into ${this.currentLanguage}: ${text}`,
158
+ tool: {
159
+ translation: {
160
+ type: 'string',
161
+ description: `The translation of the input text into ${this.currentLanguage}.`,
162
+ },
163
+ },
164
+ });
165
+ const translation = response === null || response === void 0 ? void 0 : response.translation;
166
+ if (translation) {
167
+ this.aiTranslationCache.set(text, translation);
168
+ }
169
+ }
170
+ catch (error) {
171
+ console.warn('Failed to translate freeform text:', { text, error });
172
+ }
173
+ });
174
+ }
128
175
  }
@@ -32,7 +32,7 @@ export class RimoriClient {
32
32
  this.ai = new AIModule(controller, info);
33
33
  this.event = new EventModule(info.pluginId);
34
34
  this.db = new DbModule(supabase, controller, info);
35
- this.plugin = new PluginModule(supabase, controller, info);
35
+ this.plugin = new PluginModule(supabase, controller, info, this.ai);
36
36
  this.exercise = new ExerciseModule(supabase, controller, info, this.event);
37
37
  controller.onUpdate((updatedInfo) => {
38
38
  this.rimoriInfo = updatedInfo;
@@ -3,6 +3,7 @@ import { RimoriCommunicationHandler, RimoriInfo } from '../CommunicationHandler'
3
3
  import { Translator } from '../../controller/TranslationController';
4
4
  import { ActivePlugin, Plugin } from '../../fromRimori/PluginTypes';
5
5
  import { SupabaseClient } from '../CommunicationHandler';
6
+ import { AIModule } from './AIModule';
6
7
  export type Theme = 'light' | 'dark' | 'system';
7
8
  export type ApplicationMode = 'main' | 'sidebar' | 'settings';
8
9
  /**
@@ -22,7 +23,7 @@ export declare class PluginModule {
22
23
  releaseChannel: string;
23
24
  applicationMode: ApplicationMode;
24
25
  theme: Theme;
25
- constructor(supabase: SupabaseClient, communicationHandler: RimoriCommunicationHandler, info: RimoriInfo);
26
+ constructor(supabase: SupabaseClient, communicationHandler: RimoriCommunicationHandler, info: RimoriInfo, ai: AIModule);
26
27
  /**
27
28
  * Set the settings for the plugin.
28
29
  * @param settings The settings to set.
@@ -14,14 +14,14 @@ import { Translator } from '../../controller/TranslationController';
14
14
  * Provides access to plugin settings, user info, and plugin information.
15
15
  */
16
16
  export class PluginModule {
17
- constructor(supabase, communicationHandler, info) {
17
+ constructor(supabase, communicationHandler, info, ai) {
18
18
  this.rimoriInfo = info;
19
19
  this.communicationHandler = communicationHandler;
20
20
  this.pluginId = info.pluginId;
21
21
  this.releaseChannel = info.releaseChannel;
22
22
  this.settingsController = new SettingsController(supabase, info.pluginId, info.guild);
23
23
  const currentPlugin = info.installedPlugins.find((plugin) => plugin.id === info.pluginId);
24
- this.translator = new Translator(info.interfaceLanguage, (currentPlugin === null || currentPlugin === void 0 ? void 0 : currentPlugin.endpoint) || '');
24
+ this.translator = new Translator(info.interfaceLanguage, (currentPlugin === null || currentPlugin === void 0 ? void 0 : currentPlugin.endpoint) || '', ai);
25
25
  this.communicationHandler.onUpdate((updatedInfo) => (this.rimoriInfo = updatedInfo));
26
26
  this.applicationMode = this.communicationHandler.getQueryParam('applicationMode');
27
27
  this.theme = this.communicationHandler.getQueryParam('rm_theme') || 'light';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimori/client",
3
- "version": "2.5.8-next.2",
3
+ "version": "2.5.8-next.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {