@promptbook/node 0.59.0-3 → 0.59.0-8
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/esm/index.es.js +2539 -1575
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/core.index.d.ts +2 -1
- package/esm/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/esm/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/esm/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/esm/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +1 -1
- package/package.json +2 -2
- package/umd/index.umd.js +2539 -1575
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/core.index.d.ts +2 -1
- package/umd/typings/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/umd/typings/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/umd/typings/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/umd/typings/types/PromptbookJson/MaterialKnowledgePieceJson.d.ts +1 -1
package/esm/index.es.js
CHANGED
|
@@ -137,49 +137,64 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
|
137
137
|
return PromptbookSyntaxError;
|
|
138
138
|
}(Error));
|
|
139
139
|
|
|
140
|
-
// import prepareKnowledgeFromMarkdownStringPromptbook from './prepare-knowledge-from-markdown.ptbk.md';
|
|
141
|
-
function prepareKnowledgeFromMarkdown(options) {
|
|
142
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
143
|
-
return __generator(this, function (_a) {
|
|
144
|
-
return [2 /*return*/, []];
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
140
|
/**
|
|
150
|
-
*
|
|
141
|
+
* This error indicates errors during the execution of the promptbook
|
|
151
142
|
*/
|
|
152
|
-
var
|
|
143
|
+
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
144
|
+
__extends(PromptbookExecutionError, _super);
|
|
145
|
+
function PromptbookExecutionError(message) {
|
|
146
|
+
var _this = _super.call(this, message) || this;
|
|
147
|
+
_this.name = 'PromptbookExecutionError';
|
|
148
|
+
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
149
|
+
return _this;
|
|
150
|
+
}
|
|
151
|
+
return PromptbookExecutionError;
|
|
152
|
+
}(Error));
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
|
-
*
|
|
155
|
+
* Asserts that the execution of a promptnook is successful
|
|
156
156
|
*
|
|
157
|
-
* @
|
|
157
|
+
* @param executionResult - The partial result of the promptnook execution
|
|
158
|
+
* @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
|
|
158
159
|
*/
|
|
159
|
-
function
|
|
160
|
-
var
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
164
|
-
var section = _c.value;
|
|
165
|
-
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
166
|
-
}
|
|
160
|
+
function assertsExecutionSuccessful(executionResult) {
|
|
161
|
+
var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
|
|
162
|
+
if (isSuccessful === true) {
|
|
163
|
+
return;
|
|
167
164
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
165
|
+
if (errors.length === 0) {
|
|
166
|
+
throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
|
|
167
|
+
}
|
|
168
|
+
else if (errors.length === 1) {
|
|
169
|
+
throw errors[0];
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
|
|
174
173
|
}
|
|
175
|
-
return maxDeepness + 1;
|
|
176
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* TODO: [🧠] Can this return type be better typed than void
|
|
177
|
+
*/
|
|
177
178
|
|
|
178
179
|
/**
|
|
179
180
|
* The maximum number of iterations for a loops
|
|
180
181
|
*/
|
|
181
182
|
var LOOP_LIMIT = 1000;
|
|
182
183
|
|
|
184
|
+
/**
|
|
185
|
+
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
186
|
+
*/
|
|
187
|
+
var PromptbookLogicError = /** @class */ (function (_super) {
|
|
188
|
+
__extends(PromptbookLogicError, _super);
|
|
189
|
+
function PromptbookLogicError(message) {
|
|
190
|
+
var _this = _super.call(this, message) || this;
|
|
191
|
+
_this.name = 'PromptbookLogicError';
|
|
192
|
+
Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
|
|
193
|
+
return _this;
|
|
194
|
+
}
|
|
195
|
+
return PromptbookLogicError;
|
|
196
|
+
}(Error));
|
|
197
|
+
|
|
183
198
|
/**
|
|
184
199
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
185
200
|
*/
|
|
@@ -195,804 +210,400 @@ var UnexpectedError = /** @class */ (function (_super) {
|
|
|
195
210
|
}(Error));
|
|
196
211
|
|
|
197
212
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* Note: This function does work with code blocks
|
|
201
|
-
* Note: This function does not work with markdown comments
|
|
202
|
-
*
|
|
203
|
-
* @param markdown The markdown string to parse.
|
|
204
|
-
* @returns The MarkdownStructure object.
|
|
213
|
+
* Tests if given string is valid URL.
|
|
205
214
|
*
|
|
206
|
-
*
|
|
215
|
+
* Note: Dataurl are considered perfectly valid.
|
|
207
216
|
*/
|
|
208
|
-
function
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
var current = root;
|
|
213
|
-
var isInsideCodeBlock = false;
|
|
217
|
+
function isValidUrl(url) {
|
|
218
|
+
if (typeof url !== 'string') {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
214
221
|
try {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
218
|
-
if (isInsideCodeBlock || !headingMatch) {
|
|
219
|
-
if (line.startsWith('```')) {
|
|
220
|
-
isInsideCodeBlock = !isInsideCodeBlock;
|
|
221
|
-
}
|
|
222
|
-
current.contentLines.push(line);
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
var level = headingMatch.groups.mark.length;
|
|
226
|
-
var title = headingMatch.groups.title.trim();
|
|
227
|
-
var parent_1 = void 0;
|
|
228
|
-
if (level > current.level) {
|
|
229
|
-
// Note: Going deeper (next section is child of current)
|
|
230
|
-
parent_1 = current;
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
234
|
-
parent_1 = current;
|
|
235
|
-
var loopLimit = LOOP_LIMIT;
|
|
236
|
-
while (parent_1.level !== level - 1) {
|
|
237
|
-
if (loopLimit-- < 0) {
|
|
238
|
-
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
239
|
-
}
|
|
240
|
-
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
241
|
-
// [🌻]
|
|
242
|
-
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
243
|
-
}
|
|
244
|
-
parent_1 = parent_1.parent;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
248
|
-
parent_1.sections.push(section);
|
|
249
|
-
current = section;
|
|
250
|
-
}
|
|
222
|
+
if (url.startsWith('blob:')) {
|
|
223
|
+
url = url.replace(/^blob:/, '');
|
|
251
224
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
226
|
+
var urlObject = new URL(url);
|
|
227
|
+
if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
|
|
228
|
+
return false;
|
|
257
229
|
}
|
|
258
|
-
|
|
230
|
+
return true;
|
|
259
231
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return markdownStructure;
|
|
232
|
+
catch (error) {
|
|
233
|
+
return false;
|
|
263
234
|
}
|
|
264
|
-
// [🌻]
|
|
265
|
-
throw new Error('The markdown file must have exactly one top-level section.');
|
|
266
|
-
// return root;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* @private
|
|
270
|
-
*/
|
|
271
|
-
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
272
|
-
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
273
|
-
return {
|
|
274
|
-
level: level,
|
|
275
|
-
title: title,
|
|
276
|
-
content: spaceTrim(contentLines.join('\n')),
|
|
277
|
-
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
278
|
-
};
|
|
279
235
|
}
|
|
280
236
|
|
|
281
237
|
/**
|
|
282
|
-
*
|
|
238
|
+
* Validates PromptbookJson if it is logically valid
|
|
283
239
|
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* Note: It flattens nested lists
|
|
287
|
-
* Note: It can not work with html syntax and comments
|
|
240
|
+
* It checks:
|
|
241
|
+
* - if it has correct parameters dependency
|
|
288
242
|
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
243
|
+
* It does NOT check:
|
|
244
|
+
* - if it is valid json
|
|
245
|
+
* - if it is meaningful
|
|
246
|
+
*
|
|
247
|
+
* @param promptbook valid or invalid PromptbookJson
|
|
248
|
+
* @returns the same promptbook if it is logically valid
|
|
249
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
291
250
|
*/
|
|
292
|
-
function
|
|
293
|
-
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
|
|
251
|
+
function validatePromptbookJson(promptbook) {
|
|
252
|
+
// TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
|
|
253
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
254
|
+
if (promptbook.promptbookUrl !== undefined) {
|
|
255
|
+
if (!isValidUrl(promptbook.promptbookUrl)) {
|
|
256
|
+
// TODO: This should be maybe the syntax error detected during parsing
|
|
257
|
+
throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
261
|
+
if (!Array.isArray(promptbook.parameters)) {
|
|
262
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
263
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
264
|
+
}
|
|
265
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
266
|
+
if (!Array.isArray(promptbook.promptTemplates)) {
|
|
267
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
268
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
|
|
269
|
+
}
|
|
270
|
+
var _loop_1 = function (parameter) {
|
|
271
|
+
if (parameter.isInput && parameter.isOutput) {
|
|
272
|
+
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
273
|
+
}
|
|
274
|
+
// Note: Testing that parameter is either intermediate or output BUT not created and unused
|
|
275
|
+
if (!parameter.isInput &&
|
|
276
|
+
!parameter.isOutput &&
|
|
277
|
+
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
278
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
279
|
+
}
|
|
280
|
+
// Note: Testing that parameter is either input or result of some template
|
|
281
|
+
if (!parameter.isInput &&
|
|
282
|
+
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
283
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
284
|
+
}
|
|
285
|
+
};
|
|
297
286
|
try {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var
|
|
301
|
-
|
|
302
|
-
isInCodeBlock = !isInCodeBlock;
|
|
303
|
-
}
|
|
304
|
-
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
305
|
-
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
306
|
-
listItems.push(listItem);
|
|
307
|
-
}
|
|
287
|
+
// Note: Check each parameter individually
|
|
288
|
+
for (var _e = __values(promptbook.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
289
|
+
var parameter = _f.value;
|
|
290
|
+
_loop_1(parameter);
|
|
308
291
|
}
|
|
309
292
|
}
|
|
310
293
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
311
294
|
finally {
|
|
312
295
|
try {
|
|
313
|
-
if (
|
|
296
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
314
297
|
}
|
|
315
298
|
finally { if (e_1) throw e_1.error; }
|
|
316
299
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Extracts all code blocks from markdown.
|
|
330
|
-
*
|
|
331
|
-
* Note: There are 3 simmilar function:
|
|
332
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
333
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
334
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
335
|
-
*
|
|
336
|
-
* @param markdown any valid markdown
|
|
337
|
-
* @returns code blocks with language and content
|
|
338
|
-
*
|
|
339
|
-
*/
|
|
340
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
341
|
-
var e_1, _a;
|
|
342
|
-
var codeBlocks = [];
|
|
343
|
-
var lines = markdown.split('\n');
|
|
344
|
-
var currentCodeBlock = null;
|
|
300
|
+
// Note: Check each template individually
|
|
301
|
+
var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
|
|
302
|
+
var isInput = _a.isInput;
|
|
303
|
+
return isInput;
|
|
304
|
+
}).map(function (_a) {
|
|
305
|
+
var name = _a.name;
|
|
306
|
+
return name;
|
|
307
|
+
}));
|
|
345
308
|
try {
|
|
346
|
-
for (var
|
|
347
|
-
var
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
309
|
+
for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
310
|
+
var template = _h.value;
|
|
311
|
+
if (definedParameters.has(template.resultingParameterName)) {
|
|
312
|
+
throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
|
|
313
|
+
}
|
|
314
|
+
definedParameters.add(template.resultingParameterName);
|
|
315
|
+
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
316
|
+
(template.modelRequirements.modelVariant === undefined)) {
|
|
317
|
+
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
318
|
+
}
|
|
319
|
+
if (template.jokers && template.jokers.length > 0) {
|
|
320
|
+
if (!template.expectFormat &&
|
|
321
|
+
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
322
|
+
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
326
|
+
var joker = _k.value;
|
|
327
|
+
if (!template.dependentParameterNames.includes(joker)) {
|
|
328
|
+
throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
|
|
329
|
+
}
|
|
357
330
|
}
|
|
358
|
-
|
|
359
|
-
|
|
331
|
+
}
|
|
332
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
333
|
+
finally {
|
|
334
|
+
try {
|
|
335
|
+
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
336
|
+
}
|
|
337
|
+
finally { if (e_3) throw e_3.error; }
|
|
360
338
|
}
|
|
361
339
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
340
|
+
if (template.expectations) {
|
|
341
|
+
try {
|
|
342
|
+
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
343
|
+
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
344
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
345
|
+
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
346
|
+
}
|
|
347
|
+
if (min !== undefined && min < 0) {
|
|
348
|
+
throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
|
|
349
|
+
}
|
|
350
|
+
if (max !== undefined && max <= 0) {
|
|
351
|
+
throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
356
|
+
finally {
|
|
357
|
+
try {
|
|
358
|
+
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
359
|
+
}
|
|
360
|
+
finally { if (e_4) throw e_4.error; }
|
|
365
361
|
}
|
|
366
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
367
362
|
}
|
|
368
363
|
}
|
|
369
364
|
}
|
|
370
|
-
catch (
|
|
365
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
371
366
|
finally {
|
|
372
367
|
try {
|
|
373
|
-
if (
|
|
368
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
374
369
|
}
|
|
375
|
-
finally { if (
|
|
370
|
+
finally { if (e_2) throw e_2.error; }
|
|
376
371
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
372
|
+
// Note: Detect circular dependencies
|
|
373
|
+
var resovedParameters = promptbook.parameters
|
|
374
|
+
.filter(function (_a) {
|
|
375
|
+
var isInput = _a.isInput;
|
|
376
|
+
return isInput;
|
|
377
|
+
})
|
|
378
|
+
.map(function (_a) {
|
|
379
|
+
var name = _a.name;
|
|
380
|
+
return name;
|
|
381
|
+
});
|
|
382
|
+
var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
383
|
+
var loopLimit = LOOP_LIMIT;
|
|
384
|
+
var _loop_2 = function () {
|
|
385
|
+
if (loopLimit-- < 0) {
|
|
386
|
+
throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
|
|
387
|
+
}
|
|
388
|
+
var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
|
|
389
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
390
|
+
});
|
|
391
|
+
if (currentlyResovedTemplates.length === 0) {
|
|
392
|
+
throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
393
|
+
.map(function (_a) {
|
|
394
|
+
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
395
|
+
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
396
|
+
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
397
|
+
.join(', '));
|
|
398
|
+
})
|
|
399
|
+
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
|
|
400
|
+
}
|
|
401
|
+
resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
|
|
402
|
+
var resultingParameterName = _a.resultingParameterName;
|
|
403
|
+
return resultingParameterName;
|
|
404
|
+
})), false);
|
|
405
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
|
|
406
|
+
};
|
|
407
|
+
while (unresovedTemplates.length > 0) {
|
|
408
|
+
_loop_2();
|
|
380
409
|
}
|
|
381
|
-
return
|
|
410
|
+
return promptbook;
|
|
382
411
|
}
|
|
412
|
+
/**
|
|
413
|
+
* TODO: [🧠] Work with promptbookVersion
|
|
414
|
+
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
415
|
+
* > /**
|
|
416
|
+
* > * Validates PromptbookJson if it is logically valid.
|
|
417
|
+
* > *
|
|
418
|
+
* > * It checks:
|
|
419
|
+
* > * - it has a valid structure
|
|
420
|
+
* > * - ...
|
|
421
|
+
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
422
|
+
*/
|
|
383
423
|
|
|
384
424
|
/**
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* Note: If there are multiple or no code blocks the function throws an error
|
|
388
|
-
*
|
|
389
|
-
* Note: There are 3 simmilar function:
|
|
390
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
391
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
392
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
425
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
393
426
|
*
|
|
394
|
-
* @
|
|
395
|
-
*
|
|
427
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
428
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
396
429
|
*/
|
|
397
|
-
function
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
430
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
431
|
+
__extends(ExpectError, _super);
|
|
432
|
+
function ExpectError(message) {
|
|
433
|
+
var _this = _super.call(this, message) || this;
|
|
434
|
+
_this.name = 'ExpectError';
|
|
435
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
436
|
+
return _this;
|
|
402
437
|
}
|
|
403
|
-
return
|
|
404
|
-
}
|
|
405
|
-
/***
|
|
406
|
-
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
407
|
-
*/
|
|
438
|
+
return ExpectError;
|
|
439
|
+
}(Error));
|
|
408
440
|
|
|
409
441
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
* @param {string} content - The string to remove comments from.
|
|
413
|
-
* @returns {string} The input string with all comments removed.
|
|
442
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
414
443
|
*/
|
|
415
|
-
function
|
|
416
|
-
|
|
444
|
+
function isValidJsonString(value /* <-[👨⚖️] */) {
|
|
445
|
+
try {
|
|
446
|
+
JSON.parse(value);
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
catch (error) {
|
|
450
|
+
if (!(error instanceof Error)) {
|
|
451
|
+
throw error;
|
|
452
|
+
}
|
|
453
|
+
if (error.message.includes('Unexpected token')) {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
417
458
|
}
|
|
418
459
|
|
|
419
460
|
/**
|
|
420
|
-
*
|
|
461
|
+
* The version of the Promptbook library
|
|
421
462
|
*/
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
463
|
+
var PROMPTBOOK_VERSION = '0.59.0-7';
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Function `addUsage` will add multiple usages into one
|
|
467
|
+
*
|
|
468
|
+
* Note: If you provide 0 values, it returns void usage
|
|
469
|
+
*/
|
|
470
|
+
function addUsage() {
|
|
471
|
+
var usageItems = [];
|
|
425
472
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
var
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
473
|
+
usageItems[_i] = arguments[_i];
|
|
474
|
+
}
|
|
475
|
+
var initialStructure = {
|
|
476
|
+
price: { value: 0 },
|
|
477
|
+
input: {
|
|
478
|
+
tokensCount: { value: 0 },
|
|
479
|
+
charactersCount: { value: 0 },
|
|
480
|
+
wordsCount: { value: 0 },
|
|
481
|
+
sentencesCount: { value: 0 },
|
|
482
|
+
linesCount: { value: 0 },
|
|
483
|
+
paragraphsCount: { value: 0 },
|
|
484
|
+
pagesCount: { value: 0 },
|
|
485
|
+
},
|
|
486
|
+
output: {
|
|
487
|
+
tokensCount: { value: 0 },
|
|
488
|
+
charactersCount: { value: 0 },
|
|
489
|
+
wordsCount: { value: 0 },
|
|
490
|
+
sentencesCount: { value: 0 },
|
|
491
|
+
linesCount: { value: 0 },
|
|
492
|
+
paragraphsCount: { value: 0 },
|
|
493
|
+
pagesCount: { value: 0 },
|
|
494
|
+
},
|
|
495
|
+
};
|
|
496
|
+
return usageItems.reduce(function (acc, item) {
|
|
497
|
+
var e_1, _a, e_2, _b;
|
|
498
|
+
var _c;
|
|
499
|
+
acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
|
|
500
|
+
try {
|
|
501
|
+
for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
502
|
+
var key = _e.value;
|
|
503
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
504
|
+
//@ts-ignore
|
|
505
|
+
if (item.input[key]) {
|
|
506
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
507
|
+
//@ts-ignore
|
|
508
|
+
acc.input[key].value += item.input[key].value || 0;
|
|
509
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
510
|
+
//@ts-ignore
|
|
511
|
+
if (item.input[key].isUncertain) {
|
|
512
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
513
|
+
//@ts-ignore
|
|
514
|
+
acc.input[key].isUncertain = true;
|
|
515
|
+
}
|
|
436
516
|
}
|
|
437
517
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
finally { if (e_2) throw e_2.error; }
|
|
518
|
+
}
|
|
519
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
520
|
+
finally {
|
|
521
|
+
try {
|
|
522
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
444
523
|
}
|
|
524
|
+
finally { if (e_1) throw e_1.error; }
|
|
445
525
|
}
|
|
446
|
-
}
|
|
447
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
448
|
-
finally {
|
|
449
526
|
try {
|
|
450
|
-
|
|
527
|
+
for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
528
|
+
var key = _g.value;
|
|
529
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
530
|
+
//@ts-ignore
|
|
531
|
+
if (item.output[key]) {
|
|
532
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
533
|
+
//@ts-ignore
|
|
534
|
+
acc.output[key].value += item.output[key].value || 0;
|
|
535
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
536
|
+
//@ts-ignore
|
|
537
|
+
if (item.output[key].isUncertain) {
|
|
538
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
539
|
+
//@ts-ignore
|
|
540
|
+
acc.output[key].isUncertain = true;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
451
544
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
545
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
546
|
+
finally {
|
|
547
|
+
try {
|
|
548
|
+
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
549
|
+
}
|
|
550
|
+
finally { if (e_2) throw e_2.error; }
|
|
551
|
+
}
|
|
552
|
+
return acc;
|
|
553
|
+
}, initialStructure);
|
|
455
554
|
}
|
|
456
555
|
|
|
457
556
|
/**
|
|
458
|
-
*
|
|
557
|
+
* Counts number of characters in the text
|
|
459
558
|
*/
|
|
460
|
-
|
|
559
|
+
function countCharacters(text) {
|
|
560
|
+
// Remove null characters
|
|
561
|
+
text = text.replace(/\0/g, '');
|
|
562
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
563
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
564
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
565
|
+
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
566
|
+
return text.length;
|
|
567
|
+
}
|
|
461
568
|
|
|
462
569
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @param template the template with parameters in {curly} braces
|
|
466
|
-
* @returns the list of parameter names
|
|
570
|
+
* Counts number of lines in the text
|
|
467
571
|
*/
|
|
468
|
-
function
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
var parameterNames = new Set();
|
|
472
|
-
try {
|
|
473
|
-
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
474
|
-
var match = matches_1_1.value;
|
|
475
|
-
var parameterName = match[0].slice(1, -1);
|
|
476
|
-
parameterNames.add(parameterName);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
480
|
-
finally {
|
|
481
|
-
try {
|
|
482
|
-
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
483
|
-
}
|
|
484
|
-
finally { if (e_1) throw e_1.error; }
|
|
572
|
+
function countLines(text) {
|
|
573
|
+
if (text === '') {
|
|
574
|
+
return 0;
|
|
485
575
|
}
|
|
486
|
-
return
|
|
576
|
+
return text.split('\n').length;
|
|
487
577
|
}
|
|
488
578
|
|
|
489
579
|
/**
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
* @param script from which to extract the variables
|
|
493
|
-
* @returns the list of variable names
|
|
494
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
580
|
+
* Counts number of pages in the text
|
|
495
581
|
*/
|
|
496
|
-
function
|
|
497
|
-
var
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
try {
|
|
502
|
-
eval(script);
|
|
503
|
-
}
|
|
504
|
-
catch (error) {
|
|
505
|
-
if (!(error instanceof ReferenceError)) {
|
|
506
|
-
throw error;
|
|
507
|
-
}
|
|
508
|
-
var undefinedName = error.message.split(' ')[0];
|
|
509
|
-
/*
|
|
510
|
-
Note: Remapping error
|
|
511
|
-
From: [ReferenceError: thing is not defined],
|
|
512
|
-
To: [Error: Parameter {thing} is not defined],
|
|
513
|
-
*/
|
|
514
|
-
if (!undefinedName) {
|
|
515
|
-
throw error;
|
|
516
|
-
}
|
|
517
|
-
if (script.includes(undefinedName + '(')) {
|
|
518
|
-
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
521
|
-
variables.add(undefinedName);
|
|
522
|
-
script = "const ".concat(undefinedName, " = '';") + script;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
catch (error) {
|
|
527
|
-
if (!(error instanceof Error)) {
|
|
528
|
-
throw error;
|
|
529
|
-
}
|
|
530
|
-
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 "); }));
|
|
531
|
-
}
|
|
532
|
-
return variables;
|
|
582
|
+
function countPages(text) {
|
|
583
|
+
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
584
|
+
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
585
|
+
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
586
|
+
return pageCount;
|
|
533
587
|
}
|
|
534
|
-
/**
|
|
535
|
-
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
536
|
-
*/
|
|
537
588
|
|
|
538
589
|
/**
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
* @param promptTemplate the template with used parameters
|
|
542
|
-
* @returns the set of parameter names
|
|
543
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
544
|
-
*/
|
|
545
|
-
function extractParametersFromPromptTemplate(promptTemplate) {
|
|
546
|
-
var e_1, _a, e_2, _b;
|
|
547
|
-
var parameterNames = new Set();
|
|
548
|
-
try {
|
|
549
|
-
for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
550
|
-
var parameterName = _d.value;
|
|
551
|
-
parameterNames.add(parameterName);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
555
|
-
finally {
|
|
556
|
-
try {
|
|
557
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
558
|
-
}
|
|
559
|
-
finally { if (e_1) throw e_1.error; }
|
|
560
|
-
}
|
|
561
|
-
if (promptTemplate.executionType === 'SCRIPT') {
|
|
562
|
-
try {
|
|
563
|
-
for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
564
|
-
var parameterName = _f.value;
|
|
565
|
-
parameterNames.add(parameterName);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
569
|
-
finally {
|
|
570
|
-
try {
|
|
571
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
572
|
-
}
|
|
573
|
-
finally { if (e_2) throw e_2.error; }
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
return parameterNames;
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* TODO: [🔣] If script require contentLanguage
|
|
590
|
+
* Counts number of paragraphs in the text
|
|
580
591
|
*/
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
/*
|
|
584
|
-
TODO: Tests
|
|
585
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
586
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
587
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
588
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
589
|
-
*/
|
|
590
|
-
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
591
|
-
var e_1, _a;
|
|
592
|
-
var charType;
|
|
593
|
-
var lastCharType = 'OTHER';
|
|
594
|
-
var normalizedName = '';
|
|
595
|
-
try {
|
|
596
|
-
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
597
|
-
var char = sentence_1_1.value;
|
|
598
|
-
var normalizedChar = void 0;
|
|
599
|
-
if (/^[a-z]$/.test(char)) {
|
|
600
|
-
charType = 'LOWERCASE';
|
|
601
|
-
normalizedChar = char.toUpperCase();
|
|
602
|
-
}
|
|
603
|
-
else if (/^[A-Z]$/.test(char)) {
|
|
604
|
-
charType = 'UPPERCASE';
|
|
605
|
-
normalizedChar = char;
|
|
606
|
-
}
|
|
607
|
-
else if (/^[0-9]$/.test(char)) {
|
|
608
|
-
charType = 'NUMBER';
|
|
609
|
-
normalizedChar = char;
|
|
610
|
-
}
|
|
611
|
-
else if (/^\/$/.test(char)) {
|
|
612
|
-
charType = 'SLASH';
|
|
613
|
-
normalizedChar = char;
|
|
614
|
-
}
|
|
615
|
-
else {
|
|
616
|
-
charType = 'OTHER';
|
|
617
|
-
normalizedChar = '_';
|
|
618
|
-
}
|
|
619
|
-
if (charType !== lastCharType &&
|
|
620
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
621
|
-
!(lastCharType === 'NUMBER') &&
|
|
622
|
-
!(charType === 'NUMBER')) {
|
|
623
|
-
normalizedName += '_';
|
|
624
|
-
}
|
|
625
|
-
normalizedName += normalizedChar;
|
|
626
|
-
lastCharType = charType;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
630
|
-
finally {
|
|
631
|
-
try {
|
|
632
|
-
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
633
|
-
}
|
|
634
|
-
finally { if (e_1) throw e_1.error; }
|
|
635
|
-
}
|
|
636
|
-
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
637
|
-
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
638
|
-
normalizedName = normalizedName.replace(/^_/, '');
|
|
639
|
-
normalizedName = normalizedName.replace(/_$/, '');
|
|
640
|
-
return normalizedName;
|
|
592
|
+
function countParagraphs(text) {
|
|
593
|
+
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
641
594
|
}
|
|
642
|
-
/**
|
|
643
|
-
* TODO: [🌺] Use some intermediate util splitWords
|
|
644
|
-
*/
|
|
645
|
-
|
|
646
|
-
/**
|
|
647
|
-
* Execution type describes the way how the block is executed
|
|
648
|
-
*
|
|
649
|
-
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
650
|
-
*/
|
|
651
|
-
var ExecutionTypes = [
|
|
652
|
-
'PROMPT_TEMPLATE',
|
|
653
|
-
'SIMPLE_TEMPLATE',
|
|
654
|
-
'SCRIPT',
|
|
655
|
-
'PROMPT_DIALOG',
|
|
656
|
-
// <- [🥻] Insert here when making new command
|
|
657
|
-
];
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Units of text measurement
|
|
661
|
-
*/
|
|
662
|
-
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
|
|
663
|
-
/**
|
|
664
|
-
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
665
|
-
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
666
|
-
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
667
|
-
*/
|
|
668
595
|
|
|
669
596
|
/**
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
673
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
597
|
+
* Split text into sentences
|
|
674
598
|
*/
|
|
675
|
-
function
|
|
676
|
-
|
|
677
|
-
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
678
|
-
// Remove italic formatting
|
|
679
|
-
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
680
|
-
// Remove code formatting
|
|
681
|
-
str = str.replace(/`(.*?)`/g, '$1');
|
|
682
|
-
return str;
|
|
599
|
+
function splitIntoSentences(text) {
|
|
600
|
+
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
683
601
|
}
|
|
684
|
-
|
|
685
602
|
/**
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
689
|
-
* Note: it also works only with decimal numbers
|
|
690
|
-
*
|
|
691
|
-
* @returns parsed number
|
|
692
|
-
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
693
|
-
*
|
|
694
|
-
* @private within the parseCommand
|
|
603
|
+
* Counts number of sentences in the text
|
|
695
604
|
*/
|
|
696
|
-
function
|
|
697
|
-
|
|
698
|
-
if (typeof value === 'number') {
|
|
699
|
-
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
700
|
-
}
|
|
701
|
-
if (typeof value !== 'string') {
|
|
702
|
-
return 0;
|
|
703
|
-
}
|
|
704
|
-
value = value.trim();
|
|
705
|
-
if (value.startsWith('+')) {
|
|
706
|
-
return parseNumber(value.substring(1));
|
|
707
|
-
}
|
|
708
|
-
if (value.startsWith('-')) {
|
|
709
|
-
var number = parseNumber(value.substring(1));
|
|
710
|
-
if (number === 0) {
|
|
711
|
-
return 0; // <- Note: To prevent -0
|
|
712
|
-
}
|
|
713
|
-
return -number;
|
|
714
|
-
}
|
|
715
|
-
value = value.replace(/,/g, '.');
|
|
716
|
-
value = value.toUpperCase();
|
|
717
|
-
if (value === '') {
|
|
718
|
-
return 0;
|
|
719
|
-
}
|
|
720
|
-
if (value === '♾' || value.startsWith('INF')) {
|
|
721
|
-
return Infinity;
|
|
722
|
-
}
|
|
723
|
-
if (value.includes('/')) {
|
|
724
|
-
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
725
|
-
var numerator = parseNumber(numerator_);
|
|
726
|
-
var denominator = parseNumber(denominator_);
|
|
727
|
-
if (denominator === 0) {
|
|
728
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
729
|
-
}
|
|
730
|
-
return numerator / denominator;
|
|
731
|
-
}
|
|
732
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
733
|
-
return 0;
|
|
734
|
-
}
|
|
735
|
-
if (value.includes('E')) {
|
|
736
|
-
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
737
|
-
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
738
|
-
}
|
|
739
|
-
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
740
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
741
|
-
}
|
|
742
|
-
var num = parseFloat(value);
|
|
743
|
-
if (isNaN(num)) {
|
|
744
|
-
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
745
|
-
}
|
|
746
|
-
return num;
|
|
747
|
-
}
|
|
748
|
-
/**
|
|
749
|
-
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
750
|
-
*/
|
|
751
|
-
|
|
752
|
-
/**
|
|
753
|
-
* Parses one line of ul/ol to command
|
|
754
|
-
*
|
|
755
|
-
* @returns parsed command object
|
|
756
|
-
* @throws {PromptbookSyntaxError} if the command is invalid
|
|
757
|
-
*
|
|
758
|
-
* @private within the promptbookStringToJson
|
|
759
|
-
*/
|
|
760
|
-
function parseCommand(listItem) {
|
|
761
|
-
var e_1, _a;
|
|
762
|
-
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
763
|
-
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
764
|
-
}
|
|
765
|
-
var type = listItem.trim();
|
|
766
|
-
type = type.split('`').join('');
|
|
767
|
-
type = type.split('"').join('');
|
|
768
|
-
type = type.split("'").join('');
|
|
769
|
-
type = type.split('~').join('');
|
|
770
|
-
type = type.split('[').join('');
|
|
771
|
-
type = type.split(']').join('');
|
|
772
|
-
type = type.split('(').join('');
|
|
773
|
-
type = type.split(')').join('');
|
|
774
|
-
type = normalizeTo_SCREAMING_CASE(type);
|
|
775
|
-
type = type.split('DIALOGUE').join('DIALOG');
|
|
776
|
-
var listItemParts = listItem
|
|
777
|
-
.split(' ')
|
|
778
|
-
.map(function (part) { return part.trim(); })
|
|
779
|
-
.filter(function (item) { return item !== ''; })
|
|
780
|
-
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
781
|
-
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
782
|
-
.map(removeMarkdownFormatting);
|
|
783
|
-
if (type.startsWith('URL') ||
|
|
784
|
-
type.startsWith('PTBK_URL') ||
|
|
785
|
-
type.startsWith('PTBKURL') ||
|
|
786
|
-
type.startsWith('PROMPTBOOK_URL') ||
|
|
787
|
-
type.startsWith('PROMPTBOOKURL') ||
|
|
788
|
-
type.startsWith('HTTPS')) {
|
|
789
|
-
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
790
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
791
|
-
}
|
|
792
|
-
var promptbookUrlString = listItemParts.pop();
|
|
793
|
-
var promptbookUrl = new URL(promptbookUrlString);
|
|
794
|
-
if (promptbookUrl.protocol !== 'https:') {
|
|
795
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
796
|
-
}
|
|
797
|
-
if (promptbookUrl.hash !== '') {
|
|
798
|
-
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 ")));
|
|
799
|
-
}
|
|
800
|
-
return {
|
|
801
|
-
type: 'PROMPTBOOK_URL',
|
|
802
|
-
promptbookUrl: promptbookUrl,
|
|
803
|
-
};
|
|
804
|
-
}
|
|
805
|
-
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
806
|
-
if (listItemParts.length !== 2) {
|
|
807
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
808
|
-
}
|
|
809
|
-
var promptbookVersion = listItemParts.pop();
|
|
810
|
-
// TODO: Validate version
|
|
811
|
-
return {
|
|
812
|
-
type: 'PROMPTBOOK_VERSION',
|
|
813
|
-
promptbookVersion: promptbookVersion,
|
|
814
|
-
};
|
|
815
|
-
}
|
|
816
|
-
else if (type.startsWith('EXECUTE') ||
|
|
817
|
-
type.startsWith('EXEC') ||
|
|
818
|
-
type.startsWith('PROMPT_DIALOG') ||
|
|
819
|
-
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
820
|
-
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
821
|
-
if (executionTypes.length !== 1) {
|
|
822
|
-
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 "); }));
|
|
823
|
-
}
|
|
824
|
-
return {
|
|
825
|
-
type: 'EXECUTE',
|
|
826
|
-
executionType: executionTypes[0],
|
|
827
|
-
};
|
|
828
|
-
}
|
|
829
|
-
else if (type.startsWith('MODEL')) {
|
|
830
|
-
// TODO: Make this more elegant and dynamically
|
|
831
|
-
if (type.startsWith('MODEL_VARIANT')) {
|
|
832
|
-
if (type === 'MODEL_VARIANT_CHAT') {
|
|
833
|
-
return {
|
|
834
|
-
type: 'MODEL',
|
|
835
|
-
key: 'modelVariant',
|
|
836
|
-
value: 'CHAT',
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
|
-
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
840
|
-
return {
|
|
841
|
-
type: 'MODEL',
|
|
842
|
-
key: 'modelVariant',
|
|
843
|
-
value: 'COMPLETION',
|
|
844
|
-
};
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
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 "); }));
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
if (type.startsWith('MODEL_NAME')) {
|
|
851
|
-
return {
|
|
852
|
-
type: 'MODEL',
|
|
853
|
-
key: 'modelName',
|
|
854
|
-
value: listItemParts.pop(),
|
|
855
|
-
};
|
|
856
|
-
}
|
|
857
|
-
else {
|
|
858
|
-
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 "); }));
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
else if (type.startsWith('PARAM') ||
|
|
862
|
-
type.startsWith('INPUT_PARAM') ||
|
|
863
|
-
type.startsWith('OUTPUT_PARAM') ||
|
|
864
|
-
listItem.startsWith('{') ||
|
|
865
|
-
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
866
|
-
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
867
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
868
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
869
|
-
}
|
|
870
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
871
|
-
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
872
|
-
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
873
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
874
|
-
}
|
|
875
|
-
var isInput = type.startsWith('INPUT');
|
|
876
|
-
var isOutput = type.startsWith('OUTPUT');
|
|
877
|
-
if (listItem.startsWith('> {')) {
|
|
878
|
-
isInput = false;
|
|
879
|
-
isOutput = false;
|
|
880
|
-
}
|
|
881
|
-
return {
|
|
882
|
-
type: 'PARAMETER',
|
|
883
|
-
parameterName: parameterName,
|
|
884
|
-
parameterDescription: parameterDescription.trim() || null,
|
|
885
|
-
isInput: isInput,
|
|
886
|
-
isOutput: isOutput,
|
|
887
|
-
};
|
|
888
|
-
}
|
|
889
|
-
else if (type.startsWith('JOKER')) {
|
|
890
|
-
if (listItemParts.length !== 2) {
|
|
891
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
892
|
-
}
|
|
893
|
-
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
894
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
895
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
896
|
-
}
|
|
897
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
898
|
-
var parameterName = parametersMatch.groups.parameterName;
|
|
899
|
-
return {
|
|
900
|
-
type: 'JOKER',
|
|
901
|
-
parameterName: parameterName,
|
|
902
|
-
};
|
|
903
|
-
}
|
|
904
|
-
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
905
|
-
if (listItemParts.length !== 2) {
|
|
906
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
907
|
-
}
|
|
908
|
-
var functionName = listItemParts.pop();
|
|
909
|
-
return {
|
|
910
|
-
type: 'POSTPROCESS',
|
|
911
|
-
functionName: functionName,
|
|
912
|
-
};
|
|
913
|
-
}
|
|
914
|
-
else if (type.startsWith('EXPECT_JSON')) {
|
|
915
|
-
return {
|
|
916
|
-
type: 'EXPECT_FORMAT',
|
|
917
|
-
format: 'JSON',
|
|
918
|
-
};
|
|
919
|
-
// [🥤]
|
|
920
|
-
}
|
|
921
|
-
else if (type.startsWith('EXPECT')) {
|
|
922
|
-
try {
|
|
923
|
-
listItemParts.shift();
|
|
924
|
-
var sign = void 0;
|
|
925
|
-
var signRaw = listItemParts.shift();
|
|
926
|
-
if (/^exact/i.test(signRaw)) {
|
|
927
|
-
sign = 'EXACTLY';
|
|
928
|
-
}
|
|
929
|
-
else if (/^min/i.test(signRaw)) {
|
|
930
|
-
sign = 'MINIMUM';
|
|
931
|
-
}
|
|
932
|
-
else if (/^max/i.test(signRaw)) {
|
|
933
|
-
sign = 'MAXIMUM';
|
|
934
|
-
}
|
|
935
|
-
else {
|
|
936
|
-
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
937
|
-
}
|
|
938
|
-
var amountRaw = listItemParts.shift();
|
|
939
|
-
var amount = parseNumber(amountRaw);
|
|
940
|
-
if (amount < 0) {
|
|
941
|
-
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
942
|
-
}
|
|
943
|
-
if (amount !== Math.floor(amount)) {
|
|
944
|
-
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
945
|
-
}
|
|
946
|
-
var unitRaw = listItemParts.shift();
|
|
947
|
-
var unit = undefined;
|
|
948
|
-
try {
|
|
949
|
-
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()) {
|
|
950
|
-
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
951
|
-
var existingUnitText = existingUnit;
|
|
952
|
-
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
953
|
-
if (existingUnitText === 'CHARACTER') {
|
|
954
|
-
existingUnitText = 'CHAR';
|
|
955
|
-
}
|
|
956
|
-
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
957
|
-
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
958
|
-
if (unit !== undefined) {
|
|
959
|
-
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
960
|
-
}
|
|
961
|
-
unit = existingUnit;
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
966
|
-
finally {
|
|
967
|
-
try {
|
|
968
|
-
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
969
|
-
}
|
|
970
|
-
finally { if (e_1) throw e_1.error; }
|
|
971
|
-
}
|
|
972
|
-
if (unit === undefined) {
|
|
973
|
-
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
974
|
-
}
|
|
975
|
-
return {
|
|
976
|
-
type: 'EXPECT_AMOUNT',
|
|
977
|
-
sign: sign,
|
|
978
|
-
unit: unit,
|
|
979
|
-
amount: amount,
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
catch (error) {
|
|
983
|
-
if (!(error instanceof Error)) {
|
|
984
|
-
throw error;
|
|
985
|
-
}
|
|
986
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
987
|
-
}
|
|
988
|
-
/*
|
|
989
|
-
} else if (type.startsWith('__________________')) {
|
|
990
|
-
// <- [🥻] Insert here when making new command
|
|
991
|
-
*/
|
|
992
|
-
}
|
|
993
|
-
else {
|
|
994
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
995
|
-
}
|
|
605
|
+
function countSentences(text) {
|
|
606
|
+
return splitIntoSentences(text).length;
|
|
996
607
|
}
|
|
997
608
|
|
|
998
609
|
var defaultDiacriticsRemovalMap = [
|
|
@@ -1247,674 +858,744 @@ function removeDiacritics(input) {
|
|
|
1247
858
|
});
|
|
1248
859
|
}
|
|
1249
860
|
|
|
1250
|
-
|
|
1251
|
-
|
|
861
|
+
/**
|
|
862
|
+
* Counts number of words in the text
|
|
863
|
+
*/
|
|
864
|
+
function countWords(text) {
|
|
865
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
866
|
+
text = removeDiacritics(text);
|
|
867
|
+
return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Index of all counter functions
|
|
872
|
+
*/
|
|
873
|
+
var CountUtils = {
|
|
874
|
+
CHARACTERS: countCharacters,
|
|
875
|
+
WORDS: countWords,
|
|
876
|
+
SENTENCES: countSentences,
|
|
877
|
+
PARAGRAPHS: countParagraphs,
|
|
878
|
+
LINES: countLines,
|
|
879
|
+
PAGES: countPages,
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
884
|
+
*
|
|
885
|
+
* Note: There are two simmilar functions:
|
|
886
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
887
|
+
* - `isPassingExpectations` which returns a boolean
|
|
888
|
+
*
|
|
889
|
+
* @throws {ExpectError} if the expectations are not met
|
|
890
|
+
* @returns {void} Nothing
|
|
891
|
+
*/
|
|
892
|
+
function checkExpectations(expectations, value) {
|
|
1252
893
|
var e_1, _a;
|
|
1253
|
-
sentence = removeDiacritics(sentence);
|
|
1254
|
-
var charType;
|
|
1255
|
-
var lastCharType = 'OTHER';
|
|
1256
|
-
var normalizedName = '';
|
|
1257
894
|
try {
|
|
1258
|
-
for (var
|
|
1259
|
-
var
|
|
1260
|
-
var
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
charType = 'UPPERCASE';
|
|
1267
|
-
normalizedChar = char.toLowerCase();
|
|
1268
|
-
}
|
|
1269
|
-
else if (/^[0-9]$/.test(char)) {
|
|
1270
|
-
charType = 'NUMBER';
|
|
1271
|
-
normalizedChar = char;
|
|
1272
|
-
}
|
|
1273
|
-
else if (/^\/$/.test(char)) {
|
|
1274
|
-
charType = 'SLASH';
|
|
1275
|
-
normalizedChar = char;
|
|
1276
|
-
}
|
|
1277
|
-
else {
|
|
1278
|
-
charType = 'OTHER';
|
|
1279
|
-
normalizedChar = '-';
|
|
895
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
896
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
897
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
898
|
+
if (min && amount < min) {
|
|
899
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
900
|
+
} /* not else */
|
|
901
|
+
if (max && amount > max) {
|
|
902
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1280
903
|
}
|
|
1281
|
-
if (charType !== lastCharType &&
|
|
1282
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
1283
|
-
!(lastCharType === 'NUMBER') &&
|
|
1284
|
-
!(charType === 'NUMBER')) {
|
|
1285
|
-
normalizedName += '-';
|
|
1286
|
-
}
|
|
1287
|
-
normalizedName += normalizedChar;
|
|
1288
|
-
lastCharType = charType;
|
|
1289
904
|
}
|
|
1290
905
|
}
|
|
1291
906
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1292
907
|
finally {
|
|
1293
908
|
try {
|
|
1294
|
-
if (
|
|
909
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1295
910
|
}
|
|
1296
911
|
finally { if (e_1) throw e_1.error; }
|
|
1297
912
|
}
|
|
1298
|
-
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
1299
|
-
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
1300
|
-
normalizedName = normalizedName.replace(/^-/, '');
|
|
1301
|
-
normalizedName = normalizedName.replace(/-$/, '');
|
|
1302
|
-
return normalizedName;
|
|
1303
913
|
}
|
|
914
|
+
/**
|
|
915
|
+
* TODO: [💝] Unite object for expecting amount and format
|
|
916
|
+
*/
|
|
1304
917
|
|
|
1305
918
|
/**
|
|
1306
|
-
*
|
|
919
|
+
* This error occurs during the parameter replacement in the template
|
|
1307
920
|
*
|
|
1308
|
-
*
|
|
1309
|
-
* @returns text without emojis
|
|
921
|
+
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
1310
922
|
*/
|
|
1311
|
-
function
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
}
|
|
923
|
+
var TemplateError = /** @class */ (function (_super) {
|
|
924
|
+
__extends(TemplateError, _super);
|
|
925
|
+
function TemplateError(message) {
|
|
926
|
+
var _this = _super.call(this, message) || this;
|
|
927
|
+
_this.name = 'TemplateError';
|
|
928
|
+
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
929
|
+
return _this;
|
|
930
|
+
}
|
|
931
|
+
return TemplateError;
|
|
932
|
+
}(Error));
|
|
1319
933
|
|
|
1320
934
|
/**
|
|
1321
|
-
*
|
|
935
|
+
* Replaces parameters in template with values from parameters object
|
|
936
|
+
*
|
|
937
|
+
* @param template the template with parameters in {curly} braces
|
|
938
|
+
* @param parameters the object with parameters
|
|
939
|
+
* @returns the template with replaced parameters
|
|
940
|
+
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
941
|
+
*
|
|
942
|
+
* @private within the createPromptbookExecutor
|
|
1322
943
|
*/
|
|
1323
|
-
function
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
944
|
+
function replaceParameters(template, parameters) {
|
|
945
|
+
var replacedTemplate = template;
|
|
946
|
+
var match;
|
|
947
|
+
var loopLimit = LOOP_LIMIT;
|
|
948
|
+
var _loop_1 = function () {
|
|
949
|
+
if (loopLimit-- < 0) {
|
|
950
|
+
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
951
|
+
}
|
|
952
|
+
var precol = match.groups.precol;
|
|
953
|
+
var parameterName = match.groups.parameterName;
|
|
954
|
+
if (parameterName === '') {
|
|
955
|
+
return "continue";
|
|
956
|
+
}
|
|
957
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
958
|
+
throw new TemplateError('Parameter is already opened or not closed');
|
|
959
|
+
}
|
|
960
|
+
if (parameters[parameterName] === undefined) {
|
|
961
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
962
|
+
}
|
|
963
|
+
var parameterValue = parameters[parameterName];
|
|
964
|
+
if (parameterValue === undefined) {
|
|
965
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
966
|
+
}
|
|
967
|
+
parameterValue = parameterValue.toString();
|
|
968
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
969
|
+
parameterValue = parameterValue
|
|
970
|
+
.split('\n')
|
|
971
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
972
|
+
.join('\n');
|
|
973
|
+
}
|
|
974
|
+
replacedTemplate =
|
|
975
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
976
|
+
parameterValue +
|
|
977
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
978
|
+
};
|
|
979
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
980
|
+
.exec(replacedTemplate))) {
|
|
981
|
+
_loop_1();
|
|
982
|
+
}
|
|
983
|
+
// [💫] Check if there are parameters that are not closed properly
|
|
984
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
985
|
+
throw new TemplateError('Parameter is not closed');
|
|
986
|
+
}
|
|
987
|
+
// [💫] Check if there are parameters that are not opened properly
|
|
988
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
989
|
+
throw new TemplateError('Parameter is not opened');
|
|
990
|
+
}
|
|
991
|
+
return replacedTemplate;
|
|
1328
992
|
}
|
|
1329
993
|
|
|
1330
994
|
/**
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
1333
|
-
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
1334
|
-
* @param options - Options and tools for the compilation
|
|
1335
|
-
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
1336
|
-
* @throws {PromptbookSyntaxError} if the promptbook string is not valid
|
|
995
|
+
* Creates executor function from promptbook and execution tools.
|
|
1337
996
|
*
|
|
1338
|
-
*
|
|
1339
|
-
*
|
|
997
|
+
* @returns The executor function
|
|
998
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1340
999
|
*/
|
|
1341
|
-
function
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1000
|
+
function createPromptbookExecutor(options) {
|
|
1001
|
+
var _this = this;
|
|
1002
|
+
var promptbook = options.promptbook, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
1003
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
|
|
1004
|
+
validatePromptbookJson(promptbook);
|
|
1005
|
+
var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1006
|
+
function executeSingleTemplate(currentTemplate) {
|
|
1007
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1008
|
+
var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
|
|
1009
|
+
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1010
|
+
var _this = this;
|
|
1011
|
+
return __generator(this, function (_o) {
|
|
1012
|
+
switch (_o.label) {
|
|
1013
|
+
case 0:
|
|
1014
|
+
name = "promptbook-executor-frame-".concat(currentTemplate.name);
|
|
1015
|
+
title = currentTemplate.title;
|
|
1016
|
+
priority = promptbook.promptTemplates.length - promptbook.promptTemplates.indexOf(currentTemplate);
|
|
1017
|
+
if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
|
|
1018
|
+
return [4 /*yield*/, onProgress({
|
|
1019
|
+
name: name,
|
|
1020
|
+
title: title,
|
|
1021
|
+
isStarted: false,
|
|
1022
|
+
isDone: false,
|
|
1023
|
+
executionType: currentTemplate.executionType,
|
|
1024
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1025
|
+
parameterValue: null,
|
|
1026
|
+
// <- [3]
|
|
1027
|
+
})];
|
|
1028
|
+
case 1:
|
|
1029
|
+
_o.sent();
|
|
1030
|
+
_o.label = 2;
|
|
1031
|
+
case 2:
|
|
1032
|
+
result = null;
|
|
1033
|
+
resultString = null;
|
|
1034
|
+
expectError = null;
|
|
1035
|
+
maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
|
|
1036
|
+
jokers = currentTemplate.jokers || [];
|
|
1037
|
+
attempt = -jokers.length;
|
|
1038
|
+
_o.label = 3;
|
|
1039
|
+
case 3:
|
|
1040
|
+
if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
|
|
1041
|
+
isJokerAttempt = attempt < 0;
|
|
1042
|
+
joker = jokers[jokers.length + attempt];
|
|
1043
|
+
if (isJokerAttempt && !joker) {
|
|
1044
|
+
throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
|
|
1045
|
+
}
|
|
1046
|
+
result = null;
|
|
1047
|
+
resultString = null;
|
|
1048
|
+
expectError = null;
|
|
1049
|
+
if (isJokerAttempt) {
|
|
1050
|
+
if (typeof parametersToPass[joker] === 'undefined') {
|
|
1051
|
+
throw new PromptbookExecutionError("Joker parameter {".concat(joker, "} not defined"));
|
|
1052
|
+
}
|
|
1053
|
+
resultString = parametersToPass[joker];
|
|
1054
|
+
}
|
|
1055
|
+
_o.label = 4;
|
|
1056
|
+
case 4:
|
|
1057
|
+
_o.trys.push([4, 45, 46, 47]);
|
|
1058
|
+
if (!!isJokerAttempt) return [3 /*break*/, 27];
|
|
1059
|
+
_a = currentTemplate.executionType;
|
|
1060
|
+
switch (_a) {
|
|
1061
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
|
|
1062
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
|
|
1063
|
+
case 'SCRIPT': return [3 /*break*/, 13];
|
|
1064
|
+
case 'PROMPT_DIALOG': return [3 /*break*/, 24];
|
|
1065
|
+
}
|
|
1066
|
+
return [3 /*break*/, 26];
|
|
1067
|
+
case 5:
|
|
1068
|
+
resultString = replaceParameters(currentTemplate.content, parametersToPass);
|
|
1069
|
+
return [3 /*break*/, 27];
|
|
1070
|
+
case 6:
|
|
1071
|
+
prompt = {
|
|
1072
|
+
title: currentTemplate.title,
|
|
1073
|
+
promptbookUrl: "".concat(promptbook.promptbookUrl
|
|
1074
|
+
? promptbook.promptbookUrl
|
|
1075
|
+
: 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
1076
|
+
parameters: parametersToPass,
|
|
1077
|
+
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1078
|
+
modelRequirements: currentTemplate.modelRequirements,
|
|
1079
|
+
expectations: currentTemplate.expectations,
|
|
1080
|
+
expectFormat: currentTemplate.expectFormat,
|
|
1081
|
+
postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
1082
|
+
var errors, _a, _b, scriptTools, error_5, e_5_1;
|
|
1083
|
+
var e_5, _c;
|
|
1084
|
+
return __generator(this, function (_d) {
|
|
1085
|
+
switch (_d.label) {
|
|
1086
|
+
case 0:
|
|
1087
|
+
errors = [];
|
|
1088
|
+
_d.label = 1;
|
|
1089
|
+
case 1:
|
|
1090
|
+
_d.trys.push([1, 8, 9, 10]);
|
|
1091
|
+
_a = __values(tools.script), _b = _a.next();
|
|
1092
|
+
_d.label = 2;
|
|
1093
|
+
case 2:
|
|
1094
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
1095
|
+
scriptTools = _b.value;
|
|
1096
|
+
_d.label = 3;
|
|
1097
|
+
case 3:
|
|
1098
|
+
_d.trys.push([3, 5, , 6]);
|
|
1099
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1100
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1101
|
+
script: "".concat(functionName, "(result)"),
|
|
1102
|
+
parameters: {
|
|
1103
|
+
result: result || '',
|
|
1104
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1105
|
+
},
|
|
1106
|
+
})];
|
|
1107
|
+
case 4: return [2 /*return*/, _d.sent()];
|
|
1108
|
+
case 5:
|
|
1109
|
+
error_5 = _d.sent();
|
|
1110
|
+
if (!(error_5 instanceof Error)) {
|
|
1111
|
+
throw error_5;
|
|
1112
|
+
}
|
|
1113
|
+
errors.push(error_5);
|
|
1114
|
+
return [3 /*break*/, 6];
|
|
1115
|
+
case 6:
|
|
1116
|
+
_b = _a.next();
|
|
1117
|
+
return [3 /*break*/, 2];
|
|
1118
|
+
case 7: return [3 /*break*/, 10];
|
|
1119
|
+
case 8:
|
|
1120
|
+
e_5_1 = _d.sent();
|
|
1121
|
+
e_5 = { error: e_5_1 };
|
|
1122
|
+
return [3 /*break*/, 10];
|
|
1123
|
+
case 9:
|
|
1124
|
+
try {
|
|
1125
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1126
|
+
}
|
|
1127
|
+
finally { if (e_5) throw e_5.error; }
|
|
1128
|
+
return [7 /*endfinally*/];
|
|
1129
|
+
case 10:
|
|
1130
|
+
if (errors.length === 0) {
|
|
1131
|
+
throw new PromptbookExecutionError('Postprocessing in LlmExecutionTools failed because no ScriptExecutionTools were provided');
|
|
1132
|
+
}
|
|
1133
|
+
else if (errors.length === 1) {
|
|
1134
|
+
throw errors[0];
|
|
1135
|
+
}
|
|
1136
|
+
else {
|
|
1137
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}); }; }),
|
|
1142
|
+
};
|
|
1143
|
+
_b = currentTemplate.modelRequirements.modelVariant;
|
|
1144
|
+
switch (_b) {
|
|
1145
|
+
case 'CHAT': return [3 /*break*/, 7];
|
|
1146
|
+
case 'COMPLETION': return [3 /*break*/, 9];
|
|
1147
|
+
}
|
|
1148
|
+
return [3 /*break*/, 11];
|
|
1149
|
+
case 7: return [4 /*yield*/, tools.llm.gptChat(prompt)];
|
|
1150
|
+
case 8:
|
|
1151
|
+
chatThread = _o.sent();
|
|
1152
|
+
// TODO: [🍬] Destroy chatThread
|
|
1153
|
+
result = chatThread;
|
|
1154
|
+
resultString = chatThread.content;
|
|
1155
|
+
return [3 /*break*/, 12];
|
|
1156
|
+
case 9: return [4 /*yield*/, tools.llm.gptComplete(prompt)];
|
|
1157
|
+
case 10:
|
|
1158
|
+
completionResult = _o.sent();
|
|
1159
|
+
result = completionResult;
|
|
1160
|
+
resultString = completionResult.content;
|
|
1161
|
+
return [3 /*break*/, 12];
|
|
1162
|
+
case 11: throw new PromptbookExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
|
|
1163
|
+
case 12: return [3 /*break*/, 27];
|
|
1164
|
+
case 13:
|
|
1165
|
+
if (tools.script.length === 0) {
|
|
1166
|
+
throw new PromptbookExecutionError('No script execution tools are available');
|
|
1167
|
+
}
|
|
1168
|
+
if (!currentTemplate.contentLanguage) {
|
|
1169
|
+
throw new PromptbookExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
|
|
1170
|
+
}
|
|
1171
|
+
// TODO: DRY [1]
|
|
1172
|
+
scriptExecutionErrors = [];
|
|
1173
|
+
_o.label = 14;
|
|
1174
|
+
case 14:
|
|
1175
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
1176
|
+
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
1177
|
+
_o.label = 15;
|
|
1178
|
+
case 15:
|
|
1179
|
+
if (!!_d.done) return [3 /*break*/, 20];
|
|
1180
|
+
scriptTools = _d.value;
|
|
1181
|
+
_o.label = 16;
|
|
1182
|
+
case 16:
|
|
1183
|
+
_o.trys.push([16, 18, , 19]);
|
|
1184
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1185
|
+
scriptLanguage: currentTemplate.contentLanguage,
|
|
1186
|
+
script: currentTemplate.content,
|
|
1187
|
+
parameters: parametersToPass,
|
|
1188
|
+
})];
|
|
1189
|
+
case 17:
|
|
1190
|
+
resultString = _o.sent();
|
|
1191
|
+
return [3 /*break*/, 20];
|
|
1192
|
+
case 18:
|
|
1193
|
+
error_2 = _o.sent();
|
|
1194
|
+
if (!(error_2 instanceof Error)) {
|
|
1195
|
+
throw error_2;
|
|
1196
|
+
}
|
|
1197
|
+
scriptExecutionErrors.push(error_2);
|
|
1198
|
+
return [3 /*break*/, 19];
|
|
1199
|
+
case 19:
|
|
1200
|
+
_d = _c.next();
|
|
1201
|
+
return [3 /*break*/, 15];
|
|
1202
|
+
case 20: return [3 /*break*/, 23];
|
|
1203
|
+
case 21:
|
|
1204
|
+
e_2_1 = _o.sent();
|
|
1205
|
+
e_2 = { error: e_2_1 };
|
|
1206
|
+
return [3 /*break*/, 23];
|
|
1207
|
+
case 22:
|
|
1208
|
+
try {
|
|
1209
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
1210
|
+
}
|
|
1211
|
+
finally { if (e_2) throw e_2.error; }
|
|
1212
|
+
return [7 /*endfinally*/];
|
|
1213
|
+
case 23:
|
|
1214
|
+
if (resultString !== null) {
|
|
1215
|
+
return [3 /*break*/, 27];
|
|
1216
|
+
}
|
|
1217
|
+
if (scriptExecutionErrors.length === 1) {
|
|
1218
|
+
throw scriptExecutionErrors[0];
|
|
1219
|
+
}
|
|
1220
|
+
else {
|
|
1221
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
|
|
1222
|
+
.map(function (error) { return '- ' + error.message; })
|
|
1223
|
+
.join('\n\n')), "\n "); }));
|
|
1224
|
+
}
|
|
1225
|
+
case 24:
|
|
1226
|
+
if (tools.userInterface === undefined) {
|
|
1227
|
+
throw new PromptbookExecutionError('User interface tools are not available');
|
|
1228
|
+
}
|
|
1229
|
+
return [4 /*yield*/, tools.userInterface.promptDialog({
|
|
1230
|
+
promptTitle: currentTemplate.title,
|
|
1231
|
+
promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
|
|
1232
|
+
defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
|
|
1233
|
+
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
1234
|
+
placeholder: undefined,
|
|
1235
|
+
priority: priority,
|
|
1236
|
+
})];
|
|
1237
|
+
case 25:
|
|
1238
|
+
// TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
1239
|
+
resultString = _o.sent();
|
|
1240
|
+
return [3 /*break*/, 27];
|
|
1241
|
+
case 26: throw new PromptbookExecutionError(
|
|
1351
1242
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1243
|
+
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
1244
|
+
case 27:
|
|
1245
|
+
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
1246
|
+
_o.label = 28;
|
|
1247
|
+
case 28:
|
|
1248
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
1249
|
+
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
1250
|
+
_o.label = 29;
|
|
1251
|
+
case 29:
|
|
1252
|
+
if (!!_f.done) return [3 /*break*/, 41];
|
|
1253
|
+
functionName = _f.value;
|
|
1254
|
+
// TODO: DRY [1]
|
|
1255
|
+
scriptExecutionErrors = [];
|
|
1256
|
+
postprocessingError = null;
|
|
1257
|
+
_o.label = 30;
|
|
1258
|
+
case 30:
|
|
1259
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
1260
|
+
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
1261
|
+
_o.label = 31;
|
|
1262
|
+
case 31:
|
|
1263
|
+
if (!!_h.done) return [3 /*break*/, 36];
|
|
1264
|
+
scriptTools = _h.value;
|
|
1265
|
+
_o.label = 32;
|
|
1266
|
+
case 32:
|
|
1267
|
+
_o.trys.push([32, 34, , 35]);
|
|
1268
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1269
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1270
|
+
script: "".concat(functionName, "(resultString)"),
|
|
1271
|
+
parameters: {
|
|
1272
|
+
resultString: resultString || '',
|
|
1273
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1274
|
+
},
|
|
1275
|
+
})];
|
|
1276
|
+
case 33:
|
|
1277
|
+
resultString = _o.sent();
|
|
1278
|
+
postprocessingError = null;
|
|
1279
|
+
return [3 /*break*/, 36];
|
|
1280
|
+
case 34:
|
|
1281
|
+
error_3 = _o.sent();
|
|
1282
|
+
if (!(error_3 instanceof Error)) {
|
|
1283
|
+
throw error_3;
|
|
1284
|
+
}
|
|
1285
|
+
postprocessingError = error_3;
|
|
1286
|
+
scriptExecutionErrors.push(error_3);
|
|
1287
|
+
return [3 /*break*/, 35];
|
|
1288
|
+
case 35:
|
|
1289
|
+
_h = _g.next();
|
|
1290
|
+
return [3 /*break*/, 31];
|
|
1291
|
+
case 36: return [3 /*break*/, 39];
|
|
1292
|
+
case 37:
|
|
1293
|
+
e_3_1 = _o.sent();
|
|
1294
|
+
e_3 = { error: e_3_1 };
|
|
1295
|
+
return [3 /*break*/, 39];
|
|
1296
|
+
case 38:
|
|
1297
|
+
try {
|
|
1298
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
1299
|
+
}
|
|
1300
|
+
finally { if (e_3) throw e_3.error; }
|
|
1301
|
+
return [7 /*endfinally*/];
|
|
1302
|
+
case 39:
|
|
1303
|
+
if (postprocessingError) {
|
|
1304
|
+
throw postprocessingError;
|
|
1305
|
+
}
|
|
1306
|
+
_o.label = 40;
|
|
1307
|
+
case 40:
|
|
1308
|
+
_f = _e.next();
|
|
1309
|
+
return [3 /*break*/, 29];
|
|
1310
|
+
case 41: return [3 /*break*/, 44];
|
|
1311
|
+
case 42:
|
|
1312
|
+
e_4_1 = _o.sent();
|
|
1313
|
+
e_4 = { error: e_4_1 };
|
|
1314
|
+
return [3 /*break*/, 44];
|
|
1315
|
+
case 43:
|
|
1316
|
+
try {
|
|
1317
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
1318
|
+
}
|
|
1319
|
+
finally { if (e_4) throw e_4.error; }
|
|
1320
|
+
return [7 /*endfinally*/];
|
|
1321
|
+
case 44:
|
|
1322
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1323
|
+
if (currentTemplate.expectFormat) {
|
|
1324
|
+
if (currentTemplate.expectFormat === 'JSON') {
|
|
1325
|
+
if (!isValidJsonString(resultString || '')) {
|
|
1326
|
+
throw new ExpectError('Expected valid JSON string');
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1331
|
+
if (currentTemplate.expectations) {
|
|
1332
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
1333
|
+
}
|
|
1334
|
+
return [3 /*break*/, 49];
|
|
1335
|
+
case 45:
|
|
1336
|
+
error_4 = _o.sent();
|
|
1337
|
+
if (!(error_4 instanceof ExpectError)) {
|
|
1338
|
+
throw error_4;
|
|
1339
|
+
}
|
|
1340
|
+
expectError = error_4;
|
|
1341
|
+
return [3 /*break*/, 47];
|
|
1342
|
+
case 46:
|
|
1343
|
+
if (!isJokerAttempt &&
|
|
1344
|
+
currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
|
|
1345
|
+
prompt
|
|
1346
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
1347
|
+
// In that case we don’t want to make a report about it because it’s not a llm execution error
|
|
1348
|
+
) {
|
|
1349
|
+
// TODO: [🧠] Maybe put other executionTypes into report
|
|
1350
|
+
executionReport.promptExecutions.push({
|
|
1351
|
+
prompt: {
|
|
1352
|
+
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
1353
|
+
content: prompt.content,
|
|
1354
|
+
modelRequirements: prompt.modelRequirements,
|
|
1355
|
+
expectations: prompt.expectations,
|
|
1356
|
+
expectFormat: prompt.expectFormat,
|
|
1357
|
+
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1358
|
+
},
|
|
1359
|
+
result: result || undefined,
|
|
1360
|
+
error: expectError || undefined,
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
return [7 /*endfinally*/];
|
|
1364
|
+
case 47:
|
|
1365
|
+
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
1366
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
|
|
1367
|
+
}
|
|
1368
|
+
_o.label = 48;
|
|
1369
|
+
case 48:
|
|
1370
|
+
attempt++;
|
|
1371
|
+
return [3 /*break*/, 3];
|
|
1372
|
+
case 49:
|
|
1373
|
+
if (resultString === null) {
|
|
1374
|
+
throw new UnexpectedError('Something went wrong and prompt result is null');
|
|
1375
|
+
}
|
|
1376
|
+
if (onProgress /* <- [3] */) {
|
|
1377
|
+
onProgress({
|
|
1378
|
+
name: name,
|
|
1379
|
+
title: title,
|
|
1380
|
+
isStarted: true,
|
|
1381
|
+
isDone: true,
|
|
1382
|
+
executionType: currentTemplate.executionType,
|
|
1383
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1384
|
+
parameterValue: resultString,
|
|
1385
|
+
// <- [3]
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because Promptbook checks logic consistency during construction */, _m));
|
|
1389
|
+
return [2 /*return*/];
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
|
|
1395
|
+
var e_1, _c;
|
|
1396
|
+
return __generator(this, function (_d) {
|
|
1397
|
+
switch (_d.label) {
|
|
1398
|
+
case 0:
|
|
1399
|
+
parametersToPass = inputParameters;
|
|
1400
|
+
executionReport = {
|
|
1401
|
+
promptbookUrl: promptbook.promptbookUrl,
|
|
1402
|
+
title: promptbook.title,
|
|
1403
|
+
promptbookUsedVersion: PROMPTBOOK_VERSION,
|
|
1404
|
+
promptbookRequestedVersion: promptbook.promptbookVersion,
|
|
1405
|
+
description: promptbook.description,
|
|
1406
|
+
promptExecutions: [],
|
|
1359
1407
|
};
|
|
1360
|
-
|
|
1361
|
-
return [4 /*yield*/, prepareKnowledgeFromMarkdown()];
|
|
1408
|
+
_d.label = 1;
|
|
1362
1409
|
case 1:
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1410
|
+
_d.trys.push([1, 6, , 7]);
|
|
1411
|
+
resovedParameters_1 = promptbook.parameters
|
|
1412
|
+
.filter(function (_a) {
|
|
1413
|
+
var isInput = _a.isInput;
|
|
1414
|
+
return isInput;
|
|
1415
|
+
})
|
|
1416
|
+
.map(function (_a) {
|
|
1417
|
+
var name = _a.name;
|
|
1418
|
+
return name;
|
|
1419
|
+
});
|
|
1420
|
+
unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
1421
|
+
resolving_1 = [];
|
|
1422
|
+
loopLimit = LOOP_LIMIT;
|
|
1423
|
+
_loop_1 = function () {
|
|
1424
|
+
var currentTemplate, work_1;
|
|
1425
|
+
return __generator(this, function (_e) {
|
|
1426
|
+
switch (_e.label) {
|
|
1427
|
+
case 0:
|
|
1428
|
+
if (loopLimit-- < 0) {
|
|
1429
|
+
throw new UnexpectedError('Loop limit reached during resolving parameters promptbook execution');
|
|
1430
|
+
}
|
|
1431
|
+
currentTemplate = unresovedTemplates.find(function (template) {
|
|
1432
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
|
|
1433
|
+
});
|
|
1434
|
+
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
1435
|
+
throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
|
|
1436
|
+
case 1:
|
|
1437
|
+
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
1438
|
+
/* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
1439
|
+
case 2:
|
|
1440
|
+
/* [5] */ _e.sent();
|
|
1441
|
+
return [3 /*break*/, 4];
|
|
1442
|
+
case 3:
|
|
1443
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
|
|
1444
|
+
work_1 = executeSingleTemplate(currentTemplate)
|
|
1445
|
+
.then(function () {
|
|
1446
|
+
resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
|
|
1447
|
+
})
|
|
1448
|
+
.then(function () {
|
|
1449
|
+
resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
|
|
1450
|
+
});
|
|
1451
|
+
resolving_1.push(work_1);
|
|
1452
|
+
_e.label = 4;
|
|
1453
|
+
case 4: return [2 /*return*/];
|
|
1384
1454
|
}
|
|
1385
|
-
}
|
|
1386
|
-
else {
|
|
1387
|
-
promptbookJson.parameters.push({
|
|
1388
|
-
name: parameterName,
|
|
1389
|
-
description: parameterDescription || undefined,
|
|
1390
|
-
isInput: isInput,
|
|
1391
|
-
isOutput: isOutput,
|
|
1392
|
-
});
|
|
1393
|
-
}
|
|
1455
|
+
});
|
|
1394
1456
|
};
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
if (
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1457
|
+
_d.label = 2;
|
|
1458
|
+
case 2:
|
|
1459
|
+
if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
|
|
1460
|
+
return [5 /*yield**/, _loop_1()];
|
|
1461
|
+
case 3:
|
|
1462
|
+
_d.sent();
|
|
1463
|
+
return [3 /*break*/, 2];
|
|
1464
|
+
case 4: return [4 /*yield*/, Promise.all(resolving_1)];
|
|
1465
|
+
case 5:
|
|
1466
|
+
_d.sent();
|
|
1467
|
+
return [3 /*break*/, 7];
|
|
1468
|
+
case 6:
|
|
1469
|
+
error_1 = _d.sent();
|
|
1470
|
+
if (!(error_1 instanceof Error)) {
|
|
1471
|
+
throw error_1;
|
|
1409
1472
|
}
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1473
|
+
usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1474
|
+
var result = _a.result;
|
|
1475
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1476
|
+
})), false));
|
|
1477
|
+
return [2 /*return*/, {
|
|
1478
|
+
isSuccessful: false,
|
|
1479
|
+
errors: [error_1],
|
|
1480
|
+
usage: usage_1,
|
|
1481
|
+
executionReport: executionReport,
|
|
1482
|
+
outputParameters: parametersToPass,
|
|
1483
|
+
}];
|
|
1484
|
+
case 7:
|
|
1413
1485
|
try {
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
1420
|
-
break;
|
|
1421
|
-
case 'PROMPTBOOK_VERSION':
|
|
1422
|
-
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
1423
|
-
break;
|
|
1424
|
-
case 'MODEL':
|
|
1425
|
-
defaultModelRequirements[command.key] = command.value;
|
|
1426
|
-
break;
|
|
1427
|
-
case 'PARAMETER':
|
|
1428
|
-
addParam(command);
|
|
1429
|
-
break;
|
|
1430
|
-
default:
|
|
1431
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
1486
|
+
// Note: Filter ONLY output parameters
|
|
1487
|
+
for (_a = __values(promptbook.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1488
|
+
parameter = _b.value;
|
|
1489
|
+
if (parameter.isOutput) {
|
|
1490
|
+
continue;
|
|
1432
1491
|
}
|
|
1492
|
+
delete parametersToPass[parameter.name];
|
|
1433
1493
|
}
|
|
1434
1494
|
}
|
|
1435
1495
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1436
1496
|
finally {
|
|
1437
1497
|
try {
|
|
1438
|
-
if (
|
|
1498
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1439
1499
|
}
|
|
1440
1500
|
finally { if (e_1) throw e_1.error; }
|
|
1441
1501
|
}
|
|
1442
|
-
|
|
1443
|
-
var
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
var isExecutionTypeChanged = false;
|
|
1454
|
-
try {
|
|
1455
|
-
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()) {
|
|
1456
|
-
var listItem = listItems_2_1.value;
|
|
1457
|
-
var command = parseCommand(listItem);
|
|
1458
|
-
switch (command.type) {
|
|
1459
|
-
case 'JOKER':
|
|
1460
|
-
jokers.push(command.parameterName);
|
|
1461
|
-
dependentParameterNames.add(command.parameterName);
|
|
1462
|
-
break;
|
|
1463
|
-
case 'EXECUTE':
|
|
1464
|
-
if (isExecutionTypeChanged) {
|
|
1465
|
-
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
1466
|
-
}
|
|
1467
|
-
executionType = command.executionType;
|
|
1468
|
-
isExecutionTypeChanged = true;
|
|
1469
|
-
break;
|
|
1470
|
-
case 'MODEL':
|
|
1471
|
-
templateModelRequirements[command.key] = command.value;
|
|
1472
|
-
break;
|
|
1473
|
-
case 'PARAMETER':
|
|
1474
|
-
// Note: This is just for detecting resulitng parameter name
|
|
1475
|
-
addParam(command);
|
|
1476
|
-
break;
|
|
1477
|
-
case 'POSTPROCESS':
|
|
1478
|
-
postprocessing.push(command.functionName);
|
|
1479
|
-
break;
|
|
1480
|
-
case 'EXPECT_AMOUNT':
|
|
1481
|
-
// eslint-disable-next-line no-case-declarations
|
|
1482
|
-
var unit = command.unit.toLowerCase();
|
|
1483
|
-
expectAmount[unit] = expectAmount[unit] || {};
|
|
1484
|
-
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
1485
|
-
if (expectAmount[unit].min !== undefined) {
|
|
1486
|
-
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
1487
|
-
}
|
|
1488
|
-
expectAmount[unit].min = command.amount;
|
|
1489
|
-
} /* not else */
|
|
1490
|
-
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
1491
|
-
if (expectAmount[unit].max !== undefined) {
|
|
1492
|
-
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
1493
|
-
}
|
|
1494
|
-
expectAmount[unit].max = command.amount;
|
|
1495
|
-
}
|
|
1496
|
-
break;
|
|
1497
|
-
case 'EXPECT_FORMAT':
|
|
1498
|
-
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
1499
|
-
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
1500
|
-
}
|
|
1501
|
-
expectFormat = command.format;
|
|
1502
|
-
break;
|
|
1503
|
-
default:
|
|
1504
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
}
|
|
1508
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1509
|
-
finally {
|
|
1510
|
-
try {
|
|
1511
|
-
if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
|
|
1512
|
-
}
|
|
1513
|
-
finally { if (e_3) throw e_3.error; }
|
|
1514
|
-
}
|
|
1515
|
-
var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
|
|
1516
|
-
if (executionType === 'SCRIPT') {
|
|
1517
|
-
if (!language) {
|
|
1518
|
-
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
1519
|
-
}
|
|
1520
|
-
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
1521
|
-
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 "); }));
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
var lastLine = section.content.split('\n').pop();
|
|
1525
|
-
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
1526
|
-
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
1527
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
1528
|
-
// TODO: Show code of invalid sections each time + DRY
|
|
1529
|
-
section.content
|
|
1530
|
-
.split('\n')
|
|
1531
|
-
.map(function (line) { return "> ".concat(line); })
|
|
1532
|
-
.join('\n')), "\n "); }));
|
|
1533
|
-
}
|
|
1534
|
-
var resultingParameterName = match.groups.resultingParamName;
|
|
1535
|
-
// TODO: [1] DRY description
|
|
1536
|
-
var description_1 = section.content;
|
|
1537
|
-
// Note: Remove codeblocks
|
|
1538
|
-
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
1539
|
-
//Note: Remove lists and return statement
|
|
1540
|
-
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1541
|
-
description_1 = spaceTrim(description_1);
|
|
1542
|
-
if (description_1 === '') {
|
|
1543
|
-
description_1 = undefined;
|
|
1544
|
-
}
|
|
1545
|
-
if (Object.keys(jokers).length === 0) {
|
|
1546
|
-
jokers = undefined;
|
|
1547
|
-
}
|
|
1548
|
-
if (Object.keys(expectAmount).length === 0) {
|
|
1549
|
-
expectAmount = undefined;
|
|
1550
|
-
}
|
|
1551
|
-
if (Object.keys(postprocessing).length === 0) {
|
|
1552
|
-
postprocessing = undefined;
|
|
1553
|
-
}
|
|
1554
|
-
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
1555
|
-
if (templateModelRequirements.modelVariant === undefined) {
|
|
1556
|
-
templateModelRequirements.modelVariant = 'CHAT';
|
|
1557
|
-
}
|
|
1558
|
-
promptbookJson.promptTemplates.push({
|
|
1559
|
-
name: titleToName(section.title),
|
|
1560
|
-
title: section.title,
|
|
1561
|
-
description: description_1,
|
|
1562
|
-
dependentParameterNames: Array.from(dependentParameterNames),
|
|
1563
|
-
executionType: executionType,
|
|
1564
|
-
jokers: jokers,
|
|
1565
|
-
postprocessing: postprocessing,
|
|
1566
|
-
expectations: expectAmount,
|
|
1567
|
-
expectFormat: expectFormat,
|
|
1568
|
-
modelRequirements: templateModelRequirements,
|
|
1569
|
-
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
1570
|
-
content: content,
|
|
1571
|
-
resultingParameterName: resultingParameterName,
|
|
1572
|
-
});
|
|
1573
|
-
};
|
|
1574
|
-
try {
|
|
1575
|
-
for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1576
|
-
section = _b.value;
|
|
1577
|
-
_loop_1(section);
|
|
1578
|
-
}
|
|
1579
|
-
}
|
|
1580
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1581
|
-
finally {
|
|
1582
|
-
try {
|
|
1583
|
-
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
1584
|
-
}
|
|
1585
|
-
finally { if (e_2) throw e_2.error; }
|
|
1586
|
-
}
|
|
1587
|
-
// =============================================================
|
|
1588
|
-
return [2 /*return*/, promptbookJson];
|
|
1502
|
+
usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1503
|
+
var result = _a.result;
|
|
1504
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1505
|
+
})), false));
|
|
1506
|
+
return [2 /*return*/, {
|
|
1507
|
+
isSuccessful: true,
|
|
1508
|
+
errors: [],
|
|
1509
|
+
usage: usage,
|
|
1510
|
+
executionReport: executionReport,
|
|
1511
|
+
outputParameters: parametersToPass,
|
|
1512
|
+
}];
|
|
1589
1513
|
}
|
|
1590
1514
|
});
|
|
1591
|
-
});
|
|
1515
|
+
}); };
|
|
1516
|
+
return promptbookExecutor;
|
|
1592
1517
|
}
|
|
1593
1518
|
/**
|
|
1594
|
-
* TODO:
|
|
1595
|
-
* TODO:
|
|
1596
|
-
*
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
/**
|
|
1600
|
-
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
1519
|
+
* TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
|
|
1520
|
+
* TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
|
|
1521
|
+
* Note: CreatePromptbookExecutorOptions are just connected to PromptbookExecutor so do not extract to types folder
|
|
1522
|
+
* TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
|
|
1601
1523
|
*/
|
|
1602
|
-
var PromptbookLogicError = /** @class */ (function (_super) {
|
|
1603
|
-
__extends(PromptbookLogicError, _super);
|
|
1604
|
-
function PromptbookLogicError(message) {
|
|
1605
|
-
var _this = _super.call(this, message) || this;
|
|
1606
|
-
_this.name = 'PromptbookLogicError';
|
|
1607
|
-
Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
|
|
1608
|
-
return _this;
|
|
1609
|
-
}
|
|
1610
|
-
return PromptbookLogicError;
|
|
1611
|
-
}(Error));
|
|
1612
1524
|
|
|
1613
1525
|
/**
|
|
1614
|
-
*
|
|
1526
|
+
* Prettify the html code
|
|
1615
1527
|
*
|
|
1616
|
-
*
|
|
1528
|
+
* @param content raw html code
|
|
1529
|
+
* @returns formatted html code
|
|
1617
1530
|
*/
|
|
1618
|
-
function
|
|
1619
|
-
if (typeof url !== 'string') {
|
|
1620
|
-
return false;
|
|
1621
|
-
}
|
|
1531
|
+
function prettifyMarkdown(content) {
|
|
1622
1532
|
try {
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1533
|
+
return format(content, {
|
|
1534
|
+
parser: 'markdown',
|
|
1535
|
+
plugins: [parserHtml],
|
|
1536
|
+
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
1537
|
+
endOfLine: 'lf',
|
|
1538
|
+
tabWidth: 4,
|
|
1539
|
+
singleQuote: true,
|
|
1540
|
+
trailingComma: 'all',
|
|
1541
|
+
arrowParens: 'always',
|
|
1542
|
+
printWidth: 120,
|
|
1543
|
+
htmlWhitespaceSensitivity: 'ignore',
|
|
1544
|
+
jsxBracketSameLine: false,
|
|
1545
|
+
bracketSpacing: true,
|
|
1546
|
+
});
|
|
1632
1547
|
}
|
|
1633
1548
|
catch (error) {
|
|
1634
|
-
|
|
1549
|
+
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
1550
|
+
error: error,
|
|
1551
|
+
html: content,
|
|
1552
|
+
});
|
|
1553
|
+
return content;
|
|
1635
1554
|
}
|
|
1636
1555
|
}
|
|
1637
1556
|
|
|
1638
1557
|
/**
|
|
1639
|
-
*
|
|
1640
|
-
*
|
|
1641
|
-
* It checks:
|
|
1642
|
-
* - if it has correct parameters dependency
|
|
1558
|
+
* Makes first letter of a string uppercase
|
|
1643
1559
|
*
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1560
|
+
*/
|
|
1561
|
+
function capitalize(word) {
|
|
1562
|
+
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
/**
|
|
1566
|
+
* Converts promptbook in JSON format to string format
|
|
1647
1567
|
*
|
|
1648
|
-
* @param
|
|
1649
|
-
* @returns
|
|
1650
|
-
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1568
|
+
* @param promptbookJson Promptbook in JSON format (.ptbk.json)
|
|
1569
|
+
* @returns Promptbook in string format (.ptbk.md)
|
|
1651
1570
|
*/
|
|
1652
|
-
function
|
|
1653
|
-
|
|
1654
|
-
var
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
1662
|
-
if (!Array.isArray(promptbook.parameters)) {
|
|
1663
|
-
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
1664
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
1571
|
+
function promptbookJsonToString(promptbookJson) {
|
|
1572
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
|
|
1573
|
+
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1574
|
+
var promptbookString = "# ".concat(title);
|
|
1575
|
+
if (description) {
|
|
1576
|
+
promptbookString += '\n\n';
|
|
1577
|
+
promptbookString += description;
|
|
1665
1578
|
}
|
|
1666
|
-
// TODO
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1579
|
+
// TODO:> const commands: Array<Command>
|
|
1580
|
+
var commands = [];
|
|
1581
|
+
if (promptbookUrl) {
|
|
1582
|
+
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1670
1583
|
}
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
1674
|
-
}
|
|
1675
|
-
// Note: Testing that parameter is either intermediate or output BUT not created and unused
|
|
1676
|
-
if (!parameter.isInput &&
|
|
1677
|
-
!parameter.isOutput &&
|
|
1678
|
-
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
1679
|
-
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
1680
|
-
}
|
|
1681
|
-
// Note: Testing that parameter is either input or result of some template
|
|
1682
|
-
if (!parameter.isInput &&
|
|
1683
|
-
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
1684
|
-
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
1685
|
-
}
|
|
1686
|
-
};
|
|
1584
|
+
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1585
|
+
promptbookString = prettifyMarkdown(promptbookString);
|
|
1687
1586
|
try {
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1587
|
+
for (var _g = __values(parameters.filter(function (_a) {
|
|
1588
|
+
var isInput = _a.isInput;
|
|
1589
|
+
return isInput;
|
|
1590
|
+
})), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1591
|
+
var parameter = _h.value;
|
|
1592
|
+
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1692
1593
|
}
|
|
1693
1594
|
}
|
|
1694
1595
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1695
1596
|
finally {
|
|
1696
1597
|
try {
|
|
1697
|
-
if (
|
|
1698
|
-
}
|
|
1699
|
-
finally { if (e_1) throw e_1.error; }
|
|
1700
|
-
}
|
|
1701
|
-
// Note: Check each template individually
|
|
1702
|
-
var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
|
|
1703
|
-
var isInput = _a.isInput;
|
|
1704
|
-
return isInput;
|
|
1705
|
-
}).map(function (_a) {
|
|
1706
|
-
var name = _a.name;
|
|
1707
|
-
return name;
|
|
1708
|
-
}));
|
|
1709
|
-
try {
|
|
1710
|
-
for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1711
|
-
var template = _h.value;
|
|
1712
|
-
if (definedParameters.has(template.resultingParameterName)) {
|
|
1713
|
-
throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
|
|
1714
|
-
}
|
|
1715
|
-
definedParameters.add(template.resultingParameterName);
|
|
1716
|
-
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
1717
|
-
(template.modelRequirements.modelVariant === undefined)) {
|
|
1718
|
-
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
1719
|
-
}
|
|
1720
|
-
if (template.jokers && template.jokers.length > 0) {
|
|
1721
|
-
if (!template.expectFormat &&
|
|
1722
|
-
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
1723
|
-
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
1724
|
-
}
|
|
1725
|
-
try {
|
|
1726
|
-
for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
1727
|
-
var joker = _k.value;
|
|
1728
|
-
if (!template.dependentParameterNames.includes(joker)) {
|
|
1729
|
-
throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
|
|
1730
|
-
}
|
|
1731
|
-
}
|
|
1732
|
-
}
|
|
1733
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1734
|
-
finally {
|
|
1735
|
-
try {
|
|
1736
|
-
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
1737
|
-
}
|
|
1738
|
-
finally { if (e_3) throw e_3.error; }
|
|
1739
|
-
}
|
|
1740
|
-
}
|
|
1741
|
-
if (template.expectations) {
|
|
1742
|
-
try {
|
|
1743
|
-
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1744
|
-
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1745
|
-
if (min !== undefined && max !== undefined && min > max) {
|
|
1746
|
-
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
1747
|
-
}
|
|
1748
|
-
if (min !== undefined && min < 0) {
|
|
1749
|
-
throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
|
|
1750
|
-
}
|
|
1751
|
-
if (max !== undefined && max <= 0) {
|
|
1752
|
-
throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1757
|
-
finally {
|
|
1758
|
-
try {
|
|
1759
|
-
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
1760
|
-
}
|
|
1761
|
-
finally { if (e_4) throw e_4.error; }
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
}
|
|
1765
|
-
}
|
|
1766
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1767
|
-
finally {
|
|
1768
|
-
try {
|
|
1769
|
-
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
1770
|
-
}
|
|
1771
|
-
finally { if (e_2) throw e_2.error; }
|
|
1772
|
-
}
|
|
1773
|
-
// Note: Detect circular dependencies
|
|
1774
|
-
var resovedParameters = promptbook.parameters
|
|
1775
|
-
.filter(function (_a) {
|
|
1776
|
-
var isInput = _a.isInput;
|
|
1777
|
-
return isInput;
|
|
1778
|
-
})
|
|
1779
|
-
.map(function (_a) {
|
|
1780
|
-
var name = _a.name;
|
|
1781
|
-
return name;
|
|
1782
|
-
});
|
|
1783
|
-
var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
1784
|
-
var loopLimit = LOOP_LIMIT;
|
|
1785
|
-
var _loop_2 = function () {
|
|
1786
|
-
if (loopLimit-- < 0) {
|
|
1787
|
-
throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
|
|
1788
|
-
}
|
|
1789
|
-
var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
|
|
1790
|
-
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
1791
|
-
});
|
|
1792
|
-
if (currentlyResovedTemplates.length === 0) {
|
|
1793
|
-
throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
1794
|
-
.map(function (_a) {
|
|
1795
|
-
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
1796
|
-
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
1797
|
-
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
1798
|
-
.join(', '));
|
|
1799
|
-
})
|
|
1800
|
-
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
|
|
1801
|
-
}
|
|
1802
|
-
resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
|
|
1803
|
-
var resultingParameterName = _a.resultingParameterName;
|
|
1804
|
-
return resultingParameterName;
|
|
1805
|
-
})), false);
|
|
1806
|
-
unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
|
|
1807
|
-
};
|
|
1808
|
-
while (unresovedTemplates.length > 0) {
|
|
1809
|
-
_loop_2();
|
|
1810
|
-
}
|
|
1811
|
-
return promptbook;
|
|
1812
|
-
}
|
|
1813
|
-
/**
|
|
1814
|
-
* TODO: [🧠] Work with promptbookVersion
|
|
1815
|
-
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
1816
|
-
* > /**
|
|
1817
|
-
* > * Validates PromptbookJson if it is logically valid.
|
|
1818
|
-
* > *
|
|
1819
|
-
* > * It checks:
|
|
1820
|
-
* > * - it has a valid structure
|
|
1821
|
-
* > * - ...
|
|
1822
|
-
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
1823
|
-
*/
|
|
1824
|
-
|
|
1825
|
-
/**
|
|
1826
|
-
* This error indicates that the promptbook library cannot be propperly loaded
|
|
1827
|
-
*/
|
|
1828
|
-
var PromptbookLibraryError = /** @class */ (function (_super) {
|
|
1829
|
-
__extends(PromptbookLibraryError, _super);
|
|
1830
|
-
function PromptbookLibraryError(message) {
|
|
1831
|
-
var _this = _super.call(this, message) || this;
|
|
1832
|
-
_this.name = 'PromptbookLibraryError';
|
|
1833
|
-
Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
|
|
1834
|
-
return _this;
|
|
1835
|
-
}
|
|
1836
|
-
return PromptbookLibraryError;
|
|
1837
|
-
}(Error));
|
|
1838
|
-
|
|
1839
|
-
/**
|
|
1840
|
-
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
1841
|
-
*/
|
|
1842
|
-
new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
|
|
1843
|
-
/**
|
|
1844
|
-
* Detects if the code is running in a Node.js environment
|
|
1845
|
-
*/
|
|
1846
|
-
var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
1847
|
-
/**
|
|
1848
|
-
* Detects if the code is running in a web worker
|
|
1849
|
-
*/
|
|
1850
|
-
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");
|
|
1851
|
-
|
|
1852
|
-
/**
|
|
1853
|
-
* Prettify the html code
|
|
1854
|
-
*
|
|
1855
|
-
* @param content raw html code
|
|
1856
|
-
* @returns formatted html code
|
|
1857
|
-
*/
|
|
1858
|
-
function prettifyMarkdown(content) {
|
|
1859
|
-
try {
|
|
1860
|
-
return format(content, {
|
|
1861
|
-
parser: 'markdown',
|
|
1862
|
-
plugins: [parserHtml],
|
|
1863
|
-
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
1864
|
-
endOfLine: 'lf',
|
|
1865
|
-
tabWidth: 4,
|
|
1866
|
-
singleQuote: true,
|
|
1867
|
-
trailingComma: 'all',
|
|
1868
|
-
arrowParens: 'always',
|
|
1869
|
-
printWidth: 120,
|
|
1870
|
-
htmlWhitespaceSensitivity: 'ignore',
|
|
1871
|
-
jsxBracketSameLine: false,
|
|
1872
|
-
bracketSpacing: true,
|
|
1873
|
-
});
|
|
1874
|
-
}
|
|
1875
|
-
catch (error) {
|
|
1876
|
-
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
1877
|
-
error: error,
|
|
1878
|
-
html: content,
|
|
1879
|
-
});
|
|
1880
|
-
return content;
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
/**
|
|
1885
|
-
* Converts promptbook in JSON format to string format
|
|
1886
|
-
*
|
|
1887
|
-
* @param promptbookJson Promptbook in JSON format (.ptbk.json)
|
|
1888
|
-
* @returns Promptbook in string format (.ptbk.md)
|
|
1889
|
-
*/
|
|
1890
|
-
function promptbookJsonToString(promptbookJson) {
|
|
1891
|
-
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
|
|
1892
|
-
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1893
|
-
var promptbookString = "# ".concat(title);
|
|
1894
|
-
if (description) {
|
|
1895
|
-
promptbookString += '\n\n';
|
|
1896
|
-
promptbookString += description;
|
|
1897
|
-
}
|
|
1898
|
-
// TODO:> const commands: Array<Command>
|
|
1899
|
-
var commands = [];
|
|
1900
|
-
if (promptbookUrl) {
|
|
1901
|
-
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1902
|
-
}
|
|
1903
|
-
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1904
|
-
promptbookString = prettifyMarkdown(promptbookString);
|
|
1905
|
-
try {
|
|
1906
|
-
for (var _g = __values(parameters.filter(function (_a) {
|
|
1907
|
-
var isInput = _a.isInput;
|
|
1908
|
-
return isInput;
|
|
1909
|
-
})), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1910
|
-
var parameter = _h.value;
|
|
1911
|
-
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1912
|
-
}
|
|
1913
|
-
}
|
|
1914
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1915
|
-
finally {
|
|
1916
|
-
try {
|
|
1917
|
-
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
1598
|
+
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
1918
1599
|
}
|
|
1919
1600
|
finally { if (e_1) throw e_1.error; }
|
|
1920
1601
|
}
|
|
@@ -2006,252 +1687,1535 @@ function promptbookJsonToString(promptbookJson) {
|
|
|
2006
1687
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
2007
1688
|
finally {
|
|
2008
1689
|
try {
|
|
2009
|
-
if (postprocessing_1_1 && !postprocessing_1_1.done && (_e = postprocessing_1.return)) _e.call(postprocessing_1);
|
|
2010
|
-
}
|
|
2011
|
-
finally { if (e_5) throw e_5.error; }
|
|
2012
|
-
}
|
|
2013
|
-
} /* not else */
|
|
2014
|
-
if (expectations) {
|
|
2015
|
-
try {
|
|
2016
|
-
for (var _l = (e_6 = void 0, __values(Object.entries(expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
2017
|
-
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
2018
|
-
if (min === max) {
|
|
2019
|
-
commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
2020
|
-
}
|
|
2021
|
-
else {
|
|
2022
|
-
if (min !== undefined) {
|
|
2023
|
-
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
2024
|
-
} /* not else */
|
|
2025
|
-
if (max !== undefined) {
|
|
2026
|
-
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
2027
|
-
}
|
|
1690
|
+
if (postprocessing_1_1 && !postprocessing_1_1.done && (_e = postprocessing_1.return)) _e.call(postprocessing_1);
|
|
1691
|
+
}
|
|
1692
|
+
finally { if (e_5) throw e_5.error; }
|
|
1693
|
+
}
|
|
1694
|
+
} /* not else */
|
|
1695
|
+
if (expectations) {
|
|
1696
|
+
try {
|
|
1697
|
+
for (var _l = (e_6 = void 0, __values(Object.entries(expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1698
|
+
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1699
|
+
if (min === max) {
|
|
1700
|
+
commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1701
|
+
}
|
|
1702
|
+
else {
|
|
1703
|
+
if (min !== undefined) {
|
|
1704
|
+
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1705
|
+
} /* not else */
|
|
1706
|
+
if (max !== undefined) {
|
|
1707
|
+
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1713
|
+
finally {
|
|
1714
|
+
try {
|
|
1715
|
+
if (_m && !_m.done && (_f = _l.return)) _f.call(_l);
|
|
1716
|
+
}
|
|
1717
|
+
finally { if (e_6) throw e_6.error; }
|
|
1718
|
+
}
|
|
1719
|
+
} /* not else */
|
|
1720
|
+
if (expectFormat) {
|
|
1721
|
+
if (expectFormat === 'JSON') {
|
|
1722
|
+
// TODO: @deprecated remove
|
|
1723
|
+
commands_1.push("EXPECT JSON");
|
|
1724
|
+
}
|
|
1725
|
+
} /* not else */
|
|
1726
|
+
promptbookString += '\n\n';
|
|
1727
|
+
promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1728
|
+
promptbookString += '\n\n';
|
|
1729
|
+
promptbookString += '```' + contentLanguage;
|
|
1730
|
+
promptbookString += '\n';
|
|
1731
|
+
promptbookString += spaceTrim$1(content);
|
|
1732
|
+
// <- TODO: !!! Escape
|
|
1733
|
+
// <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
|
|
1734
|
+
promptbookString += '\n';
|
|
1735
|
+
promptbookString += '```';
|
|
1736
|
+
promptbookString += '\n\n';
|
|
1737
|
+
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1741
|
+
finally {
|
|
1742
|
+
try {
|
|
1743
|
+
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
|
|
1744
|
+
}
|
|
1745
|
+
finally { if (e_3) throw e_3.error; }
|
|
1746
|
+
}
|
|
1747
|
+
return promptbookString;
|
|
1748
|
+
}
|
|
1749
|
+
/**
|
|
1750
|
+
* @private internal util of promptbookJsonToString
|
|
1751
|
+
*/
|
|
1752
|
+
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
1753
|
+
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
1754
|
+
var parameterString = "{".concat(name, "}");
|
|
1755
|
+
if (description) {
|
|
1756
|
+
parameterString = "".concat(parameterString, " ").concat(description);
|
|
1757
|
+
}
|
|
1758
|
+
return parameterString;
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* TODO: Escape all
|
|
1762
|
+
*/
|
|
1763
|
+
|
|
1764
|
+
/**
|
|
1765
|
+
* This error indicates that promptbook not found in the library
|
|
1766
|
+
*/
|
|
1767
|
+
var PromptbookNotFoundError = /** @class */ (function (_super) {
|
|
1768
|
+
__extends(PromptbookNotFoundError, _super);
|
|
1769
|
+
function PromptbookNotFoundError(message) {
|
|
1770
|
+
var _this = _super.call(this, message) || this;
|
|
1771
|
+
_this.name = 'PromptbookNotFoundError';
|
|
1772
|
+
Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
|
|
1773
|
+
return _this;
|
|
1774
|
+
}
|
|
1775
|
+
return PromptbookNotFoundError;
|
|
1776
|
+
}(Error));
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* This error indicates errors in referencing promptbooks between each other
|
|
1780
|
+
*/
|
|
1781
|
+
var PromptbookReferenceError = /** @class */ (function (_super) {
|
|
1782
|
+
__extends(PromptbookReferenceError, _super);
|
|
1783
|
+
function PromptbookReferenceError(message) {
|
|
1784
|
+
var _this = _super.call(this, message) || this;
|
|
1785
|
+
_this.name = 'PromptbookReferenceError';
|
|
1786
|
+
Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
|
|
1787
|
+
return _this;
|
|
1788
|
+
}
|
|
1789
|
+
return PromptbookReferenceError;
|
|
1790
|
+
}(Error));
|
|
1791
|
+
|
|
1792
|
+
/**
|
|
1793
|
+
* Library of promptbooks that groups together promptbooks for an application.
|
|
1794
|
+
* This implementation is a very thin wrapper around the Array / Map of promptbooks.
|
|
1795
|
+
*
|
|
1796
|
+
* @see https://github.com/webgptorg/promptbook#promptbook-library
|
|
1797
|
+
*/
|
|
1798
|
+
var SimplePromptbookLibrary = /** @class */ (function () {
|
|
1799
|
+
/**
|
|
1800
|
+
* Constructs a promptbook library from promptbooks
|
|
1801
|
+
*
|
|
1802
|
+
* @param promptbooks !!!
|
|
1803
|
+
*
|
|
1804
|
+
* Note: During the construction logic of all promptbooks are validated
|
|
1805
|
+
* Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
|
|
1806
|
+
*/
|
|
1807
|
+
function SimplePromptbookLibrary() {
|
|
1808
|
+
var e_1, _a;
|
|
1809
|
+
var promptbooks = [];
|
|
1810
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1811
|
+
promptbooks[_i] = arguments[_i];
|
|
1812
|
+
}
|
|
1813
|
+
this.library = new Map();
|
|
1814
|
+
try {
|
|
1815
|
+
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
1816
|
+
var promptbook = promptbooks_1_1.value;
|
|
1817
|
+
if (promptbook.promptbookUrl === undefined) {
|
|
1818
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
1819
|
+
}
|
|
1820
|
+
validatePromptbookJson(promptbook);
|
|
1821
|
+
// Note: [🦄]
|
|
1822
|
+
if (this.library.has(promptbook.promptbookUrl) &&
|
|
1823
|
+
promptbookJsonToString(promptbook) !==
|
|
1824
|
+
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
1825
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
|
|
1826
|
+
}
|
|
1827
|
+
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1831
|
+
finally {
|
|
1832
|
+
try {
|
|
1833
|
+
if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
|
|
1834
|
+
}
|
|
1835
|
+
finally { if (e_1) throw e_1.error; }
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Gets all promptbooks in the library
|
|
1840
|
+
*/
|
|
1841
|
+
SimplePromptbookLibrary.prototype.listPromptbooks = function () {
|
|
1842
|
+
return Array.from(this.library.keys());
|
|
1843
|
+
};
|
|
1844
|
+
/**
|
|
1845
|
+
* Gets promptbook by its URL
|
|
1846
|
+
*
|
|
1847
|
+
* Note: This is not a direct fetching from the URL, but a lookup in the library
|
|
1848
|
+
*/
|
|
1849
|
+
SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
|
|
1850
|
+
var _this = this;
|
|
1851
|
+
var promptbook = this.library.get(url);
|
|
1852
|
+
if (!promptbook) {
|
|
1853
|
+
if (this.listPromptbooks().length === 0) {
|
|
1854
|
+
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
1855
|
+
}
|
|
1856
|
+
throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
1857
|
+
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
1858
|
+
.join('\n')), "\n\n "); }));
|
|
1859
|
+
}
|
|
1860
|
+
return promptbook;
|
|
1861
|
+
};
|
|
1862
|
+
/**
|
|
1863
|
+
* Checks whether given prompt was defined in any promptbook in the library
|
|
1864
|
+
*/
|
|
1865
|
+
SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
|
|
1866
|
+
return true;
|
|
1867
|
+
};
|
|
1868
|
+
return SimplePromptbookLibrary;
|
|
1869
|
+
}());
|
|
1870
|
+
|
|
1871
|
+
/**
|
|
1872
|
+
* Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
|
|
1873
|
+
*
|
|
1874
|
+
* Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
|
|
1875
|
+
* Note: During the construction syntax and logic of all sources are validated
|
|
1876
|
+
*
|
|
1877
|
+
* @param promptbookSources
|
|
1878
|
+
* @returns PromptbookLibrary
|
|
1879
|
+
*/
|
|
1880
|
+
function createPromptbookLibraryFromSources() {
|
|
1881
|
+
var promptbookSources = [];
|
|
1882
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1883
|
+
promptbookSources[_i] = arguments[_i];
|
|
1884
|
+
}
|
|
1885
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1886
|
+
var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
|
|
1887
|
+
var e_1, _a;
|
|
1888
|
+
return __generator(this, function (_b) {
|
|
1889
|
+
switch (_b.label) {
|
|
1890
|
+
case 0:
|
|
1891
|
+
promptbooks = new Array();
|
|
1892
|
+
_b.label = 1;
|
|
1893
|
+
case 1:
|
|
1894
|
+
_b.trys.push([1, 8, 9, 10]);
|
|
1895
|
+
promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
|
|
1896
|
+
_b.label = 2;
|
|
1897
|
+
case 2:
|
|
1898
|
+
if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
|
|
1899
|
+
source = promptbookSources_1_1.value;
|
|
1900
|
+
promptbook = void 0;
|
|
1901
|
+
if (!(typeof source === 'string')) return [3 /*break*/, 4];
|
|
1902
|
+
return [4 /*yield*/, promptbookStringToJson(source)];
|
|
1903
|
+
case 3:
|
|
1904
|
+
// Note: When directly creating from string, no need to validate the source
|
|
1905
|
+
// The validation is performed always before execution
|
|
1906
|
+
promptbook = _b.sent();
|
|
1907
|
+
return [3 /*break*/, 5];
|
|
1908
|
+
case 4:
|
|
1909
|
+
promptbook = source;
|
|
1910
|
+
_b.label = 5;
|
|
1911
|
+
case 5:
|
|
1912
|
+
promptbooks.push(promptbook);
|
|
1913
|
+
_b.label = 6;
|
|
1914
|
+
case 6:
|
|
1915
|
+
promptbookSources_1_1 = promptbookSources_1.next();
|
|
1916
|
+
return [3 /*break*/, 2];
|
|
1917
|
+
case 7: return [3 /*break*/, 10];
|
|
1918
|
+
case 8:
|
|
1919
|
+
e_1_1 = _b.sent();
|
|
1920
|
+
e_1 = { error: e_1_1 };
|
|
1921
|
+
return [3 /*break*/, 10];
|
|
1922
|
+
case 9:
|
|
1923
|
+
try {
|
|
1924
|
+
if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
|
|
1925
|
+
}
|
|
1926
|
+
finally { if (e_1) throw e_1.error; }
|
|
1927
|
+
return [7 /*endfinally*/];
|
|
1928
|
+
case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
|
|
1929
|
+
}
|
|
1930
|
+
});
|
|
1931
|
+
});
|
|
1932
|
+
}
|
|
1933
|
+
/**
|
|
1934
|
+
* TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
|
|
1935
|
+
*/
|
|
1936
|
+
|
|
1937
|
+
/* tslint:disable */
|
|
1938
|
+
function normalizeToKebabCase(sentence) {
|
|
1939
|
+
var e_1, _a;
|
|
1940
|
+
sentence = removeDiacritics(sentence);
|
|
1941
|
+
var charType;
|
|
1942
|
+
var lastCharType = 'OTHER';
|
|
1943
|
+
var normalizedName = '';
|
|
1944
|
+
try {
|
|
1945
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
1946
|
+
var char = sentence_1_1.value;
|
|
1947
|
+
var normalizedChar = void 0;
|
|
1948
|
+
if (/^[a-z]$/.test(char)) {
|
|
1949
|
+
charType = 'LOWERCASE';
|
|
1950
|
+
normalizedChar = char;
|
|
1951
|
+
}
|
|
1952
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
1953
|
+
charType = 'UPPERCASE';
|
|
1954
|
+
normalizedChar = char.toLowerCase();
|
|
1955
|
+
}
|
|
1956
|
+
else if (/^[0-9]$/.test(char)) {
|
|
1957
|
+
charType = 'NUMBER';
|
|
1958
|
+
normalizedChar = char;
|
|
1959
|
+
}
|
|
1960
|
+
else if (/^\/$/.test(char)) {
|
|
1961
|
+
charType = 'SLASH';
|
|
1962
|
+
normalizedChar = char;
|
|
1963
|
+
}
|
|
1964
|
+
else {
|
|
1965
|
+
charType = 'OTHER';
|
|
1966
|
+
normalizedChar = '-';
|
|
1967
|
+
}
|
|
1968
|
+
if (charType !== lastCharType &&
|
|
1969
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
1970
|
+
!(lastCharType === 'NUMBER') &&
|
|
1971
|
+
!(charType === 'NUMBER')) {
|
|
1972
|
+
normalizedName += '-';
|
|
1973
|
+
}
|
|
1974
|
+
normalizedName += normalizedChar;
|
|
1975
|
+
lastCharType = charType;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1979
|
+
finally {
|
|
1980
|
+
try {
|
|
1981
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
1982
|
+
}
|
|
1983
|
+
finally { if (e_1) throw e_1.error; }
|
|
1984
|
+
}
|
|
1985
|
+
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
1986
|
+
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
1987
|
+
normalizedName = normalizedName.replace(/^-/, '');
|
|
1988
|
+
normalizedName = normalizedName.replace(/-$/, '');
|
|
1989
|
+
return normalizedName;
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
function prepareKnowledgeFromMarkdown(options) {
|
|
1993
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1994
|
+
var content, llmTools, library, promptbook, executor, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
|
|
1995
|
+
var _this = this;
|
|
1996
|
+
return __generator(this, function (_a) {
|
|
1997
|
+
switch (_a.label) {
|
|
1998
|
+
case 0:
|
|
1999
|
+
content = options.content, llmTools = options.llmTools;
|
|
2000
|
+
return [4 /*yield*/, createPromptbookLibraryFromSources(
|
|
2001
|
+
/* !!!! ...(promptbookLibrary as Array<PromptbookJson>)*/ {
|
|
2002
|
+
title: 'Prepare Knowledge from Markdown',
|
|
2003
|
+
promptbookUrl: 'https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md',
|
|
2004
|
+
promptbookVersion: '0.59.0-5',
|
|
2005
|
+
parameters: [
|
|
2006
|
+
{ name: 'content', description: 'Markdown document content', isInput: true, isOutput: false },
|
|
2007
|
+
{ name: 'knowledge', description: 'The knowledge JSON object', isInput: false, isOutput: true },
|
|
2008
|
+
],
|
|
2009
|
+
promptTemplates: [
|
|
2010
|
+
{
|
|
2011
|
+
name: 'knowledge',
|
|
2012
|
+
title: 'Knowledge',
|
|
2013
|
+
dependentParameterNames: ['content'],
|
|
2014
|
+
executionType: 'PROMPT_TEMPLATE',
|
|
2015
|
+
modelRequirements: { modelVariant: 'CHAT', modelName: 'claude-3-opus-20240229' },
|
|
2016
|
+
content: 'You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}',
|
|
2017
|
+
resultingParameterName: 'knowledge',
|
|
2018
|
+
},
|
|
2019
|
+
],
|
|
2020
|
+
knowledge: [],
|
|
2021
|
+
})];
|
|
2022
|
+
case 1:
|
|
2023
|
+
library = _a.sent();
|
|
2024
|
+
promptbook = library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md');
|
|
2025
|
+
executor = createPromptbookExecutor({
|
|
2026
|
+
promptbook: promptbook,
|
|
2027
|
+
tools: {
|
|
2028
|
+
llm: llmTools,
|
|
2029
|
+
script: [
|
|
2030
|
+
/* <- TODO: Allow to just not define script tools */
|
|
2031
|
+
],
|
|
2032
|
+
},
|
|
2033
|
+
});
|
|
2034
|
+
return [4 /*yield*/, executor({ content: content })];
|
|
2035
|
+
case 2:
|
|
2036
|
+
result = _a.sent();
|
|
2037
|
+
assertsExecutionSuccessful(result);
|
|
2038
|
+
outputParameters = result.outputParameters;
|
|
2039
|
+
knowledgeRaw = outputParameters.knowledge;
|
|
2040
|
+
knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
|
|
2041
|
+
return [4 /*yield*/, Promise.all(knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
|
|
2042
|
+
var name, title, content, keywords, index, sources;
|
|
2043
|
+
return __generator(this, function (_a) {
|
|
2044
|
+
name = "piece-".concat(i);
|
|
2045
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 100));
|
|
2046
|
+
content = spaceTrim$1(knowledgeTextPiece);
|
|
2047
|
+
keywords = [];
|
|
2048
|
+
index = [];
|
|
2049
|
+
sources = [];
|
|
2050
|
+
try {
|
|
2051
|
+
// TODO: !!!! Summarize name and title from the content
|
|
2052
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 30));
|
|
2053
|
+
name = normalizeToKebabCase(title);
|
|
2054
|
+
// TODO: !!!! Extract keywords via prompt
|
|
2055
|
+
// TODO: !!!! Index through LLM model
|
|
2056
|
+
// TODO: [🖖] !!!! Make system for sources and identification of sources
|
|
2057
|
+
}
|
|
2058
|
+
catch (error) {
|
|
2059
|
+
console.error(error);
|
|
2060
|
+
}
|
|
2061
|
+
return [2 /*return*/, {
|
|
2062
|
+
name: name,
|
|
2063
|
+
title: title,
|
|
2064
|
+
content: content,
|
|
2065
|
+
keywords: keywords,
|
|
2066
|
+
index: index,
|
|
2067
|
+
sources: sources,
|
|
2068
|
+
}];
|
|
2069
|
+
});
|
|
2070
|
+
}); }))];
|
|
2071
|
+
case 3:
|
|
2072
|
+
knowledge = _a.sent();
|
|
2073
|
+
return [2 /*return*/, knowledge];
|
|
2074
|
+
}
|
|
2075
|
+
});
|
|
2076
|
+
});
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
/**
|
|
2080
|
+
* Supported script languages
|
|
2081
|
+
*/
|
|
2082
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
2083
|
+
|
|
2084
|
+
/**
|
|
2085
|
+
* Computes the deepness of the markdown structure.
|
|
2086
|
+
*
|
|
2087
|
+
* @private within the library
|
|
2088
|
+
*/
|
|
2089
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
2090
|
+
var e_1, _a;
|
|
2091
|
+
var maxDeepness = 0;
|
|
2092
|
+
try {
|
|
2093
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2094
|
+
var section = _c.value;
|
|
2095
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2099
|
+
finally {
|
|
2100
|
+
try {
|
|
2101
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2102
|
+
}
|
|
2103
|
+
finally { if (e_1) throw e_1.error; }
|
|
2104
|
+
}
|
|
2105
|
+
return maxDeepness + 1;
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
/**
|
|
2109
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
2110
|
+
*
|
|
2111
|
+
* Note: This function does work with code blocks
|
|
2112
|
+
* Note: This function does not work with markdown comments
|
|
2113
|
+
*
|
|
2114
|
+
* @param markdown The markdown string to parse.
|
|
2115
|
+
* @returns The MarkdownStructure object.
|
|
2116
|
+
*
|
|
2117
|
+
* @private within the library
|
|
2118
|
+
*/
|
|
2119
|
+
function markdownToMarkdownStructure(markdown) {
|
|
2120
|
+
var e_1, _a;
|
|
2121
|
+
var lines = markdown.split('\n');
|
|
2122
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
2123
|
+
var current = root;
|
|
2124
|
+
var isInsideCodeBlock = false;
|
|
2125
|
+
try {
|
|
2126
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2127
|
+
var line = lines_1_1.value;
|
|
2128
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
2129
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
2130
|
+
if (line.startsWith('```')) {
|
|
2131
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
2132
|
+
}
|
|
2133
|
+
current.contentLines.push(line);
|
|
2134
|
+
}
|
|
2135
|
+
else {
|
|
2136
|
+
var level = headingMatch.groups.mark.length;
|
|
2137
|
+
var title = headingMatch.groups.title.trim();
|
|
2138
|
+
var parent_1 = void 0;
|
|
2139
|
+
if (level > current.level) {
|
|
2140
|
+
// Note: Going deeper (next section is child of current)
|
|
2141
|
+
parent_1 = current;
|
|
2142
|
+
}
|
|
2143
|
+
else {
|
|
2144
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
2145
|
+
parent_1 = current;
|
|
2146
|
+
var loopLimit = LOOP_LIMIT;
|
|
2147
|
+
while (parent_1.level !== level - 1) {
|
|
2148
|
+
if (loopLimit-- < 0) {
|
|
2149
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
2150
|
+
}
|
|
2151
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
2152
|
+
// [🌻]
|
|
2153
|
+
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
2154
|
+
}
|
|
2155
|
+
parent_1 = parent_1.parent;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
2159
|
+
parent_1.sections.push(section);
|
|
2160
|
+
current = section;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2165
|
+
finally {
|
|
2166
|
+
try {
|
|
2167
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2168
|
+
}
|
|
2169
|
+
finally { if (e_1) throw e_1.error; }
|
|
2170
|
+
}
|
|
2171
|
+
if (root.sections.length === 1) {
|
|
2172
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
2173
|
+
return markdownStructure;
|
|
2174
|
+
}
|
|
2175
|
+
// [🌻]
|
|
2176
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
2177
|
+
// return root;
|
|
2178
|
+
}
|
|
2179
|
+
/**
|
|
2180
|
+
* @private
|
|
2181
|
+
*/
|
|
2182
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
2183
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
2184
|
+
return {
|
|
2185
|
+
level: level,
|
|
2186
|
+
title: title,
|
|
2187
|
+
content: spaceTrim(contentLines.join('\n')),
|
|
2188
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
2189
|
+
};
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
/**
|
|
2193
|
+
* Utility function to extract all list items from markdown
|
|
2194
|
+
*
|
|
2195
|
+
* Note: It works with both ul and ol
|
|
2196
|
+
* Note: It omits list items in code blocks
|
|
2197
|
+
* Note: It flattens nested lists
|
|
2198
|
+
* Note: It can not work with html syntax and comments
|
|
2199
|
+
*
|
|
2200
|
+
* @param markdown any valid markdown
|
|
2201
|
+
* @returns
|
|
2202
|
+
*/
|
|
2203
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
2204
|
+
var e_1, _a;
|
|
2205
|
+
var lines = markdown.split('\n');
|
|
2206
|
+
var listItems = [];
|
|
2207
|
+
var isInCodeBlock = false;
|
|
2208
|
+
try {
|
|
2209
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2210
|
+
var line = lines_1_1.value;
|
|
2211
|
+
var trimmedLine = line.trim();
|
|
2212
|
+
if (trimmedLine.startsWith('```')) {
|
|
2213
|
+
isInCodeBlock = !isInCodeBlock;
|
|
2214
|
+
}
|
|
2215
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
2216
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
2217
|
+
listItems.push(listItem);
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2222
|
+
finally {
|
|
2223
|
+
try {
|
|
2224
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2225
|
+
}
|
|
2226
|
+
finally { if (e_1) throw e_1.error; }
|
|
2227
|
+
}
|
|
2228
|
+
return listItems;
|
|
2229
|
+
}
|
|
2230
|
+
|
|
2231
|
+
/**
|
|
2232
|
+
* Extracts all code blocks from markdown.
|
|
2233
|
+
*
|
|
2234
|
+
* Note: There are 3 simmilar function:
|
|
2235
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2236
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2237
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2238
|
+
*
|
|
2239
|
+
* @param markdown any valid markdown
|
|
2240
|
+
* @returns code blocks with language and content
|
|
2241
|
+
*
|
|
2242
|
+
*/
|
|
2243
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
2244
|
+
var e_1, _a;
|
|
2245
|
+
var codeBlocks = [];
|
|
2246
|
+
var lines = markdown.split('\n');
|
|
2247
|
+
var currentCodeBlock = null;
|
|
2248
|
+
try {
|
|
2249
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2250
|
+
var line = lines_1_1.value;
|
|
2251
|
+
if (line.startsWith('```')) {
|
|
2252
|
+
var language = line.slice(3).trim() || null;
|
|
2253
|
+
if (currentCodeBlock === null) {
|
|
2254
|
+
currentCodeBlock = { language: language, content: '' };
|
|
2255
|
+
}
|
|
2256
|
+
else {
|
|
2257
|
+
if (language !== null) {
|
|
2258
|
+
// [🌻]
|
|
2259
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
2260
|
+
}
|
|
2261
|
+
codeBlocks.push(currentCodeBlock);
|
|
2262
|
+
currentCodeBlock = null;
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
else if (currentCodeBlock !== null) {
|
|
2266
|
+
if (currentCodeBlock.content !== '') {
|
|
2267
|
+
currentCodeBlock.content += '\n';
|
|
2268
|
+
}
|
|
2269
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2274
|
+
finally {
|
|
2275
|
+
try {
|
|
2276
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2277
|
+
}
|
|
2278
|
+
finally { if (e_1) throw e_1.error; }
|
|
2279
|
+
}
|
|
2280
|
+
if (currentCodeBlock !== null) {
|
|
2281
|
+
// [🌻]
|
|
2282
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
2283
|
+
}
|
|
2284
|
+
return codeBlocks;
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
/**
|
|
2288
|
+
* Extracts exactly ONE code block from markdown.
|
|
2289
|
+
*
|
|
2290
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
2291
|
+
*
|
|
2292
|
+
* Note: There are 3 simmilar function:
|
|
2293
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2294
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2295
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2296
|
+
*
|
|
2297
|
+
* @param markdown any valid markdown
|
|
2298
|
+
* @returns code block with language and content
|
|
2299
|
+
*/
|
|
2300
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
2301
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
2302
|
+
if (codeBlocks.length !== 1) {
|
|
2303
|
+
// TODO: Report more specific place where the error happened
|
|
2304
|
+
throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
|
|
2305
|
+
}
|
|
2306
|
+
return codeBlocks[0];
|
|
2307
|
+
}
|
|
2308
|
+
/***
|
|
2309
|
+
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
2310
|
+
*/
|
|
2311
|
+
|
|
2312
|
+
/**
|
|
2313
|
+
* Removes HTML or Markdown comments from a string.
|
|
2314
|
+
*
|
|
2315
|
+
* @param {string} content - The string to remove comments from.
|
|
2316
|
+
* @returns {string} The input string with all comments removed.
|
|
2317
|
+
*/
|
|
2318
|
+
function removeContentComments(content) {
|
|
2319
|
+
return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
/**
|
|
2323
|
+
* Creates a new set with all elements that are present in either set
|
|
2324
|
+
*/
|
|
2325
|
+
function union() {
|
|
2326
|
+
var e_1, _a, e_2, _b;
|
|
2327
|
+
var sets = [];
|
|
2328
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2329
|
+
sets[_i] = arguments[_i];
|
|
2330
|
+
}
|
|
2331
|
+
var union = new Set();
|
|
2332
|
+
try {
|
|
2333
|
+
for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
|
|
2334
|
+
var set = sets_1_1.value;
|
|
2335
|
+
try {
|
|
2336
|
+
for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
2337
|
+
var item = _d.value;
|
|
2338
|
+
union.add(item);
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2342
|
+
finally {
|
|
2343
|
+
try {
|
|
2344
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
2345
|
+
}
|
|
2346
|
+
finally { if (e_2) throw e_2.error; }
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2351
|
+
finally {
|
|
2352
|
+
try {
|
|
2353
|
+
if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
|
|
2354
|
+
}
|
|
2355
|
+
finally { if (e_1) throw e_1.error; }
|
|
2356
|
+
}
|
|
2357
|
+
return union;
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
/**
|
|
2361
|
+
* Parses the template and returns the list of all parameter names
|
|
2362
|
+
*
|
|
2363
|
+
* @param template the template with parameters in {curly} braces
|
|
2364
|
+
* @returns the list of parameter names
|
|
2365
|
+
*/
|
|
2366
|
+
function extractParameters(template) {
|
|
2367
|
+
var e_1, _a;
|
|
2368
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
2369
|
+
var parameterNames = new Set();
|
|
2370
|
+
try {
|
|
2371
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
2372
|
+
var match = matches_1_1.value;
|
|
2373
|
+
var parameterName = match[0].slice(1, -1);
|
|
2374
|
+
parameterNames.add(parameterName);
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2378
|
+
finally {
|
|
2379
|
+
try {
|
|
2380
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
2381
|
+
}
|
|
2382
|
+
finally { if (e_1) throw e_1.error; }
|
|
2383
|
+
}
|
|
2384
|
+
return parameterNames;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
2389
|
+
*
|
|
2390
|
+
* @param script from which to extract the variables
|
|
2391
|
+
* @returns the list of variable names
|
|
2392
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2393
|
+
*/
|
|
2394
|
+
function extractVariables(script) {
|
|
2395
|
+
var variables = new Set();
|
|
2396
|
+
script = "(()=>{".concat(script, "})()");
|
|
2397
|
+
try {
|
|
2398
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
2399
|
+
try {
|
|
2400
|
+
eval(script);
|
|
2401
|
+
}
|
|
2402
|
+
catch (error) {
|
|
2403
|
+
if (!(error instanceof ReferenceError)) {
|
|
2404
|
+
throw error;
|
|
2405
|
+
}
|
|
2406
|
+
var undefinedName = error.message.split(' ')[0];
|
|
2407
|
+
/*
|
|
2408
|
+
Note: Remapping error
|
|
2409
|
+
From: [ReferenceError: thing is not defined],
|
|
2410
|
+
To: [Error: Parameter {thing} is not defined],
|
|
2411
|
+
*/
|
|
2412
|
+
if (!undefinedName) {
|
|
2413
|
+
throw error;
|
|
2414
|
+
}
|
|
2415
|
+
if (script.includes(undefinedName + '(')) {
|
|
2416
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
2417
|
+
}
|
|
2418
|
+
else {
|
|
2419
|
+
variables.add(undefinedName);
|
|
2420
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
catch (error) {
|
|
2425
|
+
if (!(error instanceof Error)) {
|
|
2426
|
+
throw error;
|
|
2427
|
+
}
|
|
2428
|
+
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 "); }));
|
|
2429
|
+
}
|
|
2430
|
+
return variables;
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
2433
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2434
|
+
*/
|
|
2435
|
+
|
|
2436
|
+
/**
|
|
2437
|
+
* Parses the prompt template and returns the set of all used parameters
|
|
2438
|
+
*
|
|
2439
|
+
* @param promptTemplate the template with used parameters
|
|
2440
|
+
* @returns the set of parameter names
|
|
2441
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2442
|
+
*/
|
|
2443
|
+
function extractParametersFromPromptTemplate(promptTemplate) {
|
|
2444
|
+
var e_1, _a, e_2, _b;
|
|
2445
|
+
var parameterNames = new Set();
|
|
2446
|
+
try {
|
|
2447
|
+
for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
2448
|
+
var parameterName = _d.value;
|
|
2449
|
+
parameterNames.add(parameterName);
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2453
|
+
finally {
|
|
2454
|
+
try {
|
|
2455
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
2456
|
+
}
|
|
2457
|
+
finally { if (e_1) throw e_1.error; }
|
|
2458
|
+
}
|
|
2459
|
+
if (promptTemplate.executionType === 'SCRIPT') {
|
|
2460
|
+
try {
|
|
2461
|
+
for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
2462
|
+
var parameterName = _f.value;
|
|
2463
|
+
parameterNames.add(parameterName);
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2467
|
+
finally {
|
|
2468
|
+
try {
|
|
2469
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
2470
|
+
}
|
|
2471
|
+
finally { if (e_2) throw e_2.error; }
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
return parameterNames;
|
|
2475
|
+
}
|
|
2476
|
+
/**
|
|
2477
|
+
* TODO: [🔣] If script require contentLanguage
|
|
2478
|
+
*/
|
|
2479
|
+
|
|
2480
|
+
/* tslint:disable */
|
|
2481
|
+
/*
|
|
2482
|
+
TODO: Tests
|
|
2483
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
2484
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
2485
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
2486
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
2487
|
+
*/
|
|
2488
|
+
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
2489
|
+
var e_1, _a;
|
|
2490
|
+
var charType;
|
|
2491
|
+
var lastCharType = 'OTHER';
|
|
2492
|
+
var normalizedName = '';
|
|
2493
|
+
try {
|
|
2494
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
2495
|
+
var char = sentence_1_1.value;
|
|
2496
|
+
var normalizedChar = void 0;
|
|
2497
|
+
if (/^[a-z]$/.test(char)) {
|
|
2498
|
+
charType = 'LOWERCASE';
|
|
2499
|
+
normalizedChar = char.toUpperCase();
|
|
2500
|
+
}
|
|
2501
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
2502
|
+
charType = 'UPPERCASE';
|
|
2503
|
+
normalizedChar = char;
|
|
2504
|
+
}
|
|
2505
|
+
else if (/^[0-9]$/.test(char)) {
|
|
2506
|
+
charType = 'NUMBER';
|
|
2507
|
+
normalizedChar = char;
|
|
2508
|
+
}
|
|
2509
|
+
else if (/^\/$/.test(char)) {
|
|
2510
|
+
charType = 'SLASH';
|
|
2511
|
+
normalizedChar = char;
|
|
2512
|
+
}
|
|
2513
|
+
else {
|
|
2514
|
+
charType = 'OTHER';
|
|
2515
|
+
normalizedChar = '_';
|
|
2516
|
+
}
|
|
2517
|
+
if (charType !== lastCharType &&
|
|
2518
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
2519
|
+
!(lastCharType === 'NUMBER') &&
|
|
2520
|
+
!(charType === 'NUMBER')) {
|
|
2521
|
+
normalizedName += '_';
|
|
2522
|
+
}
|
|
2523
|
+
normalizedName += normalizedChar;
|
|
2524
|
+
lastCharType = charType;
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2528
|
+
finally {
|
|
2529
|
+
try {
|
|
2530
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
2531
|
+
}
|
|
2532
|
+
finally { if (e_1) throw e_1.error; }
|
|
2533
|
+
}
|
|
2534
|
+
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
2535
|
+
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
2536
|
+
normalizedName = normalizedName.replace(/^_/, '');
|
|
2537
|
+
normalizedName = normalizedName.replace(/_$/, '');
|
|
2538
|
+
return normalizedName;
|
|
2539
|
+
}
|
|
2540
|
+
/**
|
|
2541
|
+
* TODO: [🌺] Use some intermediate util splitWords
|
|
2542
|
+
*/
|
|
2543
|
+
|
|
2544
|
+
/**
|
|
2545
|
+
* Execution type describes the way how the block is executed
|
|
2546
|
+
*
|
|
2547
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
2548
|
+
*/
|
|
2549
|
+
var ExecutionTypes = [
|
|
2550
|
+
'PROMPT_TEMPLATE',
|
|
2551
|
+
'SIMPLE_TEMPLATE',
|
|
2552
|
+
'SCRIPT',
|
|
2553
|
+
'PROMPT_DIALOG',
|
|
2554
|
+
// <- [🥻] Insert here when making new command
|
|
2555
|
+
];
|
|
2556
|
+
|
|
2557
|
+
/**
|
|
2558
|
+
* Units of text measurement
|
|
2559
|
+
*/
|
|
2560
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
|
|
2561
|
+
/**
|
|
2562
|
+
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
2563
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
2564
|
+
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
2565
|
+
*/
|
|
2566
|
+
|
|
2567
|
+
/**
|
|
2568
|
+
* Removes Markdown formatting tags from a string.
|
|
2569
|
+
*
|
|
2570
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
2571
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
2572
|
+
*/
|
|
2573
|
+
function removeMarkdownFormatting(str) {
|
|
2574
|
+
// Remove bold formatting
|
|
2575
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
2576
|
+
// Remove italic formatting
|
|
2577
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
2578
|
+
// Remove code formatting
|
|
2579
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
2580
|
+
return str;
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
/**
|
|
2584
|
+
* Function parseNumber will parse number from string
|
|
2585
|
+
*
|
|
2586
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
2587
|
+
* Note: it also works only with decimal numbers
|
|
2588
|
+
*
|
|
2589
|
+
* @returns parsed number
|
|
2590
|
+
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
2591
|
+
*
|
|
2592
|
+
* @private within the parseCommand
|
|
2593
|
+
*/
|
|
2594
|
+
function parseNumber(value) {
|
|
2595
|
+
var originalValue = value;
|
|
2596
|
+
if (typeof value === 'number') {
|
|
2597
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
2598
|
+
}
|
|
2599
|
+
if (typeof value !== 'string') {
|
|
2600
|
+
return 0;
|
|
2601
|
+
}
|
|
2602
|
+
value = value.trim();
|
|
2603
|
+
if (value.startsWith('+')) {
|
|
2604
|
+
return parseNumber(value.substring(1));
|
|
2605
|
+
}
|
|
2606
|
+
if (value.startsWith('-')) {
|
|
2607
|
+
var number = parseNumber(value.substring(1));
|
|
2608
|
+
if (number === 0) {
|
|
2609
|
+
return 0; // <- Note: To prevent -0
|
|
2610
|
+
}
|
|
2611
|
+
return -number;
|
|
2612
|
+
}
|
|
2613
|
+
value = value.replace(/,/g, '.');
|
|
2614
|
+
value = value.toUpperCase();
|
|
2615
|
+
if (value === '') {
|
|
2616
|
+
return 0;
|
|
2617
|
+
}
|
|
2618
|
+
if (value === '♾' || value.startsWith('INF')) {
|
|
2619
|
+
return Infinity;
|
|
2620
|
+
}
|
|
2621
|
+
if (value.includes('/')) {
|
|
2622
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
2623
|
+
var numerator = parseNumber(numerator_);
|
|
2624
|
+
var denominator = parseNumber(denominator_);
|
|
2625
|
+
if (denominator === 0) {
|
|
2626
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
2627
|
+
}
|
|
2628
|
+
return numerator / denominator;
|
|
2629
|
+
}
|
|
2630
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
2631
|
+
return 0;
|
|
2632
|
+
}
|
|
2633
|
+
if (value.includes('E')) {
|
|
2634
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
2635
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
2636
|
+
}
|
|
2637
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
2638
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
2639
|
+
}
|
|
2640
|
+
var num = parseFloat(value);
|
|
2641
|
+
if (isNaN(num)) {
|
|
2642
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
2643
|
+
}
|
|
2644
|
+
return num;
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
2648
|
+
*/
|
|
2649
|
+
|
|
2650
|
+
/**
|
|
2651
|
+
* Parses one line of ul/ol to command
|
|
2652
|
+
*
|
|
2653
|
+
* @returns parsed command object
|
|
2654
|
+
* @throws {PromptbookSyntaxError} if the command is invalid
|
|
2655
|
+
*
|
|
2656
|
+
* @private within the promptbookStringToJson
|
|
2657
|
+
*/
|
|
2658
|
+
function parseCommand(listItem) {
|
|
2659
|
+
var e_1, _a;
|
|
2660
|
+
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
2661
|
+
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
2662
|
+
}
|
|
2663
|
+
var type = listItem.trim();
|
|
2664
|
+
type = type.split('`').join('');
|
|
2665
|
+
type = type.split('"').join('');
|
|
2666
|
+
type = type.split("'").join('');
|
|
2667
|
+
type = type.split('~').join('');
|
|
2668
|
+
type = type.split('[').join('');
|
|
2669
|
+
type = type.split(']').join('');
|
|
2670
|
+
type = type.split('(').join('');
|
|
2671
|
+
type = type.split(')').join('');
|
|
2672
|
+
type = normalizeTo_SCREAMING_CASE(type);
|
|
2673
|
+
type = type.split('DIALOGUE').join('DIALOG');
|
|
2674
|
+
var listItemParts = listItem
|
|
2675
|
+
.split(' ')
|
|
2676
|
+
.map(function (part) { return part.trim(); })
|
|
2677
|
+
.filter(function (item) { return item !== ''; })
|
|
2678
|
+
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
2679
|
+
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
2680
|
+
.map(removeMarkdownFormatting);
|
|
2681
|
+
if (type.startsWith('URL') ||
|
|
2682
|
+
type.startsWith('PTBK_URL') ||
|
|
2683
|
+
type.startsWith('PTBKURL') ||
|
|
2684
|
+
type.startsWith('PROMPTBOOK_URL') ||
|
|
2685
|
+
type.startsWith('PROMPTBOOKURL') ||
|
|
2686
|
+
type.startsWith('HTTPS')) {
|
|
2687
|
+
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
2688
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
2689
|
+
}
|
|
2690
|
+
var promptbookUrlString = listItemParts.pop();
|
|
2691
|
+
var promptbookUrl = new URL(promptbookUrlString);
|
|
2692
|
+
if (promptbookUrl.protocol !== 'https:') {
|
|
2693
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
2694
|
+
}
|
|
2695
|
+
if (promptbookUrl.hash !== '') {
|
|
2696
|
+
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 ")));
|
|
2697
|
+
}
|
|
2698
|
+
return {
|
|
2699
|
+
type: 'PROMPTBOOK_URL',
|
|
2700
|
+
promptbookUrl: promptbookUrl,
|
|
2701
|
+
};
|
|
2702
|
+
}
|
|
2703
|
+
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
2704
|
+
if (listItemParts.length !== 2) {
|
|
2705
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
2706
|
+
}
|
|
2707
|
+
var promptbookVersion = listItemParts.pop();
|
|
2708
|
+
// TODO: Validate version
|
|
2709
|
+
return {
|
|
2710
|
+
type: 'PROMPTBOOK_VERSION',
|
|
2711
|
+
promptbookVersion: promptbookVersion,
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2714
|
+
else if (type.startsWith('EXECUTE') ||
|
|
2715
|
+
type.startsWith('EXEC') ||
|
|
2716
|
+
type.startsWith('PROMPT_DIALOG') ||
|
|
2717
|
+
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
2718
|
+
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
2719
|
+
if (executionTypes.length !== 1) {
|
|
2720
|
+
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 "); }));
|
|
2721
|
+
}
|
|
2722
|
+
return {
|
|
2723
|
+
type: 'EXECUTE',
|
|
2724
|
+
executionType: executionTypes[0],
|
|
2725
|
+
};
|
|
2726
|
+
}
|
|
2727
|
+
else if (type.startsWith('MODEL')) {
|
|
2728
|
+
// TODO: Make this more elegant and dynamically
|
|
2729
|
+
if (type.startsWith('MODEL_VARIANT')) {
|
|
2730
|
+
if (type === 'MODEL_VARIANT_CHAT') {
|
|
2731
|
+
return {
|
|
2732
|
+
type: 'MODEL',
|
|
2733
|
+
key: 'modelVariant',
|
|
2734
|
+
value: 'CHAT',
|
|
2735
|
+
};
|
|
2736
|
+
}
|
|
2737
|
+
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
2738
|
+
return {
|
|
2739
|
+
type: 'MODEL',
|
|
2740
|
+
key: 'modelVariant',
|
|
2741
|
+
value: 'COMPLETION',
|
|
2742
|
+
};
|
|
2743
|
+
}
|
|
2744
|
+
else {
|
|
2745
|
+
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 "); }));
|
|
2746
|
+
}
|
|
2747
|
+
}
|
|
2748
|
+
if (type.startsWith('MODEL_NAME')) {
|
|
2749
|
+
return {
|
|
2750
|
+
type: 'MODEL',
|
|
2751
|
+
key: 'modelName',
|
|
2752
|
+
value: listItemParts.pop(),
|
|
2753
|
+
};
|
|
2754
|
+
}
|
|
2755
|
+
else {
|
|
2756
|
+
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 "); }));
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
else if (type.startsWith('PARAM') ||
|
|
2760
|
+
type.startsWith('INPUT_PARAM') ||
|
|
2761
|
+
type.startsWith('OUTPUT_PARAM') ||
|
|
2762
|
+
listItem.startsWith('{') ||
|
|
2763
|
+
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
2764
|
+
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
2765
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2766
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2767
|
+
}
|
|
2768
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2769
|
+
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
2770
|
+
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
2771
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
2772
|
+
}
|
|
2773
|
+
var isInput = type.startsWith('INPUT');
|
|
2774
|
+
var isOutput = type.startsWith('OUTPUT');
|
|
2775
|
+
if (listItem.startsWith('> {')) {
|
|
2776
|
+
isInput = false;
|
|
2777
|
+
isOutput = false;
|
|
2778
|
+
}
|
|
2779
|
+
return {
|
|
2780
|
+
type: 'PARAMETER',
|
|
2781
|
+
parameterName: parameterName,
|
|
2782
|
+
parameterDescription: parameterDescription.trim() || null,
|
|
2783
|
+
isInput: isInput,
|
|
2784
|
+
isOutput: isOutput,
|
|
2785
|
+
};
|
|
2786
|
+
}
|
|
2787
|
+
else if (type.startsWith('JOKER')) {
|
|
2788
|
+
if (listItemParts.length !== 2) {
|
|
2789
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
2790
|
+
}
|
|
2791
|
+
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
2792
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2793
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2794
|
+
}
|
|
2795
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2796
|
+
var parameterName = parametersMatch.groups.parameterName;
|
|
2797
|
+
return {
|
|
2798
|
+
type: 'JOKER',
|
|
2799
|
+
parameterName: parameterName,
|
|
2800
|
+
};
|
|
2801
|
+
}
|
|
2802
|
+
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
2803
|
+
if (listItemParts.length !== 2) {
|
|
2804
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
2805
|
+
}
|
|
2806
|
+
var functionName = listItemParts.pop();
|
|
2807
|
+
return {
|
|
2808
|
+
type: 'POSTPROCESS',
|
|
2809
|
+
functionName: functionName,
|
|
2810
|
+
};
|
|
2811
|
+
}
|
|
2812
|
+
else if (type.startsWith('EXPECT_JSON')) {
|
|
2813
|
+
return {
|
|
2814
|
+
type: 'EXPECT_FORMAT',
|
|
2815
|
+
format: 'JSON',
|
|
2816
|
+
};
|
|
2817
|
+
// [🥤]
|
|
2818
|
+
}
|
|
2819
|
+
else if (type.startsWith('EXPECT')) {
|
|
2820
|
+
try {
|
|
2821
|
+
listItemParts.shift();
|
|
2822
|
+
var sign = void 0;
|
|
2823
|
+
var signRaw = listItemParts.shift();
|
|
2824
|
+
if (/^exact/i.test(signRaw)) {
|
|
2825
|
+
sign = 'EXACTLY';
|
|
2826
|
+
}
|
|
2827
|
+
else if (/^min/i.test(signRaw)) {
|
|
2828
|
+
sign = 'MINIMUM';
|
|
2829
|
+
}
|
|
2830
|
+
else if (/^max/i.test(signRaw)) {
|
|
2831
|
+
sign = 'MAXIMUM';
|
|
2832
|
+
}
|
|
2833
|
+
else {
|
|
2834
|
+
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
2835
|
+
}
|
|
2836
|
+
var amountRaw = listItemParts.shift();
|
|
2837
|
+
var amount = parseNumber(amountRaw);
|
|
2838
|
+
if (amount < 0) {
|
|
2839
|
+
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
2840
|
+
}
|
|
2841
|
+
if (amount !== Math.floor(amount)) {
|
|
2842
|
+
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
2843
|
+
}
|
|
2844
|
+
var unitRaw = listItemParts.shift();
|
|
2845
|
+
var unit = undefined;
|
|
2846
|
+
try {
|
|
2847
|
+
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()) {
|
|
2848
|
+
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
2849
|
+
var existingUnitText = existingUnit;
|
|
2850
|
+
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
2851
|
+
if (existingUnitText === 'CHARACTER') {
|
|
2852
|
+
existingUnitText = 'CHAR';
|
|
2853
|
+
}
|
|
2854
|
+
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
2855
|
+
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
2856
|
+
if (unit !== undefined) {
|
|
2857
|
+
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
2858
|
+
}
|
|
2859
|
+
unit = existingUnit;
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2864
|
+
finally {
|
|
2865
|
+
try {
|
|
2866
|
+
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
2867
|
+
}
|
|
2868
|
+
finally { if (e_1) throw e_1.error; }
|
|
2869
|
+
}
|
|
2870
|
+
if (unit === undefined) {
|
|
2871
|
+
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
2872
|
+
}
|
|
2873
|
+
return {
|
|
2874
|
+
type: 'EXPECT_AMOUNT',
|
|
2875
|
+
sign: sign,
|
|
2876
|
+
unit: unit,
|
|
2877
|
+
amount: amount,
|
|
2878
|
+
};
|
|
2879
|
+
}
|
|
2880
|
+
catch (error) {
|
|
2881
|
+
if (!(error instanceof Error)) {
|
|
2882
|
+
throw error;
|
|
2883
|
+
}
|
|
2884
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
2885
|
+
}
|
|
2886
|
+
/*
|
|
2887
|
+
} else if (type.startsWith('__________________')) {
|
|
2888
|
+
// <- [🥻] Insert here when making new command
|
|
2889
|
+
*/
|
|
2890
|
+
}
|
|
2891
|
+
else {
|
|
2892
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
/**
|
|
2897
|
+
* Removes emojis from a string and fix whitespaces
|
|
2898
|
+
*
|
|
2899
|
+
* @param text with emojis
|
|
2900
|
+
* @returns text without emojis
|
|
2901
|
+
*/
|
|
2902
|
+
function removeEmojis(text) {
|
|
2903
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
2904
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
2905
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
2906
|
+
text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
|
|
2907
|
+
text = text.replace(/\p{Extended_Pictographic}/gu, '');
|
|
2908
|
+
return text;
|
|
2909
|
+
}
|
|
2910
|
+
|
|
2911
|
+
/**
|
|
2912
|
+
* Function normalizes title to name which can be used as identifier
|
|
2913
|
+
*/
|
|
2914
|
+
function titleToName(value) {
|
|
2915
|
+
value = removeEmojis(value);
|
|
2916
|
+
value = normalizeToKebabCase(value);
|
|
2917
|
+
// TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
|
|
2918
|
+
return value;
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
/**
|
|
2922
|
+
* Compile promptbook from string (markdown) format to JSON format
|
|
2923
|
+
*
|
|
2924
|
+
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
2925
|
+
* @param options - Options and tools for the compilation
|
|
2926
|
+
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
2927
|
+
* @throws {PromptbookSyntaxError} if the promptbook string is not valid
|
|
2928
|
+
*
|
|
2929
|
+
* Note: This function does not validate logic of the pipeline only the syntax
|
|
2930
|
+
* Note: This function acts as compilation process
|
|
2931
|
+
*/
|
|
2932
|
+
function promptbookStringToJson(promptbookString, options) {
|
|
2933
|
+
if (options === void 0) { options = {}; }
|
|
2934
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2935
|
+
var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
|
|
2936
|
+
var e_1, _c, e_2, _d;
|
|
2937
|
+
return __generator(this, function (_e) {
|
|
2938
|
+
switch (_e.label) {
|
|
2939
|
+
case 0:
|
|
2940
|
+
llmTools = options.llmTools;
|
|
2941
|
+
promptbookJson = {
|
|
2942
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2943
|
+
title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
|
|
2944
|
+
promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
|
|
2945
|
+
promptbookVersion: PROMPTBOOK_VERSION,
|
|
2946
|
+
description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
|
|
2947
|
+
parameters: [],
|
|
2948
|
+
promptTemplates: [],
|
|
2949
|
+
knowledge: [],
|
|
2950
|
+
};
|
|
2951
|
+
if (!llmTools) return [3 /*break*/, 2];
|
|
2952
|
+
return [4 /*yield*/, prepareKnowledgeFromMarkdown({
|
|
2953
|
+
content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
|
|
2954
|
+
llmTools: llmTools,
|
|
2955
|
+
})];
|
|
2956
|
+
case 1:
|
|
2957
|
+
knowledge = _e.sent();
|
|
2958
|
+
console.info('!!!! knowledge', knowledge);
|
|
2959
|
+
_e.label = 2;
|
|
2960
|
+
case 2:
|
|
2961
|
+
// =============================================================
|
|
2962
|
+
// Note: 1️⃣ Normalization of the PROMPTBOOK string
|
|
2963
|
+
promptbookString = removeContentComments(promptbookString);
|
|
2964
|
+
promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
|
|
2965
|
+
promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
|
|
2966
|
+
addParam = function (parameterCommand) {
|
|
2967
|
+
var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
|
|
2968
|
+
var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
2969
|
+
if (existingParameter &&
|
|
2970
|
+
existingParameter.description &&
|
|
2971
|
+
existingParameter.description !== parameterDescription &&
|
|
2972
|
+
parameterDescription) {
|
|
2973
|
+
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 "); }));
|
|
2974
|
+
}
|
|
2975
|
+
if (existingParameter) {
|
|
2976
|
+
if (parameterDescription) {
|
|
2977
|
+
existingParameter.description = parameterDescription;
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
else {
|
|
2981
|
+
promptbookJson.parameters.push({
|
|
2982
|
+
name: parameterName,
|
|
2983
|
+
description: parameterDescription || undefined,
|
|
2984
|
+
isInput: isInput,
|
|
2985
|
+
isOutput: isOutput,
|
|
2986
|
+
});
|
|
2987
|
+
}
|
|
2988
|
+
};
|
|
2989
|
+
markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
2990
|
+
markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
2991
|
+
if (markdownStructureDeepness !== 2) {
|
|
2992
|
+
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 ")));
|
|
2993
|
+
}
|
|
2994
|
+
promptbookJson.title = markdownStructure.title;
|
|
2995
|
+
description = markdownStructure.content;
|
|
2996
|
+
// Note: Remove codeblocks
|
|
2997
|
+
description = description.split(/^```.*^```/gms).join('');
|
|
2998
|
+
//Note: Remove lists and return statement
|
|
2999
|
+
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
3000
|
+
description = spaceTrim(description);
|
|
3001
|
+
if (description === '') {
|
|
3002
|
+
description = undefined;
|
|
3003
|
+
}
|
|
3004
|
+
promptbookJson.description = description;
|
|
3005
|
+
defaultModelRequirements = {};
|
|
3006
|
+
listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
|
|
3007
|
+
try {
|
|
3008
|
+
for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
|
|
3009
|
+
listItem = listItems_1_1.value;
|
|
3010
|
+
command = parseCommand(listItem);
|
|
3011
|
+
switch (command.type) {
|
|
3012
|
+
case 'PROMPTBOOK_URL':
|
|
3013
|
+
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
3014
|
+
break;
|
|
3015
|
+
case 'PROMPTBOOK_VERSION':
|
|
3016
|
+
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
3017
|
+
break;
|
|
3018
|
+
case 'MODEL':
|
|
3019
|
+
defaultModelRequirements[command.key] = command.value;
|
|
3020
|
+
break;
|
|
3021
|
+
case 'PARAMETER':
|
|
3022
|
+
addParam(command);
|
|
3023
|
+
break;
|
|
3024
|
+
default:
|
|
3025
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3030
|
+
finally {
|
|
3031
|
+
try {
|
|
3032
|
+
if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
|
|
3033
|
+
}
|
|
3034
|
+
finally { if (e_1) throw e_1.error; }
|
|
3035
|
+
}
|
|
3036
|
+
_loop_1 = function (section) {
|
|
3037
|
+
var e_3, _f;
|
|
3038
|
+
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
3039
|
+
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
3040
|
+
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
3041
|
+
var dependentParameterNames = new Set();
|
|
3042
|
+
var executionType = 'PROMPT_TEMPLATE';
|
|
3043
|
+
var jokers = [];
|
|
3044
|
+
var postprocessing = [];
|
|
3045
|
+
var expectAmount = {};
|
|
3046
|
+
var expectFormat = undefined;
|
|
3047
|
+
var isExecutionTypeChanged = false;
|
|
3048
|
+
try {
|
|
3049
|
+
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()) {
|
|
3050
|
+
var listItem = listItems_2_1.value;
|
|
3051
|
+
var command = parseCommand(listItem);
|
|
3052
|
+
switch (command.type) {
|
|
3053
|
+
case 'JOKER':
|
|
3054
|
+
jokers.push(command.parameterName);
|
|
3055
|
+
dependentParameterNames.add(command.parameterName);
|
|
3056
|
+
break;
|
|
3057
|
+
case 'EXECUTE':
|
|
3058
|
+
if (isExecutionTypeChanged) {
|
|
3059
|
+
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
3060
|
+
}
|
|
3061
|
+
executionType = command.executionType;
|
|
3062
|
+
isExecutionTypeChanged = true;
|
|
3063
|
+
break;
|
|
3064
|
+
case 'MODEL':
|
|
3065
|
+
templateModelRequirements[command.key] = command.value;
|
|
3066
|
+
break;
|
|
3067
|
+
case 'PARAMETER':
|
|
3068
|
+
// Note: This is just for detecting resulitng parameter name
|
|
3069
|
+
addParam(command);
|
|
3070
|
+
break;
|
|
3071
|
+
case 'POSTPROCESS':
|
|
3072
|
+
postprocessing.push(command.functionName);
|
|
3073
|
+
break;
|
|
3074
|
+
case 'EXPECT_AMOUNT':
|
|
3075
|
+
// eslint-disable-next-line no-case-declarations
|
|
3076
|
+
var unit = command.unit.toLowerCase();
|
|
3077
|
+
expectAmount[unit] = expectAmount[unit] || {};
|
|
3078
|
+
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
3079
|
+
if (expectAmount[unit].min !== undefined) {
|
|
3080
|
+
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3081
|
+
}
|
|
3082
|
+
expectAmount[unit].min = command.amount;
|
|
3083
|
+
} /* not else */
|
|
3084
|
+
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
3085
|
+
if (expectAmount[unit].max !== undefined) {
|
|
3086
|
+
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3087
|
+
}
|
|
3088
|
+
expectAmount[unit].max = command.amount;
|
|
3089
|
+
}
|
|
3090
|
+
break;
|
|
3091
|
+
case 'EXPECT_FORMAT':
|
|
3092
|
+
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
3093
|
+
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
3094
|
+
}
|
|
3095
|
+
expectFormat = command.format;
|
|
3096
|
+
break;
|
|
3097
|
+
default:
|
|
3098
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
3099
|
+
}
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
3103
|
+
finally {
|
|
3104
|
+
try {
|
|
3105
|
+
if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
|
|
3106
|
+
}
|
|
3107
|
+
finally { if (e_3) throw e_3.error; }
|
|
3108
|
+
}
|
|
3109
|
+
var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
|
|
3110
|
+
if (executionType === 'SCRIPT') {
|
|
3111
|
+
if (!language) {
|
|
3112
|
+
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
3113
|
+
}
|
|
3114
|
+
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
3115
|
+
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 "); }));
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
var lastLine = section.content.split('\n').pop();
|
|
3119
|
+
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
3120
|
+
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
3121
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
3122
|
+
// TODO: Show code of invalid sections each time + DRY
|
|
3123
|
+
section.content
|
|
3124
|
+
.split('\n')
|
|
3125
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3126
|
+
.join('\n')), "\n "); }));
|
|
3127
|
+
}
|
|
3128
|
+
var resultingParameterName = match.groups.resultingParamName;
|
|
3129
|
+
// TODO: [1] DRY description
|
|
3130
|
+
var description_1 = section.content;
|
|
3131
|
+
// Note: Remove codeblocks
|
|
3132
|
+
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
3133
|
+
//Note: Remove lists and return statement
|
|
3134
|
+
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
3135
|
+
description_1 = spaceTrim(description_1);
|
|
3136
|
+
if (description_1 === '') {
|
|
3137
|
+
description_1 = undefined;
|
|
3138
|
+
}
|
|
3139
|
+
if (Object.keys(jokers).length === 0) {
|
|
3140
|
+
jokers = undefined;
|
|
3141
|
+
}
|
|
3142
|
+
if (Object.keys(expectAmount).length === 0) {
|
|
3143
|
+
expectAmount = undefined;
|
|
3144
|
+
}
|
|
3145
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
3146
|
+
postprocessing = undefined;
|
|
3147
|
+
}
|
|
3148
|
+
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
3149
|
+
if (templateModelRequirements.modelVariant === undefined) {
|
|
3150
|
+
templateModelRequirements.modelVariant = 'CHAT';
|
|
3151
|
+
}
|
|
3152
|
+
promptbookJson.promptTemplates.push({
|
|
3153
|
+
name: titleToName(section.title),
|
|
3154
|
+
title: section.title,
|
|
3155
|
+
description: description_1,
|
|
3156
|
+
dependentParameterNames: Array.from(dependentParameterNames),
|
|
3157
|
+
executionType: executionType,
|
|
3158
|
+
jokers: jokers,
|
|
3159
|
+
postprocessing: postprocessing,
|
|
3160
|
+
expectations: expectAmount,
|
|
3161
|
+
expectFormat: expectFormat,
|
|
3162
|
+
modelRequirements: templateModelRequirements,
|
|
3163
|
+
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
3164
|
+
content: content,
|
|
3165
|
+
resultingParameterName: resultingParameterName,
|
|
3166
|
+
});
|
|
3167
|
+
};
|
|
3168
|
+
try {
|
|
3169
|
+
for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
3170
|
+
section = _b.value;
|
|
3171
|
+
_loop_1(section);
|
|
2028
3172
|
}
|
|
2029
3173
|
}
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
3174
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3175
|
+
finally {
|
|
3176
|
+
try {
|
|
3177
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
3178
|
+
}
|
|
3179
|
+
finally { if (e_2) throw e_2.error; }
|
|
2035
3180
|
}
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
}
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
// TODO: @deprecated remove
|
|
2042
|
-
commands_1.push("EXPECT JSON");
|
|
2043
|
-
}
|
|
2044
|
-
} /* not else */
|
|
2045
|
-
promptbookString += '\n\n';
|
|
2046
|
-
promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
2047
|
-
promptbookString += '\n\n';
|
|
2048
|
-
promptbookString += '```' + contentLanguage;
|
|
2049
|
-
promptbookString += '\n';
|
|
2050
|
-
promptbookString += spaceTrim$1(content);
|
|
2051
|
-
// <- TODO: !!! Escape
|
|
2052
|
-
// <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
|
|
2053
|
-
promptbookString += '\n';
|
|
2054
|
-
promptbookString += '```';
|
|
2055
|
-
promptbookString += '\n\n';
|
|
2056
|
-
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
2060
|
-
finally {
|
|
2061
|
-
try {
|
|
2062
|
-
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
|
|
2063
|
-
}
|
|
2064
|
-
finally { if (e_3) throw e_3.error; }
|
|
2065
|
-
}
|
|
2066
|
-
return promptbookString;
|
|
2067
|
-
}
|
|
2068
|
-
/**
|
|
2069
|
-
* @private internal util of promptbookJsonToString
|
|
2070
|
-
*/
|
|
2071
|
-
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
2072
|
-
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
2073
|
-
var parameterString = "{".concat(name, "}");
|
|
2074
|
-
if (description) {
|
|
2075
|
-
parameterString = "".concat(parameterString, " ").concat(description);
|
|
2076
|
-
}
|
|
2077
|
-
return parameterString;
|
|
3181
|
+
// =============================================================
|
|
3182
|
+
return [2 /*return*/, promptbookJson];
|
|
3183
|
+
}
|
|
3184
|
+
});
|
|
3185
|
+
});
|
|
2078
3186
|
}
|
|
2079
3187
|
/**
|
|
2080
|
-
* TODO:
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
/**
|
|
2084
|
-
* This error indicates that promptbook not found in the library
|
|
3188
|
+
* TODO: Report here line/column of error
|
|
3189
|
+
* TODO: Use spaceTrim more effectively
|
|
3190
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
2085
3191
|
*/
|
|
2086
|
-
var PromptbookNotFoundError = /** @class */ (function (_super) {
|
|
2087
|
-
__extends(PromptbookNotFoundError, _super);
|
|
2088
|
-
function PromptbookNotFoundError(message) {
|
|
2089
|
-
var _this = _super.call(this, message) || this;
|
|
2090
|
-
_this.name = 'PromptbookNotFoundError';
|
|
2091
|
-
Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
|
|
2092
|
-
return _this;
|
|
2093
|
-
}
|
|
2094
|
-
return PromptbookNotFoundError;
|
|
2095
|
-
}(Error));
|
|
2096
3192
|
|
|
2097
3193
|
/**
|
|
2098
|
-
* This error indicates
|
|
3194
|
+
* This error indicates that the promptbook library cannot be propperly loaded
|
|
2099
3195
|
*/
|
|
2100
|
-
var
|
|
2101
|
-
__extends(
|
|
2102
|
-
function
|
|
3196
|
+
var PromptbookLibraryError = /** @class */ (function (_super) {
|
|
3197
|
+
__extends(PromptbookLibraryError, _super);
|
|
3198
|
+
function PromptbookLibraryError(message) {
|
|
2103
3199
|
var _this = _super.call(this, message) || this;
|
|
2104
|
-
_this.name = '
|
|
2105
|
-
Object.setPrototypeOf(_this,
|
|
3200
|
+
_this.name = 'PromptbookLibraryError';
|
|
3201
|
+
Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
|
|
2106
3202
|
return _this;
|
|
2107
3203
|
}
|
|
2108
|
-
return
|
|
3204
|
+
return PromptbookLibraryError;
|
|
2109
3205
|
}(Error));
|
|
2110
3206
|
|
|
2111
3207
|
/**
|
|
2112
|
-
*
|
|
2113
|
-
* This implementation is a very thin wrapper around the Array / Map of promptbooks.
|
|
2114
|
-
*
|
|
2115
|
-
* @see https://github.com/webgptorg/promptbook#promptbook-library
|
|
3208
|
+
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
2116
3209
|
*/
|
|
2117
|
-
|
|
2118
|
-
/**
|
|
2119
|
-
* Constructs a promptbook library from promptbooks
|
|
2120
|
-
*
|
|
2121
|
-
* @param promptbooks !!!
|
|
2122
|
-
*
|
|
2123
|
-
* Note: During the construction logic of all promptbooks are validated
|
|
2124
|
-
* Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
|
|
2125
|
-
*/
|
|
2126
|
-
function SimplePromptbookLibrary() {
|
|
2127
|
-
var e_1, _a;
|
|
2128
|
-
var promptbooks = [];
|
|
2129
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2130
|
-
promptbooks[_i] = arguments[_i];
|
|
2131
|
-
}
|
|
2132
|
-
this.library = new Map();
|
|
2133
|
-
try {
|
|
2134
|
-
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
2135
|
-
var promptbook = promptbooks_1_1.value;
|
|
2136
|
-
if (promptbook.promptbookUrl === undefined) {
|
|
2137
|
-
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
2138
|
-
}
|
|
2139
|
-
validatePromptbookJson(promptbook);
|
|
2140
|
-
// Note: [🦄]
|
|
2141
|
-
if (this.library.has(promptbook.promptbookUrl) &&
|
|
2142
|
-
promptbookJsonToString(promptbook) !==
|
|
2143
|
-
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
2144
|
-
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
|
|
2145
|
-
}
|
|
2146
|
-
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
2147
|
-
}
|
|
2148
|
-
}
|
|
2149
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2150
|
-
finally {
|
|
2151
|
-
try {
|
|
2152
|
-
if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
|
|
2153
|
-
}
|
|
2154
|
-
finally { if (e_1) throw e_1.error; }
|
|
2155
|
-
}
|
|
2156
|
-
}
|
|
2157
|
-
/**
|
|
2158
|
-
* Gets all promptbooks in the library
|
|
2159
|
-
*/
|
|
2160
|
-
SimplePromptbookLibrary.prototype.listPromptbooks = function () {
|
|
2161
|
-
return Array.from(this.library.keys());
|
|
2162
|
-
};
|
|
2163
|
-
/**
|
|
2164
|
-
* Gets promptbook by its URL
|
|
2165
|
-
*
|
|
2166
|
-
* Note: This is not a direct fetching from the URL, but a lookup in the library
|
|
2167
|
-
*/
|
|
2168
|
-
SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
|
|
2169
|
-
var _this = this;
|
|
2170
|
-
var promptbook = this.library.get(url);
|
|
2171
|
-
if (!promptbook) {
|
|
2172
|
-
if (this.listPromptbooks().length === 0) {
|
|
2173
|
-
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
2174
|
-
}
|
|
2175
|
-
throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
2176
|
-
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
2177
|
-
.join('\n')), "\n\n "); }));
|
|
2178
|
-
}
|
|
2179
|
-
return promptbook;
|
|
2180
|
-
};
|
|
2181
|
-
/**
|
|
2182
|
-
* Checks whether given prompt was defined in any promptbook in the library
|
|
2183
|
-
*/
|
|
2184
|
-
SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
|
|
2185
|
-
return true;
|
|
2186
|
-
};
|
|
2187
|
-
return SimplePromptbookLibrary;
|
|
2188
|
-
}());
|
|
2189
|
-
|
|
3210
|
+
new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
|
|
2190
3211
|
/**
|
|
2191
|
-
*
|
|
2192
|
-
*
|
|
2193
|
-
* Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
|
|
2194
|
-
* Note: During the construction syntax and logic of all sources are validated
|
|
2195
|
-
*
|
|
2196
|
-
* @param promptbookSources
|
|
2197
|
-
* @returns PromptbookLibrary
|
|
3212
|
+
* Detects if the code is running in a Node.js environment
|
|
2198
3213
|
*/
|
|
2199
|
-
|
|
2200
|
-
var promptbookSources = [];
|
|
2201
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2202
|
-
promptbookSources[_i] = arguments[_i];
|
|
2203
|
-
}
|
|
2204
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2205
|
-
var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
|
|
2206
|
-
var e_1, _a;
|
|
2207
|
-
return __generator(this, function (_b) {
|
|
2208
|
-
switch (_b.label) {
|
|
2209
|
-
case 0:
|
|
2210
|
-
promptbooks = new Array();
|
|
2211
|
-
_b.label = 1;
|
|
2212
|
-
case 1:
|
|
2213
|
-
_b.trys.push([1, 8, 9, 10]);
|
|
2214
|
-
promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
|
|
2215
|
-
_b.label = 2;
|
|
2216
|
-
case 2:
|
|
2217
|
-
if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
|
|
2218
|
-
source = promptbookSources_1_1.value;
|
|
2219
|
-
promptbook = void 0;
|
|
2220
|
-
if (!(typeof source === 'string')) return [3 /*break*/, 4];
|
|
2221
|
-
return [4 /*yield*/, promptbookStringToJson(source)];
|
|
2222
|
-
case 3:
|
|
2223
|
-
// Note: When directly creating from string, no need to validate the source
|
|
2224
|
-
// The validation is performed always before execution
|
|
2225
|
-
promptbook = _b.sent();
|
|
2226
|
-
return [3 /*break*/, 5];
|
|
2227
|
-
case 4:
|
|
2228
|
-
promptbook = source;
|
|
2229
|
-
_b.label = 5;
|
|
2230
|
-
case 5:
|
|
2231
|
-
promptbooks.push(promptbook);
|
|
2232
|
-
_b.label = 6;
|
|
2233
|
-
case 6:
|
|
2234
|
-
promptbookSources_1_1 = promptbookSources_1.next();
|
|
2235
|
-
return [3 /*break*/, 2];
|
|
2236
|
-
case 7: return [3 /*break*/, 10];
|
|
2237
|
-
case 8:
|
|
2238
|
-
e_1_1 = _b.sent();
|
|
2239
|
-
e_1 = { error: e_1_1 };
|
|
2240
|
-
return [3 /*break*/, 10];
|
|
2241
|
-
case 9:
|
|
2242
|
-
try {
|
|
2243
|
-
if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
|
|
2244
|
-
}
|
|
2245
|
-
finally { if (e_1) throw e_1.error; }
|
|
2246
|
-
return [7 /*endfinally*/];
|
|
2247
|
-
case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
|
|
2248
|
-
}
|
|
2249
|
-
});
|
|
2250
|
-
});
|
|
2251
|
-
}
|
|
3214
|
+
var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
2252
3215
|
/**
|
|
2253
|
-
*
|
|
3216
|
+
* Detects if the code is running in a web worker
|
|
2254
3217
|
*/
|
|
3218
|
+
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");
|
|
2255
3219
|
|
|
2256
3220
|
/**
|
|
2257
3221
|
* Constructs Promptbook from async sources
|