@promptbook/utils 0.41.2 → 0.41.4
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/bin/promptbook-cli.js +6 -0
- package/esm/index.es.js +1161 -193
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/utils.index.d.ts +12 -4
- package/esm/typings/conversion/prettify/PrettifyOptions.d.ts +7 -0
- package/esm/typings/conversion/prettify/prettifyPromptbookString.d.ts +10 -0
- package/esm/typings/conversion/prettify/prettifyPromptbookStringCli.d.ts +9 -0
- package/package.json +2 -1
- package/umd/index.umd.js +1164 -195
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/utils.index.d.ts +12 -4
- package/umd/typings/conversion/prettify/PrettifyOptions.d.ts +7 -0
- package/umd/typings/conversion/prettify/prettifyPromptbookString.d.ts +10 -0
- package/umd/typings/conversion/prettify/prettifyPromptbookStringCli.d.ts +9 -0
package/esm/index.es.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { capitalize, normalizeTo_SCREAMING_CASE, normalizeTo_PascalCase, normalizeToKebabCase, removeDiacritics } from 'n12';
|
|
1
2
|
import spaceTrim from 'spacetrim';
|
|
2
|
-
import moment from 'moment';
|
|
3
|
-
import { normalizeToKebabCase, removeDiacritics, capitalize } from 'n12';
|
|
4
3
|
import { format } from 'prettier';
|
|
5
4
|
import parserHtml from 'prettier/parser-html';
|
|
5
|
+
import colors from 'colors';
|
|
6
|
+
import commander from 'commander';
|
|
7
|
+
import { writeFile, readFile } from 'fs/promises';
|
|
8
|
+
import glob from 'glob-promise';
|
|
9
|
+
import moment from 'moment';
|
|
6
10
|
|
|
7
11
|
/*! *****************************************************************************
|
|
8
12
|
Copyright (c) Microsoft Corporation.
|
|
@@ -46,6 +50,44 @@ var __assign = function() {
|
|
|
46
50
|
return __assign.apply(this, arguments);
|
|
47
51
|
};
|
|
48
52
|
|
|
53
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
54
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
55
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
56
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
57
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
58
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
59
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function __generator(thisArg, body) {
|
|
64
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
65
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
66
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
67
|
+
function step(op) {
|
|
68
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
69
|
+
while (_) try {
|
|
70
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
71
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
72
|
+
switch (op[0]) {
|
|
73
|
+
case 0: case 1: t = op; break;
|
|
74
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
75
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
76
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
77
|
+
default:
|
|
78
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
79
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
80
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
81
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
82
|
+
if (t[2]) _.ops.pop();
|
|
83
|
+
_.trys.pop(); continue;
|
|
84
|
+
}
|
|
85
|
+
op = body.call(thisArg, _);
|
|
86
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
87
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
49
91
|
function __values(o) {
|
|
50
92
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
51
93
|
if (m) return m.call(o);
|
|
@@ -99,6 +141,382 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
|
99
141
|
return PromptbookSyntaxError;
|
|
100
142
|
}(Error));
|
|
101
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Supported script languages
|
|
146
|
+
*/
|
|
147
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Parses the template and returns the list of all parameter names
|
|
151
|
+
*
|
|
152
|
+
* @param template the template with parameters in {curly} braces
|
|
153
|
+
* @returns the list of parameter names
|
|
154
|
+
*
|
|
155
|
+
* @private within the library
|
|
156
|
+
*/
|
|
157
|
+
function extractParameters(template) {
|
|
158
|
+
var e_1, _a;
|
|
159
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
160
|
+
var parameterNames = [];
|
|
161
|
+
try {
|
|
162
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
163
|
+
var match = matches_1_1.value;
|
|
164
|
+
var parameterName = match[0].slice(1, -1);
|
|
165
|
+
if (!parameterNames.includes(parameterName)) {
|
|
166
|
+
parameterNames.push(parameterName);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
171
|
+
finally {
|
|
172
|
+
try {
|
|
173
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
174
|
+
}
|
|
175
|
+
finally { if (e_1) throw e_1.error; }
|
|
176
|
+
}
|
|
177
|
+
return parameterNames;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Computes the deepness of the markdown structure.
|
|
182
|
+
*
|
|
183
|
+
* @private within the library
|
|
184
|
+
*/
|
|
185
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
186
|
+
var e_1, _a;
|
|
187
|
+
var maxDeepness = 0;
|
|
188
|
+
try {
|
|
189
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
190
|
+
var section = _c.value;
|
|
191
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
195
|
+
finally {
|
|
196
|
+
try {
|
|
197
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
198
|
+
}
|
|
199
|
+
finally { if (e_1) throw e_1.error; }
|
|
200
|
+
}
|
|
201
|
+
return maxDeepness + 1;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The maximum number of iterations for a loops
|
|
206
|
+
*/
|
|
207
|
+
var LOOP_LIMIT = 1000;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
211
|
+
*/
|
|
212
|
+
var UnexpectedError = /** @class */ (function (_super) {
|
|
213
|
+
__extends(UnexpectedError, _super);
|
|
214
|
+
function UnexpectedError(message) {
|
|
215
|
+
var _this = _super.call(this, spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the promptbook library\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
|
|
216
|
+
_this.name = 'UnexpectedError';
|
|
217
|
+
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
218
|
+
return _this;
|
|
219
|
+
}
|
|
220
|
+
return UnexpectedError;
|
|
221
|
+
}(Error));
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
225
|
+
*
|
|
226
|
+
* Note: This function does work with code blocks
|
|
227
|
+
* Note: This function does not work with markdown comments
|
|
228
|
+
*
|
|
229
|
+
* @param markdown The markdown string to parse.
|
|
230
|
+
* @returns The MarkdownStructure object.
|
|
231
|
+
*
|
|
232
|
+
* @private within the library
|
|
233
|
+
*/
|
|
234
|
+
function markdownToMarkdownStructure(markdown) {
|
|
235
|
+
var e_1, _a;
|
|
236
|
+
var lines = markdown.split('\n');
|
|
237
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
238
|
+
var current = root;
|
|
239
|
+
var isInsideCodeBlock = false;
|
|
240
|
+
try {
|
|
241
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
242
|
+
var line = lines_1_1.value;
|
|
243
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
244
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
245
|
+
if (line.startsWith('```')) {
|
|
246
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
247
|
+
}
|
|
248
|
+
current.contentLines.push(line);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
var level = headingMatch.groups.mark.length;
|
|
252
|
+
var title = headingMatch.groups.title.trim();
|
|
253
|
+
var parent_1 = void 0;
|
|
254
|
+
if (level > current.level) {
|
|
255
|
+
// Note: Going deeper (next section is child of current)
|
|
256
|
+
parent_1 = current;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
260
|
+
parent_1 = current;
|
|
261
|
+
var loopLimit = LOOP_LIMIT;
|
|
262
|
+
while (parent_1.level !== level - 1) {
|
|
263
|
+
if (loopLimit-- < 0) {
|
|
264
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
265
|
+
}
|
|
266
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
267
|
+
// [🌻]
|
|
268
|
+
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
269
|
+
}
|
|
270
|
+
parent_1 = parent_1.parent;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
274
|
+
parent_1.sections.push(section);
|
|
275
|
+
current = section;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
280
|
+
finally {
|
|
281
|
+
try {
|
|
282
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
283
|
+
}
|
|
284
|
+
finally { if (e_1) throw e_1.error; }
|
|
285
|
+
}
|
|
286
|
+
if (root.sections.length === 1) {
|
|
287
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
288
|
+
return markdownStructure;
|
|
289
|
+
}
|
|
290
|
+
// [🌻]
|
|
291
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
292
|
+
// return root;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* @private
|
|
296
|
+
*/
|
|
297
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
298
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
299
|
+
return {
|
|
300
|
+
level: level,
|
|
301
|
+
title: title,
|
|
302
|
+
content: spaceTrim(contentLines.join('\n')),
|
|
303
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Utility function to extract all list items from markdown
|
|
309
|
+
*
|
|
310
|
+
* Note: It works with both ul and ol
|
|
311
|
+
* Note: It omits list items in code blocks
|
|
312
|
+
* Note: It flattens nested lists
|
|
313
|
+
* Note: It can not work with html syntax and comments
|
|
314
|
+
*
|
|
315
|
+
* @param markdown any valid markdown
|
|
316
|
+
* @returns
|
|
317
|
+
*/
|
|
318
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
319
|
+
var e_1, _a;
|
|
320
|
+
var lines = markdown.split('\n');
|
|
321
|
+
var listItems = [];
|
|
322
|
+
var isInCodeBlock = false;
|
|
323
|
+
try {
|
|
324
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
325
|
+
var line = lines_1_1.value;
|
|
326
|
+
var trimmedLine = line.trim();
|
|
327
|
+
if (trimmedLine.startsWith('```')) {
|
|
328
|
+
isInCodeBlock = !isInCodeBlock;
|
|
329
|
+
}
|
|
330
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
331
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
332
|
+
listItems.push(listItem);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
337
|
+
finally {
|
|
338
|
+
try {
|
|
339
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
340
|
+
}
|
|
341
|
+
finally { if (e_1) throw e_1.error; }
|
|
342
|
+
}
|
|
343
|
+
return listItems;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Extracts all code blocks from markdown.
|
|
348
|
+
*
|
|
349
|
+
* Note: There are 3 simmilar function:
|
|
350
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
351
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
352
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
353
|
+
*
|
|
354
|
+
* @param markdown any valid markdown
|
|
355
|
+
* @returns code blocks with language and content
|
|
356
|
+
*
|
|
357
|
+
*/
|
|
358
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
359
|
+
var e_1, _a;
|
|
360
|
+
var codeBlocks = [];
|
|
361
|
+
var lines = markdown.split('\n');
|
|
362
|
+
var currentCodeBlock = null;
|
|
363
|
+
try {
|
|
364
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
365
|
+
var line = lines_1_1.value;
|
|
366
|
+
if (line.startsWith('```')) {
|
|
367
|
+
var language = line.slice(3).trim() || null;
|
|
368
|
+
if (currentCodeBlock === null) {
|
|
369
|
+
currentCodeBlock = { language: language, content: '' };
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
if (language !== null) {
|
|
373
|
+
// [🌻]
|
|
374
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
375
|
+
}
|
|
376
|
+
codeBlocks.push(currentCodeBlock);
|
|
377
|
+
currentCodeBlock = null;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else if (currentCodeBlock !== null) {
|
|
381
|
+
if (currentCodeBlock.content !== '') {
|
|
382
|
+
currentCodeBlock.content += '\n';
|
|
383
|
+
}
|
|
384
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
389
|
+
finally {
|
|
390
|
+
try {
|
|
391
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
392
|
+
}
|
|
393
|
+
finally { if (e_1) throw e_1.error; }
|
|
394
|
+
}
|
|
395
|
+
if (currentCodeBlock !== null) {
|
|
396
|
+
// [🌻]
|
|
397
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
398
|
+
}
|
|
399
|
+
return codeBlocks;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Extracts exactly ONE code block from markdown.
|
|
404
|
+
*
|
|
405
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
406
|
+
*
|
|
407
|
+
* Note: There are 3 simmilar function:
|
|
408
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
409
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
410
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
411
|
+
*
|
|
412
|
+
* @param markdown any valid markdown
|
|
413
|
+
* @returns code block with language and content
|
|
414
|
+
*/
|
|
415
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
416
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
417
|
+
if (codeBlocks.length !== 1) {
|
|
418
|
+
// TODO: Report more specific place where the error happened
|
|
419
|
+
throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
|
|
420
|
+
}
|
|
421
|
+
return codeBlocks[0];
|
|
422
|
+
}
|
|
423
|
+
/***
|
|
424
|
+
* TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
425
|
+
*/
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Removes HTML or Markdown comments from a string.
|
|
429
|
+
*
|
|
430
|
+
* @param {string} content - The string to remove comments from.
|
|
431
|
+
* @returns {string} The input string with all comments removed.
|
|
432
|
+
*/
|
|
433
|
+
function removeContentComments(content) {
|
|
434
|
+
return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* The version of the Promptbook library
|
|
439
|
+
*/
|
|
440
|
+
var PROMPTBOOK_VERSION = '0.41.0';
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
444
|
+
*
|
|
445
|
+
* @param script from which to extract the variables
|
|
446
|
+
* @returns the list of variable names
|
|
447
|
+
*
|
|
448
|
+
* @private within the promptbookStringToJson
|
|
449
|
+
*/
|
|
450
|
+
function extractVariables(script) {
|
|
451
|
+
var variables = [];
|
|
452
|
+
script = "(()=>{".concat(script, "})()");
|
|
453
|
+
try {
|
|
454
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
455
|
+
try {
|
|
456
|
+
eval(script);
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
if (!(error instanceof ReferenceError)) {
|
|
460
|
+
throw error;
|
|
461
|
+
}
|
|
462
|
+
var undefinedName = error.message.split(' ')[0];
|
|
463
|
+
/*
|
|
464
|
+
Note: Remapping error
|
|
465
|
+
From: [ReferenceError: thing is not defined],
|
|
466
|
+
To: [Error: Parameter {thing} is not defined],
|
|
467
|
+
*/
|
|
468
|
+
if (!undefinedName) {
|
|
469
|
+
throw error;
|
|
470
|
+
}
|
|
471
|
+
if (script.includes(undefinedName + '(')) {
|
|
472
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
variables.push(undefinedName);
|
|
476
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
if (!(error instanceof Error)) {
|
|
482
|
+
throw error;
|
|
483
|
+
}
|
|
484
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
485
|
+
}
|
|
486
|
+
return variables;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Execution type describes the way how the block is executed
|
|
491
|
+
*
|
|
492
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
493
|
+
*/
|
|
494
|
+
var ExecutionTypes = ['PROMPT_TEMPLATE', 'SIMPLE_TEMPLATE', 'SCRIPT', 'PROMPT_DIALOG'];
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Units of text measurement
|
|
498
|
+
*/
|
|
499
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
|
|
500
|
+
/**
|
|
501
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
502
|
+
*/
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Removes Markdown formatting tags from a string.
|
|
506
|
+
*
|
|
507
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
508
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
509
|
+
*/
|
|
510
|
+
function removeMarkdownFormatting(str) {
|
|
511
|
+
// Remove bold formatting
|
|
512
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
513
|
+
// Remove italic formatting
|
|
514
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
515
|
+
// Remove code formatting
|
|
516
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
517
|
+
return str;
|
|
518
|
+
}
|
|
519
|
+
|
|
102
520
|
/**
|
|
103
521
|
* Function parseNumber will parse number from string
|
|
104
522
|
*
|
|
@@ -143,24 +561,741 @@ function parseNumber(value) {
|
|
|
143
561
|
}
|
|
144
562
|
return numerator / denominator;
|
|
145
563
|
}
|
|
146
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
147
|
-
return 0;
|
|
564
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
565
|
+
return 0;
|
|
566
|
+
}
|
|
567
|
+
if (value.includes('E')) {
|
|
568
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
569
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
570
|
+
}
|
|
571
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
572
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
573
|
+
}
|
|
574
|
+
var num = parseFloat(value);
|
|
575
|
+
if (isNaN(num)) {
|
|
576
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
577
|
+
}
|
|
578
|
+
return num;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
582
|
+
*/
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Parses one line of ul/ol to command
|
|
586
|
+
*
|
|
587
|
+
* @private within the promptbookStringToJson
|
|
588
|
+
*/
|
|
589
|
+
function parseCommand(listItem) {
|
|
590
|
+
var e_1, _a;
|
|
591
|
+
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
592
|
+
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
593
|
+
}
|
|
594
|
+
var type = listItem.trim();
|
|
595
|
+
type = type.split('`').join('');
|
|
596
|
+
type = type.split('"').join('');
|
|
597
|
+
type = type.split("'").join('');
|
|
598
|
+
type = type.split('~').join('');
|
|
599
|
+
type = type.split('[').join('');
|
|
600
|
+
type = type.split(']').join('');
|
|
601
|
+
type = type.split('(').join('');
|
|
602
|
+
type = type.split(')').join('');
|
|
603
|
+
type = normalizeTo_SCREAMING_CASE(type);
|
|
604
|
+
type = type.split('DIALOGUE').join('DIALOG');
|
|
605
|
+
var listItemParts = listItem
|
|
606
|
+
.split(' ')
|
|
607
|
+
.map(function (part) { return part.trim(); })
|
|
608
|
+
.filter(function (item) { return item !== ''; })
|
|
609
|
+
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
610
|
+
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
611
|
+
.map(removeMarkdownFormatting);
|
|
612
|
+
if (type.startsWith('URL') ||
|
|
613
|
+
type.startsWith('PTBK_URL') ||
|
|
614
|
+
type.startsWith('PTBKURL') ||
|
|
615
|
+
type.startsWith('PROMPTBOOK_URL') ||
|
|
616
|
+
type.startsWith('PROMPTBOOKURL') ||
|
|
617
|
+
type.startsWith('HTTPS')) {
|
|
618
|
+
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
619
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
620
|
+
}
|
|
621
|
+
var promptbookUrlString = listItemParts.pop();
|
|
622
|
+
var promptbookUrl = new URL(promptbookUrlString);
|
|
623
|
+
if (promptbookUrl.protocol !== 'https:') {
|
|
624
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
625
|
+
}
|
|
626
|
+
if (promptbookUrl.hash !== '') {
|
|
627
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
type: 'PROMPTBOOK_URL',
|
|
631
|
+
promptbookUrl: promptbookUrl,
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
635
|
+
if (listItemParts.length !== 2) {
|
|
636
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
637
|
+
}
|
|
638
|
+
var promptbookVersion = listItemParts.pop();
|
|
639
|
+
// TODO: Validate version
|
|
640
|
+
return {
|
|
641
|
+
type: 'PROMPTBOOK_VERSION',
|
|
642
|
+
promptbookVersion: promptbookVersion,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
else if (type.startsWith('EXECUTE') ||
|
|
646
|
+
type.startsWith('EXEC') ||
|
|
647
|
+
type.startsWith('PROMPT_DIALOG') ||
|
|
648
|
+
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
649
|
+
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
650
|
+
if (executionTypes.length !== 1) {
|
|
651
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
type: 'EXECUTE',
|
|
655
|
+
executionType: executionTypes[0],
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
else if (type.startsWith('MODEL')) {
|
|
659
|
+
// TODO: Make this more elegant and dynamically
|
|
660
|
+
if (type.startsWith('MODEL_VARIANT')) {
|
|
661
|
+
if (type === 'MODEL_VARIANT_CHAT') {
|
|
662
|
+
return {
|
|
663
|
+
type: 'MODEL',
|
|
664
|
+
key: 'modelVariant',
|
|
665
|
+
value: 'CHAT',
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
669
|
+
return {
|
|
670
|
+
type: 'MODEL',
|
|
671
|
+
key: 'modelVariant',
|
|
672
|
+
value: 'COMPLETION',
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
if (type.startsWith('MODEL_NAME')) {
|
|
680
|
+
return {
|
|
681
|
+
type: 'MODEL',
|
|
682
|
+
key: 'modelName',
|
|
683
|
+
value: listItemParts.pop(),
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
else if (type.startsWith('PARAM') ||
|
|
691
|
+
type.startsWith('INPUT_PARAM') ||
|
|
692
|
+
type.startsWith('OUTPUT_PARAM') ||
|
|
693
|
+
listItem.startsWith('{') ||
|
|
694
|
+
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
695
|
+
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
696
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
697
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
698
|
+
}
|
|
699
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
700
|
+
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
701
|
+
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
702
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
703
|
+
}
|
|
704
|
+
var isInput = type.startsWith('INPUT');
|
|
705
|
+
var isOutput = type.startsWith('OUTPUT');
|
|
706
|
+
return {
|
|
707
|
+
type: 'PARAMETER',
|
|
708
|
+
parameterName: parameterName,
|
|
709
|
+
parameterDescription: parameterDescription.trim() || null,
|
|
710
|
+
isInput: isInput,
|
|
711
|
+
isOutput: isOutput,
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
else if (type.startsWith('JOKER')) {
|
|
715
|
+
if (listItemParts.length !== 2) {
|
|
716
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
717
|
+
}
|
|
718
|
+
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
719
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
720
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
721
|
+
}
|
|
722
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
723
|
+
var parameterName = parametersMatch.groups.parameterName;
|
|
724
|
+
return {
|
|
725
|
+
type: 'JOKER',
|
|
726
|
+
parameterName: parameterName,
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
730
|
+
if (listItemParts.length !== 2) {
|
|
731
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
732
|
+
}
|
|
733
|
+
var functionName = listItemParts.pop();
|
|
734
|
+
return {
|
|
735
|
+
type: 'POSTPROCESS',
|
|
736
|
+
functionName: functionName,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
else if (type.startsWith('EXPECT_JSON')) {
|
|
740
|
+
return {
|
|
741
|
+
type: 'EXPECT_FORMAT',
|
|
742
|
+
format: 'JSON',
|
|
743
|
+
};
|
|
744
|
+
// [🥤]
|
|
745
|
+
}
|
|
746
|
+
else if (type.startsWith('EXPECT')) {
|
|
747
|
+
try {
|
|
748
|
+
listItemParts.shift();
|
|
749
|
+
var sign = void 0;
|
|
750
|
+
var signRaw = listItemParts.shift();
|
|
751
|
+
if (/^exact/i.test(signRaw)) {
|
|
752
|
+
sign = 'EXACTLY';
|
|
753
|
+
}
|
|
754
|
+
else if (/^min/i.test(signRaw)) {
|
|
755
|
+
sign = 'MINIMUM';
|
|
756
|
+
}
|
|
757
|
+
else if (/^max/i.test(signRaw)) {
|
|
758
|
+
sign = 'MAXIMUM';
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\""));
|
|
762
|
+
}
|
|
763
|
+
var amountRaw = listItemParts.shift();
|
|
764
|
+
var amount = parseNumber(amountRaw);
|
|
765
|
+
if (amount < 0) {
|
|
766
|
+
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
767
|
+
}
|
|
768
|
+
if (amount !== Math.floor(amount)) {
|
|
769
|
+
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
770
|
+
}
|
|
771
|
+
var unitRaw = listItemParts.shift();
|
|
772
|
+
var unit = undefined;
|
|
773
|
+
try {
|
|
774
|
+
for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
|
|
775
|
+
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
776
|
+
var existingUnitText = existingUnit;
|
|
777
|
+
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
778
|
+
if (existingUnitText === 'CHARACTER') {
|
|
779
|
+
existingUnitText = 'CHAR';
|
|
780
|
+
}
|
|
781
|
+
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
782
|
+
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
783
|
+
if (unit !== undefined) {
|
|
784
|
+
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
785
|
+
}
|
|
786
|
+
unit = existingUnit;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
791
|
+
finally {
|
|
792
|
+
try {
|
|
793
|
+
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
794
|
+
}
|
|
795
|
+
finally { if (e_1) throw e_1.error; }
|
|
796
|
+
}
|
|
797
|
+
if (unit === undefined) {
|
|
798
|
+
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
799
|
+
}
|
|
800
|
+
return {
|
|
801
|
+
type: 'EXPECT_AMOUNT',
|
|
802
|
+
sign: sign,
|
|
803
|
+
unit: unit,
|
|
804
|
+
amount: amount,
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
catch (error) {
|
|
808
|
+
if (!(error instanceof Error)) {
|
|
809
|
+
throw error;
|
|
810
|
+
}
|
|
811
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - Execute\n - Model\n - Parameter\n - INPUT PARAMETER\n - OUTPUT PARAMETER\n - PROMPTBOOK VERSION\n ")));
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Parse promptbook from string format to JSON format
|
|
821
|
+
*
|
|
822
|
+
* Note: This function does not validate logic of the pipeline only the syntax
|
|
823
|
+
*/
|
|
824
|
+
function promptbookStringToJson(promptbookString) {
|
|
825
|
+
var e_1, _a, e_2, _b;
|
|
826
|
+
var promptbookJson = {
|
|
827
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
828
|
+
title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
|
|
829
|
+
promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
|
|
830
|
+
promptbookVersion: PROMPTBOOK_VERSION,
|
|
831
|
+
description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
|
|
832
|
+
parameters: [],
|
|
833
|
+
promptTemplates: [],
|
|
834
|
+
};
|
|
835
|
+
// =============================================================
|
|
836
|
+
// Note: 1️⃣ Normalization of the PROMPTBOOK string
|
|
837
|
+
promptbookString = removeContentComments(promptbookString);
|
|
838
|
+
promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
|
|
839
|
+
promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
|
|
840
|
+
// =============================================================
|
|
841
|
+
///Note: 2️⃣ Function for adding parameters
|
|
842
|
+
var addParam = function (parameterCommand) {
|
|
843
|
+
var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
|
|
844
|
+
var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
845
|
+
if (existingParameter &&
|
|
846
|
+
existingParameter.description &&
|
|
847
|
+
existingParameter.description !== parameterDescription &&
|
|
848
|
+
parameterDescription) {
|
|
849
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
|
|
850
|
+
}
|
|
851
|
+
if (existingParameter) {
|
|
852
|
+
if (parameterDescription) {
|
|
853
|
+
existingParameter.description = parameterDescription;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
else {
|
|
857
|
+
promptbookJson.parameters.push({
|
|
858
|
+
name: parameterName,
|
|
859
|
+
description: parameterDescription || undefined,
|
|
860
|
+
isInput: isInput,
|
|
861
|
+
isOutput: isOutput,
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
// =============================================================
|
|
866
|
+
// Note: 3️⃣ Parse the dynamic part - the template pipeline
|
|
867
|
+
var markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
868
|
+
var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
869
|
+
if (markdownStructureDeepness !== 2) {
|
|
870
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
|
|
871
|
+
}
|
|
872
|
+
promptbookJson.title = markdownStructure.title;
|
|
873
|
+
// TODO: [1] DRY description
|
|
874
|
+
var description = markdownStructure.content;
|
|
875
|
+
// Note: Remove codeblocks
|
|
876
|
+
description = description.split(/^```.*^```/gms).join('');
|
|
877
|
+
//Note: Remove lists and return statement
|
|
878
|
+
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
879
|
+
description = spaceTrim(description);
|
|
880
|
+
if (description === '') {
|
|
881
|
+
description = undefined;
|
|
882
|
+
}
|
|
883
|
+
promptbookJson.description = description;
|
|
884
|
+
var defaultModelRequirements = {};
|
|
885
|
+
var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
|
|
886
|
+
try {
|
|
887
|
+
for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
|
|
888
|
+
var listItem = listItems_1_1.value;
|
|
889
|
+
var command = parseCommand(listItem);
|
|
890
|
+
switch (command.type) {
|
|
891
|
+
case 'PROMPTBOOK_URL':
|
|
892
|
+
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
893
|
+
break;
|
|
894
|
+
case 'PROMPTBOOK_VERSION':
|
|
895
|
+
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
896
|
+
break;
|
|
897
|
+
case 'MODEL':
|
|
898
|
+
defaultModelRequirements[command.key] = command.value;
|
|
899
|
+
break;
|
|
900
|
+
case 'PARAMETER':
|
|
901
|
+
addParam(command);
|
|
902
|
+
break;
|
|
903
|
+
default:
|
|
904
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
909
|
+
finally {
|
|
910
|
+
try {
|
|
911
|
+
if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
|
|
912
|
+
}
|
|
913
|
+
finally { if (e_1) throw e_1.error; }
|
|
914
|
+
}
|
|
915
|
+
var _loop_1 = function (section) {
|
|
916
|
+
var e_3, _e, e_4, _f, e_5, _g;
|
|
917
|
+
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
918
|
+
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
919
|
+
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
920
|
+
var dependentParameterNames = [];
|
|
921
|
+
var executionType = 'PROMPT_TEMPLATE';
|
|
922
|
+
var jokers = [];
|
|
923
|
+
var postprocessing = [];
|
|
924
|
+
var expectAmount = {};
|
|
925
|
+
var expectFormat = undefined;
|
|
926
|
+
var isExecutionTypeChanged = false;
|
|
927
|
+
try {
|
|
928
|
+
for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
|
|
929
|
+
var listItem = listItems_2_1.value;
|
|
930
|
+
var command = parseCommand(listItem);
|
|
931
|
+
switch (command.type) {
|
|
932
|
+
case 'JOKER':
|
|
933
|
+
jokers.push(command.parameterName);
|
|
934
|
+
dependentParameterNames.push(command.parameterName);
|
|
935
|
+
break;
|
|
936
|
+
case 'EXECUTE':
|
|
937
|
+
if (isExecutionTypeChanged) {
|
|
938
|
+
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
939
|
+
}
|
|
940
|
+
executionType = command.executionType;
|
|
941
|
+
isExecutionTypeChanged = true;
|
|
942
|
+
break;
|
|
943
|
+
case 'MODEL':
|
|
944
|
+
templateModelRequirements[command.key] = command.value;
|
|
945
|
+
break;
|
|
946
|
+
case 'PARAMETER':
|
|
947
|
+
// Note: This is just for detecting resulitng parameter name
|
|
948
|
+
addParam(command);
|
|
949
|
+
break;
|
|
950
|
+
case 'POSTPROCESS':
|
|
951
|
+
postprocessing.push(command.functionName);
|
|
952
|
+
break;
|
|
953
|
+
case 'EXPECT_AMOUNT':
|
|
954
|
+
// eslint-disable-next-line no-case-declarations
|
|
955
|
+
var unit = command.unit.toLowerCase();
|
|
956
|
+
expectAmount[unit] = expectAmount[unit] || {};
|
|
957
|
+
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
958
|
+
if (expectAmount[unit].min !== undefined) {
|
|
959
|
+
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
960
|
+
}
|
|
961
|
+
expectAmount[unit].min = command.amount;
|
|
962
|
+
} /* not else */
|
|
963
|
+
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
964
|
+
if (expectAmount[unit].max !== undefined) {
|
|
965
|
+
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
966
|
+
}
|
|
967
|
+
expectAmount[unit].max = command.amount;
|
|
968
|
+
}
|
|
969
|
+
break;
|
|
970
|
+
case 'EXPECT_FORMAT':
|
|
971
|
+
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
972
|
+
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
973
|
+
}
|
|
974
|
+
expectFormat = command.format;
|
|
975
|
+
break;
|
|
976
|
+
default:
|
|
977
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
982
|
+
finally {
|
|
983
|
+
try {
|
|
984
|
+
if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
|
|
985
|
+
}
|
|
986
|
+
finally { if (e_3) throw e_3.error; }
|
|
987
|
+
}
|
|
988
|
+
var _h = extractOneBlockFromMarkdown(section.content), language = _h.language, content = _h.content;
|
|
989
|
+
if (executionType === 'SCRIPT') {
|
|
990
|
+
if (!language) {
|
|
991
|
+
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
992
|
+
}
|
|
993
|
+
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
994
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
var lastLine = section.content.split('\n').pop();
|
|
998
|
+
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
999
|
+
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
1000
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
1001
|
+
// TODO: Show code of invalid sections each time + DRY
|
|
1002
|
+
section.content
|
|
1003
|
+
.split('\n')
|
|
1004
|
+
.map(function (line) { return "> ".concat(line); })
|
|
1005
|
+
.join('\n')), "\n "); }));
|
|
1006
|
+
}
|
|
1007
|
+
var resultingParameterName = match.groups.resultingParamName;
|
|
1008
|
+
// TODO: [1] DRY description
|
|
1009
|
+
var description_1 = section.content;
|
|
1010
|
+
// Note: Remove codeblocks
|
|
1011
|
+
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
1012
|
+
//Note: Remove lists and return statement
|
|
1013
|
+
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1014
|
+
description_1 = spaceTrim(description_1);
|
|
1015
|
+
if (description_1 === '') {
|
|
1016
|
+
description_1 = undefined;
|
|
1017
|
+
}
|
|
1018
|
+
if (Object.keys(jokers).length === 0) {
|
|
1019
|
+
jokers = undefined;
|
|
1020
|
+
}
|
|
1021
|
+
if (Object.keys(expectAmount).length === 0) {
|
|
1022
|
+
expectAmount = undefined;
|
|
1023
|
+
}
|
|
1024
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
1025
|
+
postprocessing = undefined;
|
|
1026
|
+
}
|
|
1027
|
+
try {
|
|
1028
|
+
for (var _j = (e_4 = void 0, __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(section.title)), false), __read(extractParameters(description_1 || '')), false), __read(extractParameters(content)), false))), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
1029
|
+
var parameterName = _k.value;
|
|
1030
|
+
dependentParameterNames.push(parameterName);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1034
|
+
finally {
|
|
1035
|
+
try {
|
|
1036
|
+
if (_k && !_k.done && (_f = _j.return)) _f.call(_j);
|
|
1037
|
+
}
|
|
1038
|
+
finally { if (e_4) throw e_4.error; }
|
|
1039
|
+
}
|
|
1040
|
+
if (executionType === 'SCRIPT') {
|
|
1041
|
+
try {
|
|
1042
|
+
for (var _l = (e_5 = void 0, __values(extractVariables(content))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1043
|
+
var parameterName = _m.value;
|
|
1044
|
+
dependentParameterNames.push(parameterName);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1048
|
+
finally {
|
|
1049
|
+
try {
|
|
1050
|
+
if (_m && !_m.done && (_g = _l.return)) _g.call(_l);
|
|
1051
|
+
}
|
|
1052
|
+
finally { if (e_5) throw e_5.error; }
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
dependentParameterNames = __spreadArray([], __read(new Set(dependentParameterNames)), false);
|
|
1056
|
+
promptbookJson.promptTemplates.push({
|
|
1057
|
+
name: normalizeTo_PascalCase(section.title),
|
|
1058
|
+
title: section.title,
|
|
1059
|
+
description: description_1,
|
|
1060
|
+
dependentParameterNames: dependentParameterNames,
|
|
1061
|
+
executionType: executionType,
|
|
1062
|
+
jokers: jokers,
|
|
1063
|
+
postprocessing: postprocessing,
|
|
1064
|
+
expectAmount: expectAmount,
|
|
1065
|
+
expectFormat: expectFormat,
|
|
1066
|
+
modelRequirements: templateModelRequirements,
|
|
1067
|
+
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
1068
|
+
content: content,
|
|
1069
|
+
resultingParameterName: resultingParameterName,
|
|
1070
|
+
});
|
|
1071
|
+
};
|
|
1072
|
+
try {
|
|
1073
|
+
for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
1074
|
+
var section = _d.value;
|
|
1075
|
+
_loop_1(section);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1079
|
+
finally {
|
|
1080
|
+
try {
|
|
1081
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
1082
|
+
}
|
|
1083
|
+
finally { if (e_2) throw e_2.error; }
|
|
1084
|
+
}
|
|
1085
|
+
// =============================================================
|
|
1086
|
+
return promptbookJson;
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* TODO: Report here line/column of error
|
|
1090
|
+
* TODO: Use spaceTrim more effectively
|
|
1091
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
1092
|
+
*/
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* Add or modify an auto-generated section in a markdown file
|
|
1096
|
+
*
|
|
1097
|
+
* @private within the library
|
|
1098
|
+
*/
|
|
1099
|
+
function addAutoGeneratedSection(content, options) {
|
|
1100
|
+
var sectionName = options.sectionName, sectionContent = options.sectionContent;
|
|
1101
|
+
var warningLine = "<!-- \u26A0\uFE0F WARNING: This section was auto-generated -->";
|
|
1102
|
+
var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
|
|
1103
|
+
var sectionMatch = content.match(sectionRegex);
|
|
1104
|
+
if (sectionMatch) {
|
|
1105
|
+
return content.replace(sectionRegex, spaceTrim(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
|
|
1106
|
+
}
|
|
1107
|
+
var placeForSection = content.match(/^##.*$/im);
|
|
1108
|
+
if (!placeForSection) {
|
|
1109
|
+
throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
|
|
148
1110
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
1111
|
+
var _a = __read(placeForSection, 1), heading = _a[0];
|
|
1112
|
+
return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* Prettify the html code
|
|
1117
|
+
*
|
|
1118
|
+
* @param html raw html code
|
|
1119
|
+
* @returns formatted html code
|
|
1120
|
+
*/
|
|
1121
|
+
function prettifyMarkdown(html) {
|
|
1122
|
+
try {
|
|
1123
|
+
return format(html, {
|
|
1124
|
+
parser: 'markdown',
|
|
1125
|
+
plugins: [parserHtml],
|
|
1126
|
+
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
1127
|
+
endOfLine: 'lf',
|
|
1128
|
+
tabWidth: 4,
|
|
1129
|
+
singleQuote: true,
|
|
1130
|
+
trailingComma: 'all',
|
|
1131
|
+
arrowParens: 'always',
|
|
1132
|
+
printWidth: 120,
|
|
1133
|
+
htmlWhitespaceSensitivity: 'ignore',
|
|
1134
|
+
jsxBracketSameLine: false,
|
|
1135
|
+
bracketSpacing: true,
|
|
1136
|
+
});
|
|
152
1137
|
}
|
|
153
|
-
|
|
154
|
-
|
|
1138
|
+
catch (error) {
|
|
1139
|
+
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
1140
|
+
error: error,
|
|
1141
|
+
html: html,
|
|
1142
|
+
});
|
|
1143
|
+
return html;
|
|
155
1144
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* !!!
|
|
1149
|
+
*/
|
|
1150
|
+
function prettifyPromptbookString(promptbookString, options) {
|
|
1151
|
+
var isGraphAdded = options.isGraphAdded, isPrettifyed = options.isPrettifyed;
|
|
1152
|
+
if (isGraphAdded) {
|
|
1153
|
+
var promptbookJson_1 = promptbookStringToJson(promptbookString);
|
|
1154
|
+
var parameterNameToTemplateName_1 = function (parameterName) {
|
|
1155
|
+
var parameter = promptbookJson_1.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
1156
|
+
if (!parameter) {
|
|
1157
|
+
throw new UnexpectedError("Could not find {".concat(parameterName, "}"));
|
|
1158
|
+
}
|
|
1159
|
+
if (parameter.isInput) {
|
|
1160
|
+
return 'input';
|
|
1161
|
+
}
|
|
1162
|
+
var template = promptbookJson_1.promptTemplates.find(function (template) { return template.resultingParameterName === parameterName; });
|
|
1163
|
+
if (!template) {
|
|
1164
|
+
throw new Error("Could not find template for {".concat(parameterName, "}"));
|
|
1165
|
+
}
|
|
1166
|
+
return 'template' + normalizeTo_PascalCase(template.title);
|
|
1167
|
+
};
|
|
1168
|
+
var promptbookMermaid_1 = spaceTrim(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(promptbookJson_1.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(promptbookJson_1.promptTemplates
|
|
1169
|
+
.flatMap(function (_a) {
|
|
1170
|
+
var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
|
|
1171
|
+
return __spreadArray([
|
|
1172
|
+
"".concat(parameterNameToTemplateName_1(resultingParameterName), "(").concat(title, ")")
|
|
1173
|
+
], __read(dependentParameterNames.map(function (dependentParameterName) {
|
|
1174
|
+
return "".concat(parameterNameToTemplateName_1(dependentParameterName), "--\"{").concat(dependentParameterName, "}\"-->").concat(parameterNameToTemplateName_1(resultingParameterName));
|
|
1175
|
+
})), false);
|
|
1176
|
+
})
|
|
1177
|
+
.join('\n')), "\n\n ").concat(block(promptbookJson_1.parameters
|
|
1178
|
+
.filter(function (_a) {
|
|
1179
|
+
var isOutput = _a.isOutput;
|
|
1180
|
+
return isOutput;
|
|
1181
|
+
})
|
|
1182
|
+
.map(function (_a) {
|
|
1183
|
+
var name = _a.name;
|
|
1184
|
+
return "".concat(parameterNameToTemplateName_1(name), "--\"{").concat(name, "}\"-->output");
|
|
1185
|
+
})
|
|
1186
|
+
.join('\n')), "\n output((Output)):::output\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n\n\n "); });
|
|
1187
|
+
var promptbookMermaidBlock = spaceTrim(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
|
|
1188
|
+
promptbookString = addAutoGeneratedSection(promptbookString, {
|
|
1189
|
+
sectionName: 'Graph',
|
|
1190
|
+
sectionContent: promptbookMermaidBlock,
|
|
1191
|
+
});
|
|
159
1192
|
}
|
|
160
|
-
|
|
1193
|
+
if (isPrettifyed) {
|
|
1194
|
+
promptbookString = prettifyMarkdown(promptbookString);
|
|
1195
|
+
}
|
|
1196
|
+
return promptbookString;
|
|
161
1197
|
}
|
|
162
1198
|
/**
|
|
163
|
-
* TODO:
|
|
1199
|
+
* TODO: [🧠] Should this be here OR in other folder
|
|
1200
|
+
* TODO: [🕌] When more than 2 functionalities, split into separate functions
|
|
1201
|
+
*/
|
|
1202
|
+
|
|
1203
|
+
/**
|
|
1204
|
+
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
1205
|
+
*/
|
|
1206
|
+
new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
|
|
1207
|
+
/**
|
|
1208
|
+
* Detects if the code is running in a Node.js environment
|
|
1209
|
+
*/
|
|
1210
|
+
var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
1211
|
+
/**
|
|
1212
|
+
* Detects if the code is running in a web worker
|
|
1213
|
+
*/
|
|
1214
|
+
new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
|
|
1215
|
+
|
|
1216
|
+
/**
|
|
1217
|
+
* !!! Initialize
|
|
1218
|
+
*/
|
|
1219
|
+
function prettifyPromptbookStringCli() {
|
|
1220
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1221
|
+
var program, filesGlob, filePaths, filePaths_1, filePaths_1_1, filePath, promptbookMarkdown, error_1, e_1_1;
|
|
1222
|
+
var e_1, _a;
|
|
1223
|
+
return __generator(this, function (_b) {
|
|
1224
|
+
switch (_b.label) {
|
|
1225
|
+
case 0:
|
|
1226
|
+
if (!isRunningInNode()) {
|
|
1227
|
+
throw new Error(spaceTrim("\n Function prettifyPromptbookStringCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use prettifyPromptbookString.\n\n "));
|
|
1228
|
+
}
|
|
1229
|
+
program = new commander.Command();
|
|
1230
|
+
program.version(PROMPTBOOK_VERSION);
|
|
1231
|
+
program.argument('<filesGlob>', 'Promptbooks to prettify');
|
|
1232
|
+
program.parse(process.argv);
|
|
1233
|
+
filesGlob = program.options.filesGlob;
|
|
1234
|
+
return [4 /*yield*/, glob(filesGlob)];
|
|
1235
|
+
case 1:
|
|
1236
|
+
filePaths = _b.sent();
|
|
1237
|
+
_b.label = 2;
|
|
1238
|
+
case 2:
|
|
1239
|
+
_b.trys.push([2, 10, 11, 12]);
|
|
1240
|
+
filePaths_1 = __values(filePaths), filePaths_1_1 = filePaths_1.next();
|
|
1241
|
+
_b.label = 3;
|
|
1242
|
+
case 3:
|
|
1243
|
+
if (!!filePaths_1_1.done) return [3 /*break*/, 9];
|
|
1244
|
+
filePath = filePaths_1_1.value;
|
|
1245
|
+
if (!filePath.endsWith('.ptbk.md')) {
|
|
1246
|
+
console.warn(colors.yellow("Skipping prettify of non-promptbook ".concat(filePath)));
|
|
1247
|
+
return [3 /*break*/, 8];
|
|
1248
|
+
}
|
|
1249
|
+
return [4 /*yield*/, readFile(filePath, 'utf-8')];
|
|
1250
|
+
case 4:
|
|
1251
|
+
promptbookMarkdown = (_b.sent());
|
|
1252
|
+
_b.label = 5;
|
|
1253
|
+
case 5:
|
|
1254
|
+
_b.trys.push([5, 7, , 8]);
|
|
1255
|
+
promptbookMarkdown = prettifyPromptbookString(promptbookMarkdown, {
|
|
1256
|
+
isGraphAdded: true,
|
|
1257
|
+
isPrettifyed: true,
|
|
1258
|
+
// <- [🕌]
|
|
1259
|
+
});
|
|
1260
|
+
return [4 /*yield*/, writeFile(filePath, promptbookMarkdown)];
|
|
1261
|
+
case 6:
|
|
1262
|
+
_b.sent();
|
|
1263
|
+
console.info(colors.america("Prettify ".concat(filePath)));
|
|
1264
|
+
return [3 /*break*/, 8];
|
|
1265
|
+
case 7:
|
|
1266
|
+
error_1 = _b.sent();
|
|
1267
|
+
if (!(error_1 instanceof Error)) {
|
|
1268
|
+
throw error_1;
|
|
1269
|
+
}
|
|
1270
|
+
console.info(colors.bgWhite('========================='));
|
|
1271
|
+
console.info(colors.red("Prettify error ".concat(filePath)));
|
|
1272
|
+
console.error(colors.bgRed(error_1.name));
|
|
1273
|
+
console.error(error_1);
|
|
1274
|
+
console.info(colors.bgWhite('========================='));
|
|
1275
|
+
return [3 /*break*/, 8];
|
|
1276
|
+
case 8:
|
|
1277
|
+
filePaths_1_1 = filePaths_1.next();
|
|
1278
|
+
return [3 /*break*/, 3];
|
|
1279
|
+
case 9: return [3 /*break*/, 12];
|
|
1280
|
+
case 10:
|
|
1281
|
+
e_1_1 = _b.sent();
|
|
1282
|
+
e_1 = { error: e_1_1 };
|
|
1283
|
+
return [3 /*break*/, 12];
|
|
1284
|
+
case 11:
|
|
1285
|
+
try {
|
|
1286
|
+
if (filePaths_1_1 && !filePaths_1_1.done && (_a = filePaths_1.return)) _a.call(filePaths_1);
|
|
1287
|
+
}
|
|
1288
|
+
finally { if (e_1) throw e_1.error; }
|
|
1289
|
+
return [7 /*endfinally*/];
|
|
1290
|
+
case 12: return [2 /*return*/];
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* TODO: [🥠] Do not export to utils directly, its just for CLI script
|
|
1297
|
+
* TODO: [🌰] Use just prettifyPromptbookStringCli
|
|
1298
|
+
* Note: 11:11
|
|
164
1299
|
*/
|
|
165
1300
|
|
|
166
1301
|
/**
|
|
@@ -334,38 +1469,6 @@ function escapeMarkdownBlock(value) {
|
|
|
334
1469
|
return value.replace(/```/g, '\\`\\`\\`');
|
|
335
1470
|
}
|
|
336
1471
|
|
|
337
|
-
/**
|
|
338
|
-
* Prettify the html code
|
|
339
|
-
*
|
|
340
|
-
* @param html raw html code
|
|
341
|
-
* @returns formatted html code
|
|
342
|
-
*/
|
|
343
|
-
function prettifyMarkdown(html) {
|
|
344
|
-
try {
|
|
345
|
-
return format(html, {
|
|
346
|
-
parser: 'markdown',
|
|
347
|
-
plugins: [parserHtml],
|
|
348
|
-
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
349
|
-
endOfLine: 'lf',
|
|
350
|
-
tabWidth: 4,
|
|
351
|
-
singleQuote: true,
|
|
352
|
-
trailingComma: 'all',
|
|
353
|
-
arrowParens: 'always',
|
|
354
|
-
printWidth: 120,
|
|
355
|
-
htmlWhitespaceSensitivity: 'ignore',
|
|
356
|
-
jsxBracketSameLine: false,
|
|
357
|
-
bracketSpacing: true,
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
catch (error) {
|
|
361
|
-
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
362
|
-
error: error,
|
|
363
|
-
html: html,
|
|
364
|
-
});
|
|
365
|
-
return html;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
1472
|
/**
|
|
370
1473
|
* The thresholds for the relative time in the `moment` library.
|
|
371
1474
|
*
|
|
@@ -653,152 +1756,6 @@ function isValidJsonString(value) {
|
|
|
653
1756
|
}
|
|
654
1757
|
}
|
|
655
1758
|
|
|
656
|
-
/**
|
|
657
|
-
* Extracts all code blocks from markdown.
|
|
658
|
-
*
|
|
659
|
-
* Note: There are 3 simmilar function:
|
|
660
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
661
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
662
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
663
|
-
*
|
|
664
|
-
* @param markdown any valid markdown
|
|
665
|
-
* @returns code blocks with language and content
|
|
666
|
-
*
|
|
667
|
-
*/
|
|
668
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
669
|
-
var e_1, _a;
|
|
670
|
-
var codeBlocks = [];
|
|
671
|
-
var lines = markdown.split('\n');
|
|
672
|
-
var currentCodeBlock = null;
|
|
673
|
-
try {
|
|
674
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
675
|
-
var line = lines_1_1.value;
|
|
676
|
-
if (line.startsWith('```')) {
|
|
677
|
-
var language = line.slice(3).trim() || null;
|
|
678
|
-
if (currentCodeBlock === null) {
|
|
679
|
-
currentCodeBlock = { language: language, content: '' };
|
|
680
|
-
}
|
|
681
|
-
else {
|
|
682
|
-
if (language !== null) {
|
|
683
|
-
// [🌻]
|
|
684
|
-
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
685
|
-
}
|
|
686
|
-
codeBlocks.push(currentCodeBlock);
|
|
687
|
-
currentCodeBlock = null;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
else if (currentCodeBlock !== null) {
|
|
691
|
-
if (currentCodeBlock.content !== '') {
|
|
692
|
-
currentCodeBlock.content += '\n';
|
|
693
|
-
}
|
|
694
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
699
|
-
finally {
|
|
700
|
-
try {
|
|
701
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
702
|
-
}
|
|
703
|
-
finally { if (e_1) throw e_1.error; }
|
|
704
|
-
}
|
|
705
|
-
if (currentCodeBlock !== null) {
|
|
706
|
-
// [🌻]
|
|
707
|
-
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
708
|
-
}
|
|
709
|
-
return codeBlocks;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
/**
|
|
713
|
-
* Utility function to extract all list items from markdown
|
|
714
|
-
*
|
|
715
|
-
* Note: It works with both ul and ol
|
|
716
|
-
* Note: It omits list items in code blocks
|
|
717
|
-
* Note: It flattens nested lists
|
|
718
|
-
* Note: It can not work with html syntax and comments
|
|
719
|
-
*
|
|
720
|
-
* @param markdown any valid markdown
|
|
721
|
-
* @returns
|
|
722
|
-
*/
|
|
723
|
-
function extractAllListItemsFromMarkdown(markdown) {
|
|
724
|
-
var e_1, _a;
|
|
725
|
-
var lines = markdown.split('\n');
|
|
726
|
-
var listItems = [];
|
|
727
|
-
var isInCodeBlock = false;
|
|
728
|
-
try {
|
|
729
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
730
|
-
var line = lines_1_1.value;
|
|
731
|
-
var trimmedLine = line.trim();
|
|
732
|
-
if (trimmedLine.startsWith('```')) {
|
|
733
|
-
isInCodeBlock = !isInCodeBlock;
|
|
734
|
-
}
|
|
735
|
-
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
736
|
-
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
737
|
-
listItems.push(listItem);
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
742
|
-
finally {
|
|
743
|
-
try {
|
|
744
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
745
|
-
}
|
|
746
|
-
finally { if (e_1) throw e_1.error; }
|
|
747
|
-
}
|
|
748
|
-
return listItems;
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
/**
|
|
752
|
-
* Extracts exactly ONE code block from markdown.
|
|
753
|
-
*
|
|
754
|
-
* Note: If there are multiple or no code blocks the function throws an error
|
|
755
|
-
*
|
|
756
|
-
* Note: There are 3 simmilar function:
|
|
757
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
758
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
759
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
760
|
-
*
|
|
761
|
-
* @param markdown any valid markdown
|
|
762
|
-
* @returns code block with language and content
|
|
763
|
-
*/
|
|
764
|
-
function extractOneBlockFromMarkdown(markdown) {
|
|
765
|
-
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
766
|
-
if (codeBlocks.length !== 1) {
|
|
767
|
-
// TODO: Report more specific place where the error happened
|
|
768
|
-
throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
|
|
769
|
-
}
|
|
770
|
-
return codeBlocks[0];
|
|
771
|
-
}
|
|
772
|
-
/***
|
|
773
|
-
* TODO: [🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
774
|
-
*/
|
|
775
|
-
|
|
776
|
-
/**
|
|
777
|
-
* Removes HTML or Markdown comments from a string.
|
|
778
|
-
*
|
|
779
|
-
* @param {string} content - The string to remove comments from.
|
|
780
|
-
* @returns {string} The input string with all comments removed.
|
|
781
|
-
*/
|
|
782
|
-
function removeContentComments(content) {
|
|
783
|
-
return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
/**
|
|
787
|
-
* Removes Markdown formatting tags from a string.
|
|
788
|
-
*
|
|
789
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
790
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
791
|
-
*/
|
|
792
|
-
function removeMarkdownFormatting(str) {
|
|
793
|
-
// Remove bold formatting
|
|
794
|
-
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
795
|
-
// Remove italic formatting
|
|
796
|
-
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
797
|
-
// Remove code formatting
|
|
798
|
-
str = str.replace(/`(.*?)`/g, '$1');
|
|
799
|
-
return str;
|
|
800
|
-
}
|
|
801
|
-
|
|
802
1759
|
/**
|
|
803
1760
|
* Extracts code block from markdown.
|
|
804
1761
|
*
|
|
@@ -939,5 +1896,16 @@ function unwrapResult(text, options) {
|
|
|
939
1896
|
* TODO: [🧠] Should this also unwrap the (parenthesis)
|
|
940
1897
|
*/
|
|
941
1898
|
|
|
942
|
-
|
|
1899
|
+
/**
|
|
1900
|
+
* Hidden utilities which should not be used by external consumers.
|
|
1901
|
+
*/
|
|
1902
|
+
var __ = {
|
|
1903
|
+
// Note: [🥠]
|
|
1904
|
+
prettifyPromptbookStringCli: prettifyPromptbookStringCli,
|
|
1905
|
+
};
|
|
1906
|
+
/**
|
|
1907
|
+
* TODO: [🧠] Maybe create some indipendent package like `markdown-tools` from both here exported and @private utilities
|
|
1908
|
+
*/
|
|
1909
|
+
|
|
1910
|
+
export { CountUtils, ExecutionReportStringOptionsDefaults, __, assertsExecutionSuccessful, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, executionReportJsonToString, extractAllBlocksFromMarkdown, extractAllListItemsFromMarkdown, extractBlock, extractOneBlockFromMarkdown, isValidJsonString, parseNumber, prettifyPromptbookString, removeContentComments, removeEmojis, removeMarkdownFormatting, removeQuotes, trimCodeBlock, trimEndOfCodeBlock, unwrapResult };
|
|
943
1911
|
//# sourceMappingURL=index.es.js.map
|