@learnpack/learnpack 5.0.122 → 5.0.126

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,381 +1,381 @@
1
- import axios from "axios"
2
- import { writeFile } from "fs/promises"
3
- import Console from "../utils/console"
4
- import { PackageInfo } from "./creatorUtilities"
5
- import * as fs from "fs"
6
- import * as path from "path"
7
- import { RIGOBOT_HOST } from "./api"
8
-
9
- type TCreateReadmeInputs = {
10
- title: string
11
- output_lang: string
12
- list_of_exercises: string
13
- tutorial_description: string
14
- include_quiz: string
15
- lesson_description: string
16
- }
17
-
18
- export const createReadme = async (
19
- token: string,
20
- inputs: TCreateReadmeInputs
21
- ) => {
22
- try {
23
- const response = await axios.post(
24
- `${RIGOBOT_HOST}/v1/prompting/completion/423/`,
25
- {
26
- inputs,
27
- include_purpose_objective: false,
28
- execute_async: false,
29
- },
30
- {
31
- headers: {
32
- "Content-Type": "application/json",
33
- Authorization: "Token " + token,
34
- },
35
- }
36
- )
37
- return response.data
38
- } catch (error) {
39
- console.error(error)
40
- return null
41
- }
42
- }
43
-
44
- export const hasCreatorPermission = async (token: string) => {
45
- Console.debug("Checking if user has creator permission")
46
- try {
47
- const response = await axios.get(
48
- `${RIGOBOT_HOST}/v1/learnpack/permissions/creator`,
49
- {
50
- headers: {
51
- "Content-Type": "application/json",
52
- Authorization: "Token " + token,
53
- },
54
- }
55
- )
56
-
57
- // If the status code is 403, it means no permission
58
- if (response.status === 403)
59
- return false
60
-
61
- Console.debug("The user is a creator! 🎉")
62
- return true
63
- } catch (error) {
64
- Console.error(error as string)
65
- return false
66
- }
67
- }
68
-
69
- type TGenerateImageParams = {
70
- prompt: string
71
- }
72
-
73
- export const generateImage = async (
74
- token: string,
75
- { prompt }: TGenerateImageParams
76
- ) => {
77
- try {
78
- const response = await axios.post(
79
- `${RIGOBOT_HOST}/v1/learnpack/tools/images`,
80
- {
81
- prompt,
82
- },
83
- {
84
- headers: {
85
- "Content-Type": "application/json",
86
- Authorization: "Token " + token,
87
- },
88
- }
89
- )
90
-
91
- return response.data
92
- } catch (error) {
93
- Console.debug(error)
94
- return null
95
- }
96
- }
97
-
98
- export async function downloadImage(
99
- imageUrl: string,
100
- savePath: string
101
- ): Promise<void> {
102
- const response = await axios.get(imageUrl, { responseType: "arraybuffer" })
103
-
104
- const buffer = Buffer.from(response.data, "binary")
105
-
106
- await writeFile(savePath, buffer)
107
- }
108
-
109
- type TTranslateInputs = {
110
- text_to_translate: string
111
- output_language: string
112
- exercise_slug: string
113
- }
114
- export const translateExercise = async (
115
- token: string,
116
- inputs: TTranslateInputs
117
- ) => {
118
- const response = await axios.post(
119
- `${RIGOBOT_HOST}/v1/prompting/completion/159/`,
120
- {
121
- inputs: inputs,
122
- include_purpose_objective: false,
123
- execute_async: false,
124
- },
125
- {
126
- headers: {
127
- "Content-Type": "application/json",
128
- Authorization: "Token " + token,
129
- },
130
- }
131
- )
132
-
133
- return response.data
134
- }
135
-
136
- type TGenerateCourseIntroductionInputs = {
137
- course_title: string
138
- lessons_context: string
139
- }
140
-
141
- export const generateCourseIntroduction = async (
142
- token: string,
143
- inputs: TGenerateCourseIntroductionInputs
144
- ) => {
145
- const response = await axios.post(
146
- `${RIGOBOT_HOST}/v1/prompting/completion/192/`,
147
- {
148
- inputs: inputs,
149
- include_purpose_objective: false,
150
- execute_async: false,
151
- },
152
- {
153
- headers: {
154
- "Content-Type": "application/json",
155
- Authorization: "Token " + token,
156
- },
157
- }
158
- )
159
-
160
- return response.data
161
- }
162
-
163
- type TInteractiveCreationInputs = {
164
- courseInfo: string
165
- prevInteractions: string
166
- }
167
- export const interactiveCreation = async (
168
- token: string,
169
- inputs: TInteractiveCreationInputs
170
- ) => {
171
- const response = await axios.post(
172
- `${RIGOBOT_HOST}/v1/prompting/completion/390/`,
173
- {
174
- inputs: inputs,
175
- include_purpose_objective: false,
176
- execute_async: false,
177
- },
178
- {
179
- headers: {
180
- "Content-Type": "application/json",
181
- Authorization: "Token " + token,
182
- },
183
- }
184
- )
185
-
186
- return response.data
187
- }
188
-
189
- type TCreateCodeFileInputs = {
190
- readme: string
191
- tutorial_info: string
192
- }
193
-
194
- export const createCodeFile = async (
195
- token: string,
196
- inputs: TCreateCodeFileInputs
197
- ) => {
198
- const response = await axios.post(
199
- `${RIGOBOT_HOST}/v1/prompting/completion/456/`,
200
- {
201
- inputs: inputs,
202
- include_purpose_objective: false,
203
- execute_async: false,
204
- },
205
- {
206
- headers: {
207
- "Content-Type": "application/json",
208
- Authorization: "Token " + token,
209
- },
210
- }
211
- )
212
-
213
- return response.data
214
- }
215
-
216
- type TCreateCodingReadmeInputs = {
217
- tutorial_description: string
218
- list_of_exercises: string
219
- output_lang: string
220
- title: string
221
- lesson_description: string
222
- }
223
- export const createCodingReadme = async (
224
- token: string,
225
- inputs: TCreateCodingReadmeInputs
226
- ) => {
227
- const response = await axios.post(
228
- `${RIGOBOT_HOST}/v1/prompting/completion/489/`,
229
- { inputs, include_purpose_objective: false, execute_async: false },
230
- {
231
- headers: {
232
- "Content-Type": "application/json",
233
- Authorization: "Token " + token,
234
- },
235
- }
236
- )
237
-
238
- return response.data
239
- }
240
-
241
- type TReadmeCreatorInputs = {
242
- tutorial_description: string
243
- list_of_exercises: string
244
- output_lang: string
245
- title: string
246
- lesson_description: string
247
- kind: string
248
- }
249
-
250
- export const readmeCreator = async (
251
- token: string,
252
- inputs: TReadmeCreatorInputs
253
- ) => {
254
- if (inputs.kind === "quiz" || inputs.kind === "read") {
255
- const createReadmeInputs: TCreateReadmeInputs = {
256
- title: inputs.title,
257
- output_lang: inputs.output_lang,
258
- list_of_exercises: inputs.list_of_exercises,
259
- tutorial_description: inputs.tutorial_description,
260
- include_quiz: inputs.kind === "quiz" ? "true" : "false",
261
- lesson_description: inputs.lesson_description,
262
- }
263
- return createReadme(token, createReadmeInputs)
264
- }
265
-
266
- if (inputs.kind === "code") {
267
- return createCodingReadme(token, {
268
- title: inputs.title,
269
- output_lang: inputs.output_lang,
270
- list_of_exercises: inputs.list_of_exercises,
271
- tutorial_description: inputs.tutorial_description,
272
- lesson_description: inputs.lesson_description,
273
- })
274
- }
275
-
276
- throw new Error("Invalid kind of lesson")
277
- }
278
-
279
- export async function createPreviewReadme(
280
- tutorialDir: string,
281
- packageInfo: PackageInfo,
282
- rigoToken: string,
283
- readmeContents: string[]
284
- ) {
285
- const readmeFilename = `README.md`
286
-
287
- const readmeContent = await generateCourseIntroduction(rigoToken, {
288
- course_title: packageInfo.title.us,
289
- lessons_context: readmeContents.join("\n"),
290
- })
291
- fs.writeFileSync(path.join(tutorialDir, readmeFilename), readmeContent.answer)
292
- }
293
-
294
- // {"lesson": "The text of the lesson", "number_of_words": "Words lenght of the lesson", "expected_number_words": "The expected number of words"}
295
-
296
- type TReduceReadmeInputs = {
297
- lesson: string
298
- number_of_words: string
299
- expected_number_words: string
300
- fkgl_results: string
301
- expected_grade_level: string
302
- }
303
- export async function makeReadmeReadable(
304
- rigoToken: string,
305
- inputs: TReduceReadmeInputs
306
- ) {
307
- try {
308
- const response = await axios.post(
309
- `${RIGOBOT_HOST}/v1/prompting/completion/588/`,
310
- { inputs, include_purpose_objective: false, execute_async: false },
311
- {
312
- headers: {
313
- "Content-Type": "application/json",
314
- Authorization: "Token " + rigoToken,
315
- },
316
- }
317
- )
318
-
319
- return response.data
320
- } catch (error) {
321
- Console.debug(error)
322
- return null
323
- }
324
- }
325
-
326
- export const isValidRigoToken = async (rigobotToken: string) => {
327
- const rigoUrl = `${RIGOBOT_HOST}/v1/auth/token/${rigobotToken}`
328
- const rigoResp = await fetch(rigoUrl)
329
- if (!rigoResp.ok) {
330
- Console.debug("Invalid Rigobot token", rigoResp.status)
331
- Console.debug(rigoResp.statusText)
332
- return false
333
- }
334
-
335
- return true
336
- }
337
-
338
- type TGenerateCourseShortNameInputs = {
339
- learnJSON: string
340
- }
341
-
342
- export const generateCourseShortName = async (
343
- token: string,
344
- inputs: TGenerateCourseShortNameInputs
345
- ) => {
346
- const response = await axios.post(
347
- `${RIGOBOT_HOST}/v1/prompting/completion/852/`,
348
- { inputs, include_purpose_objective: false, execute_async: false },
349
- {
350
- headers: {
351
- "Content-Type": "application/json",
352
- Authorization: "Token " + token,
353
- },
354
- }
355
- )
356
-
357
- return response.data
358
- }
359
-
360
- type TFillSidebarJSONInputs = {
361
- needed_translations: string
362
- sidebar_json: string
363
- }
364
-
365
- export const fillSidebarJSON = async (
366
- token: string,
367
- inputs: TFillSidebarJSONInputs
368
- ) => {
369
- const response = await axios.post(
370
- `${RIGOBOT_HOST}/v1/prompting/completion/951/`,
371
- { inputs, include_purpose_objective: false, execute_async: false },
372
- {
373
- headers: {
374
- "Content-Type": "application/json",
375
- Authorization: "Token " + token,
376
- },
377
- }
378
- )
379
-
380
- return response.data
381
- }
1
+ import axios from "axios"
2
+ import { writeFile } from "fs/promises"
3
+ import Console from "../utils/console"
4
+ import { PackageInfo } from "./creatorUtilities"
5
+ import * as fs from "fs"
6
+ import * as path from "path"
7
+ import { RIGOBOT_HOST } from "./api"
8
+
9
+ type TCreateReadmeInputs = {
10
+ title: string
11
+ output_lang: string
12
+ list_of_exercises: string
13
+ tutorial_description: string
14
+ include_quiz: string
15
+ lesson_description: string
16
+ }
17
+
18
+ export const createReadme = async (
19
+ token: string,
20
+ inputs: TCreateReadmeInputs
21
+ ) => {
22
+ try {
23
+ const response = await axios.post(
24
+ `${RIGOBOT_HOST}/v1/prompting/completion/423/`,
25
+ {
26
+ inputs,
27
+ include_purpose_objective: false,
28
+ execute_async: false,
29
+ },
30
+ {
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ Authorization: "Token " + token,
34
+ },
35
+ }
36
+ )
37
+ return response.data
38
+ } catch (error) {
39
+ console.error(error)
40
+ return null
41
+ }
42
+ }
43
+
44
+ export const hasCreatorPermission = async (token: string) => {
45
+ Console.debug("Checking if user has creator permission")
46
+ try {
47
+ const response = await axios.get(
48
+ `${RIGOBOT_HOST}/v1/learnpack/permissions/creator`,
49
+ {
50
+ headers: {
51
+ "Content-Type": "application/json",
52
+ Authorization: "Token " + token,
53
+ },
54
+ }
55
+ )
56
+
57
+ // If the status code is 403, it means no permission
58
+ if (response.status === 403)
59
+ return false
60
+
61
+ Console.debug("The user is a creator! 🎉")
62
+ return true
63
+ } catch (error) {
64
+ Console.error(error as string)
65
+ return false
66
+ }
67
+ }
68
+
69
+ type TGenerateImageParams = {
70
+ prompt: string
71
+ }
72
+
73
+ export const generateImage = async (
74
+ token: string,
75
+ { prompt }: TGenerateImageParams
76
+ ) => {
77
+ try {
78
+ const response = await axios.post(
79
+ `${RIGOBOT_HOST}/v1/learnpack/tools/images`,
80
+ {
81
+ prompt,
82
+ },
83
+ {
84
+ headers: {
85
+ "Content-Type": "application/json",
86
+ Authorization: "Token " + token,
87
+ },
88
+ }
89
+ )
90
+
91
+ return response.data
92
+ } catch (error) {
93
+ Console.debug(error)
94
+ return null
95
+ }
96
+ }
97
+
98
+ export async function downloadImage(
99
+ imageUrl: string,
100
+ savePath: string
101
+ ): Promise<void> {
102
+ const response = await axios.get(imageUrl, { responseType: "arraybuffer" })
103
+
104
+ const buffer = Buffer.from(response.data, "binary")
105
+
106
+ await writeFile(savePath, buffer)
107
+ }
108
+
109
+ type TTranslateInputs = {
110
+ text_to_translate: string
111
+ output_language: string
112
+ exercise_slug: string
113
+ }
114
+ export const translateExercise = async (
115
+ token: string,
116
+ inputs: TTranslateInputs
117
+ ) => {
118
+ const response = await axios.post(
119
+ `${RIGOBOT_HOST}/v1/prompting/completion/159/`,
120
+ {
121
+ inputs: inputs,
122
+ include_purpose_objective: false,
123
+ execute_async: false,
124
+ },
125
+ {
126
+ headers: {
127
+ "Content-Type": "application/json",
128
+ Authorization: "Token " + token,
129
+ },
130
+ }
131
+ )
132
+
133
+ return response.data
134
+ }
135
+
136
+ type TGenerateCourseIntroductionInputs = {
137
+ course_title: string
138
+ lessons_context: string
139
+ }
140
+
141
+ export const generateCourseIntroduction = async (
142
+ token: string,
143
+ inputs: TGenerateCourseIntroductionInputs
144
+ ) => {
145
+ const response = await axios.post(
146
+ `${RIGOBOT_HOST}/v1/prompting/completion/192/`,
147
+ {
148
+ inputs: inputs,
149
+ include_purpose_objective: false,
150
+ execute_async: false,
151
+ },
152
+ {
153
+ headers: {
154
+ "Content-Type": "application/json",
155
+ Authorization: "Token " + token,
156
+ },
157
+ }
158
+ )
159
+
160
+ return response.data
161
+ }
162
+
163
+ type TInteractiveCreationInputs = {
164
+ courseInfo: string
165
+ prevInteractions: string
166
+ }
167
+ export const interactiveCreation = async (
168
+ token: string,
169
+ inputs: TInteractiveCreationInputs
170
+ ) => {
171
+ const response = await axios.post(
172
+ `${RIGOBOT_HOST}/v1/prompting/completion/390/`,
173
+ {
174
+ inputs: inputs,
175
+ include_purpose_objective: false,
176
+ execute_async: false,
177
+ },
178
+ {
179
+ headers: {
180
+ "Content-Type": "application/json",
181
+ Authorization: "Token " + token,
182
+ },
183
+ }
184
+ )
185
+
186
+ return response.data
187
+ }
188
+
189
+ type TCreateCodeFileInputs = {
190
+ readme: string
191
+ tutorial_info: string
192
+ }
193
+
194
+ export const createCodeFile = async (
195
+ token: string,
196
+ inputs: TCreateCodeFileInputs
197
+ ) => {
198
+ const response = await axios.post(
199
+ `${RIGOBOT_HOST}/v1/prompting/completion/456/`,
200
+ {
201
+ inputs: inputs,
202
+ include_purpose_objective: false,
203
+ execute_async: false,
204
+ },
205
+ {
206
+ headers: {
207
+ "Content-Type": "application/json",
208
+ Authorization: "Token " + token,
209
+ },
210
+ }
211
+ )
212
+
213
+ return response.data
214
+ }
215
+
216
+ type TCreateCodingReadmeInputs = {
217
+ tutorial_description: string
218
+ list_of_exercises: string
219
+ output_lang: string
220
+ title: string
221
+ lesson_description: string
222
+ }
223
+ export const createCodingReadme = async (
224
+ token: string,
225
+ inputs: TCreateCodingReadmeInputs
226
+ ) => {
227
+ const response = await axios.post(
228
+ `${RIGOBOT_HOST}/v1/prompting/completion/489/`,
229
+ { inputs, include_purpose_objective: false, execute_async: false },
230
+ {
231
+ headers: {
232
+ "Content-Type": "application/json",
233
+ Authorization: "Token " + token,
234
+ },
235
+ }
236
+ )
237
+
238
+ return response.data
239
+ }
240
+
241
+ type TReadmeCreatorInputs = {
242
+ tutorial_description: string
243
+ list_of_exercises: string
244
+ output_lang: string
245
+ title: string
246
+ lesson_description: string
247
+ kind: string
248
+ }
249
+
250
+ export const readmeCreator = async (
251
+ token: string,
252
+ inputs: TReadmeCreatorInputs
253
+ ) => {
254
+ if (inputs.kind === "quiz" || inputs.kind === "read") {
255
+ const createReadmeInputs: TCreateReadmeInputs = {
256
+ title: inputs.title,
257
+ output_lang: inputs.output_lang,
258
+ list_of_exercises: inputs.list_of_exercises,
259
+ tutorial_description: inputs.tutorial_description,
260
+ include_quiz: inputs.kind === "quiz" ? "true" : "false",
261
+ lesson_description: inputs.lesson_description,
262
+ }
263
+ return createReadme(token, createReadmeInputs)
264
+ }
265
+
266
+ if (inputs.kind === "code") {
267
+ return createCodingReadme(token, {
268
+ title: inputs.title,
269
+ output_lang: inputs.output_lang,
270
+ list_of_exercises: inputs.list_of_exercises,
271
+ tutorial_description: inputs.tutorial_description,
272
+ lesson_description: inputs.lesson_description,
273
+ })
274
+ }
275
+
276
+ throw new Error("Invalid kind of lesson")
277
+ }
278
+
279
+ export async function createPreviewReadme(
280
+ tutorialDir: string,
281
+ packageInfo: PackageInfo,
282
+ rigoToken: string,
283
+ readmeContents: string[]
284
+ ) {
285
+ const readmeFilename = `README.md`
286
+
287
+ const readmeContent = await generateCourseIntroduction(rigoToken, {
288
+ course_title: packageInfo.title.us,
289
+ lessons_context: readmeContents.join("\n"),
290
+ })
291
+ fs.writeFileSync(path.join(tutorialDir, readmeFilename), readmeContent.answer)
292
+ }
293
+
294
+ // {"lesson": "The text of the lesson", "number_of_words": "Words lenght of the lesson", "expected_number_words": "The expected number of words"}
295
+
296
+ type TReduceReadmeInputs = {
297
+ lesson: string
298
+ number_of_words: string
299
+ expected_number_words: string
300
+ fkgl_results: string
301
+ expected_grade_level: string
302
+ }
303
+ export async function makeReadmeReadable(
304
+ rigoToken: string,
305
+ inputs: TReduceReadmeInputs
306
+ ) {
307
+ try {
308
+ const response = await axios.post(
309
+ `${RIGOBOT_HOST}/v1/prompting/completion/588/`,
310
+ { inputs, include_purpose_objective: false, execute_async: false },
311
+ {
312
+ headers: {
313
+ "Content-Type": "application/json",
314
+ Authorization: "Token " + rigoToken,
315
+ },
316
+ }
317
+ )
318
+
319
+ return response.data
320
+ } catch (error) {
321
+ Console.debug(error)
322
+ return null
323
+ }
324
+ }
325
+
326
+ export const isValidRigoToken = async (rigobotToken: string) => {
327
+ const rigoUrl = `${RIGOBOT_HOST}/v1/auth/token/${rigobotToken}`
328
+ const rigoResp = await fetch(rigoUrl)
329
+ if (!rigoResp.ok) {
330
+ Console.debug("Invalid Rigobot token", rigoResp.status)
331
+ Console.debug(rigoResp.statusText)
332
+ return false
333
+ }
334
+
335
+ return true
336
+ }
337
+
338
+ type TGenerateCourseShortNameInputs = {
339
+ learnJSON: string
340
+ }
341
+
342
+ export const generateCourseShortName = async (
343
+ token: string,
344
+ inputs: TGenerateCourseShortNameInputs
345
+ ) => {
346
+ const response = await axios.post(
347
+ `${RIGOBOT_HOST}/v1/prompting/completion/852/`,
348
+ { inputs, include_purpose_objective: false, execute_async: false },
349
+ {
350
+ headers: {
351
+ "Content-Type": "application/json",
352
+ Authorization: "Token " + token,
353
+ },
354
+ }
355
+ )
356
+
357
+ return response.data
358
+ }
359
+
360
+ type TFillSidebarJSONInputs = {
361
+ needed_translations: string
362
+ sidebar_json: string
363
+ }
364
+
365
+ export const fillSidebarJSON = async (
366
+ token: string,
367
+ inputs: TFillSidebarJSONInputs
368
+ ) => {
369
+ const response = await axios.post(
370
+ `${RIGOBOT_HOST}/v1/prompting/completion/951/`,
371
+ { inputs, include_purpose_objective: false, execute_async: false },
372
+ {
373
+ headers: {
374
+ "Content-Type": "application/json",
375
+ Authorization: "Token " + token,
376
+ },
377
+ }
378
+ )
379
+
380
+ return response.data
381
+ }