@rimori/client 2.4.0-next.4 → 2.4.0-next.5

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.
@@ -224,7 +224,7 @@ export class RimoriCommunicationHandler {
224
224
  if (event.data?.topic === 'global.supabase.requestAccess' && event.data?.eventId === eventId) {
225
225
  this.rimoriInfo = event.data.data;
226
226
  this.supabase = createClient(this.rimoriInfo!.url, this.rimoriInfo!.key, {
227
- accessToken: () => Promise.resolve(this.getToken()),
227
+ accessToken: () => Promise.resolve(this.rimoriInfo!.token),
228
228
  });
229
229
  self.onmessage = originalOnMessage; // Restore original handler
230
230
  resolve({ supabase: this.supabase, info: this.rimoriInfo! });
@@ -242,7 +242,7 @@ export class RimoriCommunicationHandler {
242
242
  // console.log({ data });
243
243
  this.rimoriInfo = data;
244
244
  this.supabase = createClient(this.rimoriInfo.url, this.rimoriInfo.key, {
245
- accessToken: () => Promise.resolve(this.getToken()),
245
+ accessToken: () => Promise.resolve(this.rimoriInfo!.token),
246
246
  });
247
247
  }
248
248
  }
@@ -250,72 +250,6 @@ export class RimoriCommunicationHandler {
250
250
  return { supabase: this.supabase!, info: this.rimoriInfo };
251
251
  }
252
252
 
253
- public async getToken(): Promise<string> {
254
- if (this.rimoriInfo && this.rimoriInfo.expiration && this.rimoriInfo.expiration > new Date()) {
255
- return this.rimoriInfo.token;
256
- }
257
-
258
- // If we don't have rimoriInfo, request it
259
- if (!this.rimoriInfo) {
260
- const { data } = await EventBus.request<RimoriInfo>(this.pluginId, 'global.supabase.requestAccess');
261
- this.rimoriInfo = data;
262
- return this.rimoriInfo.token;
263
- }
264
-
265
- // If token is expired, request fresh access
266
- const { data } = await EventBus.request<{ token: string; expiration: Date }>(
267
- this.pluginId,
268
- 'global.supabase.requestAccess',
269
- );
270
- this.rimoriInfo.token = data.token;
271
- this.rimoriInfo.expiration = data.expiration;
272
-
273
- return this.rimoriInfo.token;
274
- }
275
-
276
- /**
277
- * Gets the Supabase URL.
278
- * @returns The Supabase URL.
279
- * @deprecated All endpoints should use the backend URL instead.
280
- */
281
- public getSupabaseUrl(): string {
282
- if (!this.rimoriInfo) {
283
- throw new Error('Supabase info not found');
284
- }
285
-
286
- return this.rimoriInfo.url;
287
- }
288
-
289
- public getBackendUrl(): string {
290
- if (!this.rimoriInfo) {
291
- throw new Error('Rimori info not found');
292
- }
293
- return this.rimoriInfo.backendUrl;
294
- }
295
-
296
- public getGlobalEventTopic(preliminaryTopic: string): string {
297
- if (preliminaryTopic.startsWith('global.')) {
298
- return preliminaryTopic;
299
- }
300
- if (preliminaryTopic.startsWith('self.')) {
301
- return preliminaryTopic;
302
- }
303
- const topicParts = preliminaryTopic.split('.');
304
- if (topicParts.length === 3) {
305
- if (!topicParts[0].startsWith('pl') && topicParts[0] !== 'global') {
306
- throw new Error("The event topic must start with the plugin id or 'global'.");
307
- }
308
- return preliminaryTopic;
309
- } else if (topicParts.length > 3) {
310
- throw new Error(
311
- `The event topic must consist of 3 parts. <pluginId>.<topic area>.<action>. Received: ${preliminaryTopic}`,
312
- );
313
- }
314
-
315
- const topicRoot = this.rimoriInfo?.pluginId ?? 'global';
316
- return `${topicRoot}.${preliminaryTopic}`;
317
- }
318
-
319
253
  /**
320
254
  * Handles updates to RimoriInfo from rimori-main.
321
255
  * Updates the cached info and Supabase client, then notifies all registered callbacks.
@@ -1,334 +1,52 @@
1
- import { PostgrestQueryBuilder } from '@supabase/postgrest-js';
2
1
  import { SupabaseClient } from '@supabase/supabase-js';
3
- import { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
4
- import { generateText, Message, OnLLMResponse, streamChatGPT } from '../controller/AIController';
5
- import { generateObject, ObjectRequest } from '../controller/ObjectController';
6
- import { SettingsController, UserInfo } from '../controller/SettingsController';
7
2
  import {
8
3
  SharedContent,
9
4
  SharedContentController,
10
5
  SharedContentFilter,
11
6
  SharedContentObjectRequest,
12
7
  } from '../controller/SharedContentController';
13
- import { getSTTResponse, getTTSResponse } from '../controller/VoiceController';
14
- import { ExerciseController, CreateExerciseParams } from '../controller/ExerciseController';
15
- import { EventBus, EventBusMessage, EventHandler, EventPayload, EventListener } from '../fromRimori/EventBus';
16
- import { ActivePlugin, MainPanelAction, Plugin, Tool } from '../fromRimori/PluginTypes';
17
- import { AccomplishmentController, AccomplishmentPayload } from '../controller/AccomplishmentController';
18
8
  import { RimoriCommunicationHandler, RimoriInfo } from './CommunicationHandler';
19
- import { Translator } from '../controller/TranslationController';
20
9
  import { Logger } from './Logger';
10
+ import { PluginModule } from './module/PluginModule';
11
+ import { DbModule } from './module/DbModule';
12
+ import { EventModule } from './module/EventModule';
13
+ import { AIModule } from './module/AIModule';
14
+ import { ExerciseModule } from './module/ExerciseModule';
21
15
 
22
16
  // Add declaration for WorkerGlobalScope
23
17
  declare const WorkerGlobalScope: any;
24
18
 
25
19
  export class RimoriClient {
26
20
  private static instance: RimoriClient;
27
- private superbase: SupabaseClient;
28
21
  private pluginController: RimoriCommunicationHandler;
29
- private settingsController: SettingsController;
30
22
  private sharedContentController: SharedContentController;
31
- private exerciseController: ExerciseController;
32
- private accomplishmentHandler: AccomplishmentController;
23
+ public db: DbModule;
24
+ public event: EventModule;
25
+ public plugin: PluginModule;
26
+ public ai: AIModule;
27
+ public exercise: ExerciseModule;
33
28
  private rimoriInfo: RimoriInfo;
34
- private translator: Translator;
35
29
 
36
30
  private constructor(controller: RimoriCommunicationHandler, supabase: SupabaseClient, info: RimoriInfo) {
37
31
  this.rimoriInfo = info;
38
- this.superbase = supabase;
39
32
  this.pluginController = controller;
40
- this.exerciseController = new ExerciseController(supabase, this);
41
- this.accomplishmentHandler = new AccomplishmentController(info.pluginId);
42
- this.settingsController = new SettingsController(supabase, info.pluginId, info.guild);
43
33
  this.sharedContentController = new SharedContentController(supabase, this);
44
- const currentPlugin = info.installedPlugins.find((plugin) => plugin.id === info.pluginId);
45
- this.translator = new Translator(info.interfaceLanguage, currentPlugin?.endpoint || '');
34
+ this.ai = new AIModule(controller, info);
35
+ this.event = new EventModule(info.pluginId);
36
+ this.db = new DbModule(supabase, controller, info);
37
+ this.plugin = new PluginModule(supabase, controller, info);
38
+ this.exercise = new ExerciseModule(supabase, controller, info, this.event);
39
+
40
+ controller.onUpdate((updatedInfo) => {
41
+ this.rimoriInfo = updatedInfo;
42
+ });
46
43
 
47
44
  //only init logger in workers and on main plugin pages
48
- if (this.getQueryParam('applicationMode') !== 'sidebar') {
45
+ if (this.plugin.applicationMode !== 'sidebar') {
49
46
  Logger.getInstance(this);
50
47
  }
51
48
  }
52
49
 
53
- public get plugin() {
54
- return {
55
- pluginId: this.rimoriInfo.pluginId,
56
- /**
57
- * The release channel of this plugin installation.
58
- * Determines which database schema is used for plugin tables.
59
- */
60
- releaseChannel: this.rimoriInfo.releaseChannel,
61
- /**
62
- * Set the settings for the plugin.
63
- * @param settings The settings to set.
64
- */
65
- setSettings: async (settings: any): Promise<void> => {
66
- await this.settingsController.setSettings(settings);
67
- },
68
- /**
69
- * Get the settings for the plugin. T can be any type of settings, UserSettings or SystemSettings.
70
- * @param defaultSettings The default settings to use if no settings are found.
71
- * @param genericSettings The type of settings to get.
72
- * @returns The settings for the plugin.
73
- */
74
- getSettings: async <T extends object>(defaultSettings: T): Promise<T> => {
75
- return await this.settingsController.getSettings<T>(defaultSettings);
76
- },
77
- /**
78
- * Get the current user info.
79
- * Note: For reactive updates in React components, use the userInfo from useRimori() hook instead.
80
- * @returns The user info.
81
- */
82
- getUserInfo: (): UserInfo => {
83
- return this.rimoriInfo.profile;
84
- },
85
- /**
86
- * Register a callback to be notified when RimoriInfo is updated.
87
- * This is useful for reacting to changes in user info, tokens, or other rimori data.
88
- * @param callback - Function to call with the new RimoriInfo
89
- * @returns Cleanup function to unregister the callback
90
- */
91
- onRimoriInfoUpdate: (callback: (info: RimoriInfo) => void): (() => void) => {
92
- return this.pluginController.onUpdate(callback);
93
- },
94
- /**
95
- * Retrieves information about plugins, including:
96
- * - All installed plugins
97
- * - The currently active plugin in the main panel
98
- * - The currently active plugin in the side panel
99
- */
100
- getPluginInfo: (): {
101
- /**
102
- * All installed plugins.
103
- */
104
- installedPlugins: Plugin[];
105
- /**
106
- * The plugin that is loaded in the main panel.
107
- */
108
- mainPanelPlugin?: ActivePlugin;
109
- /**
110
- * The plugin that is loaded in the side panel.
111
- */
112
- sidePanelPlugin?: ActivePlugin;
113
- } => {
114
- return {
115
- installedPlugins: this.rimoriInfo.installedPlugins,
116
- mainPanelPlugin: this.rimoriInfo.mainPanelPlugin,
117
- sidePanelPlugin: this.rimoriInfo.sidePanelPlugin,
118
- };
119
- },
120
- /**
121
- * Get the translator for the plugin.
122
- * @returns The translator for the plugin.
123
- */
124
- getTranslator: async (): Promise<Translator> => {
125
- await this.translator.initialize();
126
- return this.translator;
127
- },
128
- };
129
- }
130
-
131
- public get db() {
132
- return {
133
- // private from<
134
- // TableName extends string & keyof GenericSchema['Tables'],
135
- // Table extends GenericSchema['Tables'][TableName],
136
- // >(relation: TableName): PostgrestQueryBuilder<GenericSchema, Table, TableName>;
137
- // private from<ViewName extends string & keyof GenericSchema['Views'], View extends GenericSchema['Views'][ViewName]>(
138
- // relation: ViewName,
139
- // ): PostgrestQueryBuilder<GenericSchema, View, ViewName>;
140
- from: <ViewName extends string & keyof GenericSchema['Views'], View extends GenericSchema['Views'][ViewName]>(
141
- relation: string,
142
- ): PostgrestQueryBuilder<GenericSchema, View, ViewName> => {
143
- const tableName = this.db.getTableName(relation);
144
- // Use the schema determined by rimori-main based on release channel
145
- // Global tables (starting with 'global_') remain in public schema
146
- // Plugin tables use the schema provided by rimori-main (plugins or plugins_alpha)
147
- if (relation.startsWith('global_')) {
148
- // Global tables stay in public schema
149
- return this.superbase.from(tableName);
150
- }
151
- // Plugin tables go to the schema provided by rimori-main
152
- return this.superbase.schema(this.rimoriInfo.dbSchema).from(tableName);
153
- },
154
- // storage: this.superbase.storage,
155
- // functions: this.superbase.functions,
156
- /**
157
- * The table prefix for of database tables of the plugin.
158
- */
159
- tablePrefix: this.rimoriInfo.tablePrefix,
160
- /**
161
- * The database schema used for plugin tables.
162
- * Determined by rimori-main based on release channel:
163
- * - 'plugins_alpha' for alpha release channel
164
- * - 'plugins' for beta and stable release channels
165
- */
166
- schema: this.rimoriInfo.dbSchema,
167
- /**
168
- * Get the table name for a given plugin table.
169
- * Internally all tables are prefixed with the plugin id. This function is used to get the correct table name for a given public table.
170
- * @param table The plugin table name to get the full table name for.
171
- * @returns The full table name.
172
- */
173
- getTableName: (table: string): string => {
174
- if (/[A-Z]/.test(table)) {
175
- throw new Error('Table name cannot include uppercase letters. Please use snake_case for table names.');
176
- }
177
- if (table.startsWith('global_')) {
178
- return table.replace('global_', '');
179
- }
180
- return this.db.tablePrefix + '_' + table;
181
- },
182
- };
183
- }
184
-
185
- public event = {
186
- /**
187
- * Emit an event to Rimori or a plugin.
188
- * The topic schema is:
189
- * {pluginId}.{eventId}
190
- * Check out the event bus documentation for more information.
191
- * For triggering events from Rimori like context menu actions use the "global" keyword.
192
- * @param topic The topic to emit the event on.
193
- * @param data The data to emit.
194
- * @param eventId The event id.
195
- */
196
- emit: (topic: string, data?: any, eventId?: number) => {
197
- const globalTopic = this.pluginController.getGlobalEventTopic(topic);
198
- EventBus.emit(this.plugin.pluginId, globalTopic, data, eventId);
199
- },
200
- /**
201
- * Request an event.
202
- * @param topic The topic to request the event on.
203
- * @param data The data to request.
204
- * @returns The response from the event.
205
- */
206
- request: <T>(topic: string, data?: any): Promise<EventBusMessage<T>> => {
207
- const globalTopic = this.pluginController.getGlobalEventTopic(topic);
208
- return EventBus.request<T>(this.plugin.pluginId, globalTopic, data);
209
- },
210
- /**
211
- * Subscribe to an event.
212
- * @param topic The topic to subscribe to.
213
- * @param callback The callback to call when the event is emitted.
214
- * @returns An EventListener object containing an off() method to unsubscribe the listeners.
215
- */
216
- on: <T = EventPayload>(topic: string | string[], callback: EventHandler<T>): EventListener => {
217
- const topics = Array.isArray(topic) ? topic : [topic];
218
- return EventBus.on<T>(
219
- topics.map((t) => this.pluginController.getGlobalEventTopic(t)),
220
- callback,
221
- );
222
- },
223
- /**
224
- * Subscribe to an event once.
225
- * @param topic The topic to subscribe to.
226
- * @param callback The callback to call when the event is emitted.
227
- */
228
- once: <T = EventPayload>(topic: string, callback: EventHandler<T>) => {
229
- EventBus.once<T>(this.pluginController.getGlobalEventTopic(topic), callback);
230
- },
231
- /**
232
- * Respond to an event.
233
- * @param topic The topic to respond to.
234
- * @param data The data to respond with.
235
- */
236
- respond: <T = EventPayload>(
237
- topic: string | string[],
238
- data: EventPayload | ((data: EventBusMessage<T>) => EventPayload | Promise<EventPayload>),
239
- ) => {
240
- const topics = Array.isArray(topic) ? topic : [topic];
241
- EventBus.respond(
242
- this.plugin.pluginId,
243
- topics.map((t) => this.pluginController.getGlobalEventTopic(t)),
244
- data,
245
- );
246
- },
247
- /**
248
- * Emit an accomplishment.
249
- * @param payload The payload to emit.
250
- */
251
- emitAccomplishment: (payload: AccomplishmentPayload) => {
252
- this.accomplishmentHandler.emitAccomplishment(payload);
253
- },
254
- /**
255
- * Subscribe to an accomplishment.
256
- * @param accomplishmentTopic The topic to subscribe to.
257
- * @param callback The callback to call when the accomplishment is emitted.
258
- */
259
- onAccomplishment: (
260
- accomplishmentTopic: string,
261
- callback: (payload: EventBusMessage<AccomplishmentPayload>) => void,
262
- ) => {
263
- this.accomplishmentHandler.subscribe(accomplishmentTopic, callback);
264
- },
265
- /**
266
- * Trigger an action that opens the sidebar and triggers an action in the designated plugin.
267
- * @param pluginId The id of the plugin to trigger the action for.
268
- * @param actionKey The key of the action to trigger.
269
- * @param text Optional text to be used for the action like for example text that the translator would look up.
270
- */
271
- emitSidebarAction: (pluginId: string, actionKey: string, text?: string) => {
272
- this.event.emit('global.sidebar.triggerAction', { plugin_id: pluginId, action_key: actionKey, text });
273
- },
274
-
275
- /**
276
- * Subscribe to main panel actions triggered by the user from the dashboard.
277
- * @param callback Handler function that receives the action data when a matching action is triggered.
278
- * @param actionsToListen Optional filter to listen only to specific action keys. If empty or not provided, all actions will trigger the callback.
279
- * @returns An EventListener object with an `off()` method for cleanup.
280
- *
281
- * @example
282
- * ```ts
283
- * const listener = client.event.onMainPanelAction((data) => {
284
- * console.log('Action received:', data.action_key);
285
- * }, ['startSession', 'pauseSession']);
286
- *
287
- * // Clean up when component unmounts to prevent events from firing
288
- * // when navigating away or returning to the page
289
- * useEffect(() => {
290
- * return () => listener.off();
291
- * }, []);
292
- * ```
293
- *
294
- * **Important:** Always call `listener.off()` when your component unmounts or when you no longer need to listen.
295
- * This prevents the event handler from firing when navigating away from or returning to the page, which could
296
- * cause unexpected behavior or duplicate event handling.
297
- */
298
- onMainPanelAction: (
299
- callback: (data: MainPanelAction) => void,
300
- actionsToListen: string | string[] = [],
301
- ): EventListener => {
302
- const listeningActions = Array.isArray(actionsToListen) ? actionsToListen : [actionsToListen];
303
- // this needs to be a emit and on because the main panel action is triggered by the user and not by the plugin
304
- this.event.emit('action.requestMain');
305
- return this.event.on<MainPanelAction>('action.requestMain', ({ data }) => {
306
- // console.log('Received action for main panel ' + data.action_key);
307
- // console.log('Listening to actions', listeningActions);
308
- if (listeningActions.length === 0 || listeningActions.includes(data.action_key)) {
309
- callback(data);
310
- }
311
- });
312
- },
313
-
314
- onSidePanelAction: (
315
- callback: (data: MainPanelAction) => void,
316
- actionsToListen: string | string[] = [],
317
- ): EventListener => {
318
- const listeningActions = Array.isArray(actionsToListen) ? actionsToListen : [actionsToListen];
319
- // this needs to be a emit and on because the main panel action is triggered by the user and not by the plugin
320
- this.event.emit('action.requestSidebar');
321
- return this.event.on<MainPanelAction>('action.requestSidebar', ({ data }) => {
322
- // console.log("eventHandler .onSidePanelAction", data);
323
- // console.log('Received action for sidebar ' + data.action);
324
- // console.log('Listening to actions', listeningActions);
325
- if (listeningActions.length === 0 || listeningActions.includes(data.action)) {
326
- callback(data);
327
- }
328
- });
329
- },
330
- };
331
-
332
50
  public navigation = {
333
51
  toDashboard: (): void => {
334
52
  this.event.emit('global.navigation.triggerToDashboard');
@@ -338,6 +56,7 @@ export class RimoriClient {
338
56
  /**
339
57
  * Get a query parameter value that was passed via MessageChannel
340
58
  * @param key The query parameter key
59
+ * @deprecated Use the plugin.applicationMode and plugin.theme properties instead
341
60
  * @returns The query parameter value or null if not found
342
61
  */
343
62
  public getQueryParam(key: string): string | null {
@@ -361,40 +80,13 @@ export class RimoriClient {
361
80
  return RimoriClient.instance;
362
81
  }
363
82
 
364
- public ai = {
365
- getText: async (messages: Message[], tools?: Tool[]): Promise<string> => {
366
- const token = await this.pluginController.getToken();
367
- return generateText(this.pluginController.getBackendUrl(), messages, tools || [], token).then(
368
- ({ messages }) => messages[0].content[0].text,
369
- );
370
- },
371
- getSteamedText: async (messages: Message[], onMessage: OnLLMResponse, tools?: Tool[]) => {
372
- const token = await this.pluginController.getToken();
373
- streamChatGPT(this.pluginController.getBackendUrl(), messages, tools || [], onMessage, token);
374
- },
375
- getVoice: async (text: string, voice = 'alloy', speed = 1, language?: string): Promise<Blob> => {
376
- const token = await this.pluginController.getToken();
377
- return getTTSResponse(this.pluginController.getBackendUrl(), { input: text, voice, speed, language }, token);
378
- },
379
- getTextFromVoice: async (file: Blob): Promise<string> => {
380
- const token = await this.pluginController.getToken();
381
- return getSTTResponse(this.pluginController.getBackendUrl(), file, token);
382
- },
383
- getObject: async <T = any>(request: ObjectRequest): Promise<T> => {
384
- const token = await this.pluginController.getToken();
385
- return generateObject<T>(this.pluginController.getBackendUrl(), request, token);
386
- },
387
- // getSteamedObject: this.generateObjectStream,
388
- };
389
-
390
83
  public runtime = {
391
84
  fetchBackend: async (url: string, options: RequestInit) => {
392
- const token = await this.pluginController.getToken();
393
- return fetch(this.pluginController.getBackendUrl() + url, {
85
+ return fetch(this.rimoriInfo.backendUrl + url, {
394
86
  ...options,
395
87
  headers: {
396
88
  ...options.headers,
397
- Authorization: `Bearer ${token}`,
89
+ Authorization: `Bearer ${this.rimoriInfo.token}`,
398
90
  },
399
91
  });
400
92
  },
@@ -504,39 +196,4 @@ export class RimoriClient {
504
196
  },
505
197
  },
506
198
  };
507
-
508
- public exercise = {
509
- /**
510
- * Fetches weekly exercises from the weekly_exercises view.
511
- * Shows exercises for the current week that haven't expired.
512
- * @returns Array of exercise objects.
513
- */
514
- view: async () => {
515
- return this.exerciseController.viewWeeklyExercises();
516
- },
517
-
518
- /**
519
- * Creates a new exercise or multiple exercises via the backend API.
520
- * When creating multiple exercises, all requests are made in parallel but only one event is emitted.
521
- * @param params Exercise creation parameters (single or array).
522
- * @returns Created exercise objects.
523
- */
524
- add: async (params: CreateExerciseParams | CreateExerciseParams[]) => {
525
- const token = await this.pluginController.getToken();
526
- const backendUrl = this.pluginController.getBackendUrl();
527
- const exercises = Array.isArray(params) ? params : [params];
528
- return this.exerciseController.addExercise(token, backendUrl, exercises);
529
- },
530
-
531
- /**
532
- * Deletes an exercise via the backend API.
533
- * @param id The exercise ID to delete.
534
- * @returns Success status.
535
- */
536
- delete: async (id: string) => {
537
- const token = await this.pluginController.getToken();
538
- const backendUrl = this.pluginController.getBackendUrl();
539
- return this.exerciseController.deleteExercise(token, backendUrl, id);
540
- },
541
- };
542
199
  }
@@ -0,0 +1,77 @@
1
+ import { RimoriCommunicationHandler, RimoriInfo } from '../CommunicationHandler';
2
+ import { generateText, Message, OnLLMResponse, streamChatGPT } from '../../controller/AIController';
3
+ import { generateObject, ObjectRequest } from '../../controller/ObjectController';
4
+ import { getSTTResponse, getTTSResponse } from '../../controller/VoiceController';
5
+ import { Tool } from '../../fromRimori/PluginTypes';
6
+
7
+ /**
8
+ * Controller for AI-related operations.
9
+ * Provides access to text generation, voice synthesis, and object generation.
10
+ */
11
+ export class AIModule {
12
+ private communicationHandler: RimoriCommunicationHandler;
13
+ private backendUrl: string;
14
+ private token: string;
15
+
16
+ constructor(communicationHandler: RimoriCommunicationHandler, info: RimoriInfo) {
17
+ this.token = info.token;
18
+ this.backendUrl = info.backendUrl;
19
+ this.communicationHandler = communicationHandler;
20
+
21
+ this.communicationHandler.onUpdate((updatedInfo) => {
22
+ this.token = updatedInfo.token;
23
+ });
24
+ }
25
+
26
+ /**
27
+ * Generate text from messages using AI.
28
+ * @param messages The messages to generate text from.
29
+ * @param tools Optional tools to use for generation.
30
+ * @returns The generated text.
31
+ */
32
+ async getText(messages: Message[], tools?: Tool[]): Promise<string> {
33
+ return generateText(this.backendUrl, messages, tools || [], this.token).then(
34
+ ({ messages }) => messages[0].content[0].text,
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Stream text generation from messages using AI.
40
+ * @param messages The messages to generate text from.
41
+ * @param onMessage Callback for each message chunk.
42
+ * @param tools Optional tools to use for generation.
43
+ */
44
+ async getSteamedText(messages: Message[], onMessage: OnLLMResponse, tools?: Tool[]): Promise<void> {
45
+ streamChatGPT(this.backendUrl, messages, tools || [], onMessage, this.token);
46
+ }
47
+
48
+ /**
49
+ * Generate voice audio from text using AI.
50
+ * @param text The text to convert to voice.
51
+ * @param voice The voice to use (default: 'alloy').
52
+ * @param speed The speed of the voice (default: 1).
53
+ * @param language Optional language for the voice.
54
+ * @returns The generated audio as a Blob.
55
+ */
56
+ async getVoice(text: string, voice = 'alloy', speed = 1, language?: string): Promise<Blob> {
57
+ return getTTSResponse(this.backendUrl, { input: text, voice, speed, language }, this.token);
58
+ }
59
+
60
+ /**
61
+ * Convert voice audio to text using AI.
62
+ * @param file The audio file to convert.
63
+ * @returns The transcribed text.
64
+ */
65
+ async getTextFromVoice(file: Blob): Promise<string> {
66
+ return getSTTResponse(this.backendUrl, file, this.token);
67
+ }
68
+
69
+ /**
70
+ * Generate a structured object from a request using AI.
71
+ * @param request The object generation request.
72
+ * @returns The generated object.
73
+ */
74
+ async getObject<T = any>(request: ObjectRequest): Promise<T> {
75
+ return generateObject<T>(this.backendUrl, request, this.token);
76
+ }
77
+ }
@@ -0,0 +1,66 @@
1
+ import { PostgrestQueryBuilder } from '@supabase/postgrest-js';
2
+ import { SupabaseClient } from '@supabase/supabase-js';
3
+ import { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
4
+ import { RimoriCommunicationHandler, RimoriInfo } from '../CommunicationHandler';
5
+
6
+ /**
7
+ * Database module for plugin database operations.
8
+ * Provides access to plugin tables with automatic prefixing and schema management.
9
+ */
10
+ export class DbModule {
11
+ private supabase: SupabaseClient;
12
+ private rimoriInfo: RimoriInfo;
13
+ public tablePrefix: string;
14
+ public schema: string;
15
+
16
+ constructor(supabase: SupabaseClient, communicationHandler: RimoriCommunicationHandler, info: RimoriInfo) {
17
+ this.supabase = supabase;
18
+ this.rimoriInfo = info;
19
+ this.tablePrefix = info.tablePrefix;
20
+ this.schema = info.dbSchema;
21
+
22
+ communicationHandler.onUpdate((updatedInfo) => {
23
+ this.rimoriInfo = updatedInfo;
24
+ this.tablePrefix = updatedInfo.tablePrefix;
25
+ this.schema = updatedInfo.dbSchema;
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Query a database table.
31
+ * Global tables (starting with 'global_') remain in public schema.
32
+ * Plugin tables use the schema provided by rimori-main (plugins or plugins_alpha).
33
+ * @param relation The table name (without prefix for plugin tables, with 'global_' for global tables).
34
+ * @returns A Postgrest query builder for the table.
35
+ */
36
+ from<ViewName extends string & keyof GenericSchema['Views'], View extends GenericSchema['Views'][ViewName]>(
37
+ relation: string,
38
+ ): PostgrestQueryBuilder<GenericSchema, View, ViewName> {
39
+ const tableName = this.getTableName(relation);
40
+ // Use the schema determined by rimori-main based on release channel
41
+ // Global tables (starting with 'global_') remain in public schema
42
+ // Plugin tables use the schema provided by rimori-main (plugins or plugins_alpha)
43
+ if (relation.startsWith('global_')) {
44
+ // Global tables stay in public schema
45
+ return this.supabase.from(tableName);
46
+ }
47
+ // Plugin tables go to the schema provided by rimori-main
48
+ return this.supabase.schema(this.rimoriInfo.dbSchema).from(tableName);
49
+ }
50
+
51
+ /**
52
+ * Get the table name for a given plugin table.
53
+ * Internally all tables are prefixed with the plugin id. This function is used to get the correct table name for a given public table.
54
+ * @param table The plugin table name to get the full table name for.
55
+ * @returns The full table name.
56
+ */
57
+ getTableName(table: string): string {
58
+ if (/[A-Z]/.test(table)) {
59
+ throw new Error('Table name cannot include uppercase letters. Please use snake_case for table names.');
60
+ }
61
+ if (table.startsWith('global_')) {
62
+ return table.replace('global_', '');
63
+ }
64
+ return this.tablePrefix + '_' + table;
65
+ }
66
+ }