@rimori/client 2.5.5 → 2.5.6-next.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.
@@ -1,288 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- export class SharedContentController {
11
- constructor(supabase, rimoriClient) {
12
- this.supabase = supabase;
13
- this.rimoriClient = rimoriClient;
14
- }
15
- /**
16
- * Fetch new shared content for a given content type.
17
- * @param contentType - The type of content to fetch.
18
- * @param generatorInstructions - The instructions for the generator. The object needs to have a tool property with a topic and keywords property to let a new unique topic be generated.
19
- * @param filter - An optional filter to apply to the query.
20
- * @param options - Optional options.
21
- * @param options.privateTopic - If the topic should be private and only be visible to the user.
22
- * @param options.skipDbSave - If true, do not persist a newly generated content to the DB (default false).
23
- * @param options.alwaysGenerateNew - If true, always generate a new content even if there is already a content with the same filter.
24
- * @param options.excludeIds - Optional list of shared_content ids to exclude from selection.
25
- * @returns The new shared content.
26
- */
27
- getNewSharedContent(contentType, generatorInstructions,
28
- //this filter is there if the content should be filtered additionally by a column and value
29
- filter, options) {
30
- return __awaiter(this, void 0, void 0, function* () {
31
- // The db cache of the shared content is temporary disabled until the new shared content implementation is completed
32
- // if (false) {
33
- // let query = this.supabase
34
- // .from('shared_content')
35
- // .select('*, scc:shared_content_completed(id, state)')
36
- // .eq('content_type', contentType)
37
- // .not('scc.state', 'in', '("completed","ongoing","hidden")')
38
- // .is('deleted_at', null);
39
- // if (options?.excludeIds?.length ?? 0 > 0) {
40
- // const excludeIds = options.excludeIds.filter((id) => !id.startsWith('internal-temp-id-'));
41
- // // Supabase expects raw PostgREST syntax like '("id1","id2")'.
42
- // const excludeList = `(${excludeIds.map((id) => `"${id}"`).join(',')})`;
43
- // query = query.not('id', 'in', excludeList);
44
- // }
45
- // if (filter) {
46
- // query.contains('data', filter);
47
- // }
48
- // const { data: newAssignments, error } = await query.limit(30);
49
- // if (error) {
50
- // console.error('error fetching new assignments:', error);
51
- // throw new Error('error fetching new assignments');
52
- // }
53
- // // console.log('newAssignments:', newAssignments);
54
- // if (!options?.alwaysGenerateNew && newAssignments.length > 0) {
55
- // const index = Math.floor(Math.random() * newAssignments.length);
56
- // return newAssignments[index];
57
- // }
58
- // }
59
- const instructions = yield this.generateNewAssignment(contentType, generatorInstructions, filter);
60
- console.log('instructions:', instructions);
61
- //create the shared content object
62
- const data = {
63
- id: 'internal-temp-id-' + Math.random().toString(36).substring(2, 15),
64
- contentType,
65
- title: instructions.title,
66
- keywords: instructions.keywords.map(({ text }) => text),
67
- data: Object.assign(Object.assign(Object.assign({}, instructions), { title: undefined, keywords: undefined }), generatorInstructions.fixedProperties),
68
- privateTopic: options === null || options === void 0 ? void 0 : options.privateTopic,
69
- };
70
- if (options === null || options === void 0 ? void 0 : options.skipDbSave) {
71
- return data;
72
- }
73
- return yield this.createSharedContent(data);
74
- });
75
- }
76
- generateNewAssignment(contentType, generatorInstructions, filter) {
77
- return __awaiter(this, void 0, void 0, function* () {
78
- const fullInstructions = yield this.getGeneratorInstructions(contentType, generatorInstructions, filter);
79
- console.log('fullInstructions:', fullInstructions);
80
- return yield this.rimoriClient.ai.getObject(fullInstructions);
81
- });
82
- }
83
- getGeneratorInstructions(contentType, generatorInstructions, filter) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- const completedTopics = yield this.getCompletedTopics(contentType, filter);
86
- generatorInstructions.instructions += `
87
- The following topics are already taken: ${completedTopics.join(', ')}`;
88
- generatorInstructions.tool.title = {
89
- type: 'string',
90
- description: 'What the topic is about. Short. ',
91
- };
92
- generatorInstructions.tool.keywords = {
93
- type: [{ text: { type: 'string' } }],
94
- description: 'Keywords around the topic of the assignment.',
95
- };
96
- return generatorInstructions;
97
- });
98
- }
99
- getCompletedTopics(contentType, filter) {
100
- return __awaiter(this, void 0, void 0, function* () {
101
- const query = this.supabase
102
- .from('shared_content')
103
- .select('title, keywords, scc:shared_content_completed(id)')
104
- .eq('content_type', contentType)
105
- .not('scc.id', 'is', null)
106
- .is('deleted_at', null);
107
- if (filter) {
108
- query.contains('data', filter);
109
- }
110
- const { data: oldAssignments, error } = yield query;
111
- if (error) {
112
- console.error('error fetching old assignments:', error);
113
- return [];
114
- }
115
- return oldAssignments.map(({ title, keywords }) => `${title}(${keywords.join(',')})`);
116
- });
117
- }
118
- getSharedContent(contentType, id) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const { data, error } = yield this.supabase
121
- .from('shared_content')
122
- .select()
123
- .eq('content_type', contentType)
124
- .eq('id', id)
125
- .is('deleted_at', null)
126
- .single();
127
- if (error) {
128
- console.error('error fetching shared content:', error);
129
- throw new Error('error fetching shared content');
130
- }
131
- return data;
132
- });
133
- }
134
- completeSharedContent(contentType, assignmentId) {
135
- return __awaiter(this, void 0, void 0, function* () {
136
- // Idempotent completion: upsert on (id, user_id) so repeated calls don't fail
137
- const { error } = yield this.supabase
138
- .from('shared_content_completed')
139
- .upsert({ content_type: contentType, id: assignmentId }, { onConflict: 'id' });
140
- if (error) {
141
- console.error('error completing shared content:', error);
142
- throw new Error('error completing shared content');
143
- }
144
- });
145
- }
146
- /**
147
- * Update state details for a shared content entry in shared_content_completed.
148
- * Assumes table has columns: state ('completed'|'ongoing'|'hidden'), reaction ('liked'|'disliked'|null), bookmarked boolean.
149
- * Upserts per (id, content_type, user).
150
- * @param param
151
- * @param param.contentType - The content type.
152
- * @param param.id - The shared content id.
153
- * @param param.state - The state to set.
154
- * @param param.reaction - Optional reaction.
155
- * @param param.bookmarked - Optional bookmark flag.
156
- */
157
- updateSharedContentState(_a) {
158
- return __awaiter(this, arguments, void 0, function* ({ contentType, id, state, reaction, bookmarked, }) {
159
- const payload = { content_type: contentType, id };
160
- if (state !== undefined)
161
- payload.state = state;
162
- if (reaction !== undefined)
163
- payload.reaction = reaction;
164
- if (bookmarked !== undefined)
165
- payload.bookmarked = bookmarked;
166
- // Prefer upsert, fall back to insert/update if upsert not allowed
167
- const { error } = yield this.supabase.from('shared_content_completed').upsert(payload, { onConflict: 'id' });
168
- if (error) {
169
- console.error('error updating shared content state:', error);
170
- throw new Error('error updating shared content state');
171
- }
172
- });
173
- }
174
- /**
175
- * Fetch shared content from the database based on optional filters.
176
- * @param contentType - The type of content to fetch.
177
- * @param filter - Optional filter to apply to the query.
178
- * @param limit - Optional limit for the number of results.
179
- * @returns Array of shared content matching the criteria.
180
- */
181
- getSharedContentList(contentType, filter, limit) {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- const query = this.supabase
184
- .from('shared_content')
185
- .select('*')
186
- .eq('content_type', contentType)
187
- .is('deleted_at', null)
188
- .limit(limit !== null && limit !== void 0 ? limit : 30);
189
- if (filter) {
190
- query.contains('data', filter);
191
- }
192
- const { data, error } = yield query;
193
- if (error) {
194
- console.error('error fetching shared content:', error);
195
- throw new Error('error fetching shared content');
196
- }
197
- return data;
198
- });
199
- }
200
- /**
201
- * Insert new shared content into the database.
202
- * @param param
203
- * @param param.contentType - The type of content to insert.
204
- * @param param.title - The title of the content.
205
- * @param param.keywords - Keywords associated with the content.
206
- * @param param.data - The content data to store.
207
- * @param param.privateTopic - Optional flag to indicate if the topic should be private.
208
- * @returns The inserted shared content.
209
- * @throws {Error} if insertion fails.
210
- */
211
- createSharedContent(_a) {
212
- return __awaiter(this, arguments, void 0, function* ({ contentType, title, keywords, data, privateTopic, }) {
213
- const { data: newContent, error } = yield this.supabase
214
- .from('shared_content')
215
- .insert({
216
- private: privateTopic,
217
- content_type: contentType,
218
- title,
219
- keywords,
220
- data,
221
- })
222
- .select();
223
- if (error) {
224
- console.error('error inserting shared content:', error);
225
- throw new Error('error inserting shared content');
226
- }
227
- return newContent[0];
228
- });
229
- }
230
- /**
231
- * Update existing shared content in the database.
232
- * @param id - The ID of the content to update.
233
- * @param updates - The updates to apply to the shared content.
234
- * @returns The updated shared content.
235
- * @throws {Error} if update fails.
236
- */
237
- updateSharedContent(id, updates) {
238
- return __awaiter(this, void 0, void 0, function* () {
239
- const updateData = {};
240
- if (updates.contentType)
241
- updateData.content_type = updates.contentType;
242
- if (updates.title)
243
- updateData.title = updates.title;
244
- if (updates.keywords)
245
- updateData.keywords = updates.keywords;
246
- if (updates.data)
247
- updateData.data = updates.data;
248
- if (updates.privateTopic !== undefined)
249
- updateData.private = updates.privateTopic;
250
- const { data: updatedContent, error } = yield this.supabase
251
- .from('shared_content')
252
- .update(updateData)
253
- .eq('id', id)
254
- .select();
255
- if (error) {
256
- console.error('error updating shared content:', error);
257
- throw new Error('error updating shared content');
258
- }
259
- if (!updatedContent || updatedContent.length === 0) {
260
- throw new Error('shared content not found');
261
- }
262
- return updatedContent[0];
263
- });
264
- }
265
- /**
266
- * Soft delete shared content by setting the deleted_at timestamp.
267
- * @param id - The ID of the content to delete.
268
- * @returns The deleted shared content record.
269
- * @throws {Error} if deletion fails or content not found.
270
- */
271
- removeSharedContent(id) {
272
- return __awaiter(this, void 0, void 0, function* () {
273
- const { data: deletedContent, error } = yield this.supabase
274
- .from('shared_content')
275
- .update({ deleted_at: new Date().toISOString() })
276
- .eq('id', id)
277
- .select();
278
- if (error) {
279
- console.error('error deleting shared content:', error);
280
- throw new Error('error deleting shared content');
281
- }
282
- if (!deletedContent || deletedContent.length === 0) {
283
- throw new Error('shared content not found or already deleted');
284
- }
285
- return deletedContent[0];
286
- });
287
- }
288
- }