@promptbook/pdf 0.78.0-0 → 0.78.3
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/README.md +0 -4
- package/esm/index.es.js +246 -192
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +4 -0
- package/esm/typings/src/_packages/types.index.d.ts +0 -2
- package/esm/typings/src/commands/_common/types/CommandType.d.ts +1 -1
- package/esm/typings/src/config.d.ts +14 -0
- package/esm/typings/src/errors/utils/getErrorReportUrl.d.ts +6 -0
- package/esm/typings/src/execution/execution-report/ExecutionReportString.d.ts +1 -1
- package/esm/typings/src/llm-providers/openai/openai-models.d.ts +1 -1
- package/esm/typings/src/pipeline/PipelineString.d.ts +1 -1
- package/package.json +2 -2
- package/umd/index.umd.js +246 -192
- package/umd/index.umd.js.map +1 -1
|
@@ -5,6 +5,8 @@ import { createCollectionFromPromise } from '../collection/constructors/createCo
|
|
|
5
5
|
import { createCollectionFromUrl } from '../collection/constructors/createCollectionFromUrl';
|
|
6
6
|
import { createSubcollection } from '../collection/constructors/createSubcollection';
|
|
7
7
|
import { NAME } from '../config';
|
|
8
|
+
import { ADMIN_EMAIL } from '../config';
|
|
9
|
+
import { ADMIN_GITHUB_NAME } from '../config';
|
|
8
10
|
import { CLAIM } from '../config';
|
|
9
11
|
import { LOGO_LIGHT_SRC } from '../config';
|
|
10
12
|
import { LOGO_DARK_SRC } from '../config';
|
|
@@ -123,6 +125,8 @@ export { createCollectionFromPromise };
|
|
|
123
125
|
export { createCollectionFromUrl };
|
|
124
126
|
export { createSubcollection };
|
|
125
127
|
export { NAME };
|
|
128
|
+
export { ADMIN_EMAIL };
|
|
129
|
+
export { ADMIN_GITHUB_NAME };
|
|
126
130
|
export { CLAIM };
|
|
127
131
|
export { LOGO_LIGHT_SRC };
|
|
128
132
|
export { LOGO_DARK_SRC };
|
|
@@ -6,7 +6,6 @@ import type { PipelineHeadCommandParser } from '../commands/_common/types/Comman
|
|
|
6
6
|
import type { PipelineTaskCommandParser } from '../commands/_common/types/CommandParser';
|
|
7
7
|
import type { CommandParserInput } from '../commands/_common/types/CommandParser';
|
|
8
8
|
import type { CommandType } from '../commands/_common/types/CommandType';
|
|
9
|
-
import type { CommandTypeOrAlias } from '../commands/_common/types/CommandType';
|
|
10
9
|
import type { CommandUsagePlace } from '../commands/_common/types/CommandUsagePlaces';
|
|
11
10
|
import type { ExpectCommand } from '../commands/EXPECT/ExpectCommand';
|
|
12
11
|
import type { ForeachJson } from '../commands/FOREACH/ForeachJson';
|
|
@@ -266,7 +265,6 @@ export type { PipelineHeadCommandParser };
|
|
|
266
265
|
export type { PipelineTaskCommandParser };
|
|
267
266
|
export type { CommandParserInput };
|
|
268
267
|
export type { CommandType };
|
|
269
|
-
export type { CommandTypeOrAlias };
|
|
270
268
|
export type { CommandUsagePlace };
|
|
271
269
|
export type { ExpectCommand };
|
|
272
270
|
export type { ForeachJson };
|
|
@@ -12,6 +12,6 @@ export type CommandType = Command['type'];
|
|
|
12
12
|
*
|
|
13
13
|
* This is a type of the command like "KNOWLEDGE" or "PERSONA"
|
|
14
14
|
*
|
|
15
|
-
|
|
15
|
+
ex--rt type CommandTypeOrAlias = Command['type'] | Command['aliasNames'] | Command['deprecatedNames'];
|
|
16
16
|
// <- TODO: [🧘] Implement
|
|
17
17
|
*/
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { CsvSettings } from './formats/csv/CsvSettings';
|
|
2
2
|
import type { IntermediateFilesStrategy } from './types/IntermediateFilesStrategy';
|
|
3
|
+
import type { string_email } from './types/typeAliases';
|
|
4
|
+
import type { string_name } from './types/typeAliases';
|
|
3
5
|
import type { string_url_image } from './types/typeAliases';
|
|
4
6
|
/**
|
|
5
7
|
* Warning message for the generated sections and files files
|
|
@@ -15,6 +17,18 @@ export declare const GENERATOR_WARNING = "\u26A0\uFE0F WARNING: This code has be
|
|
|
15
17
|
* @public exported from `@promptbook/core`
|
|
16
18
|
*/
|
|
17
19
|
export declare const NAME = "Promptbook";
|
|
20
|
+
/**
|
|
21
|
+
* Email of the responsible person
|
|
22
|
+
*
|
|
23
|
+
* @public exported from `@promptbook/core`
|
|
24
|
+
*/
|
|
25
|
+
export declare const ADMIN_EMAIL: string_email;
|
|
26
|
+
/**
|
|
27
|
+
* Name of the responsible person for the Promptbook on GitHub
|
|
28
|
+
*
|
|
29
|
+
* @public exported from `@promptbook/core`
|
|
30
|
+
*/
|
|
31
|
+
export declare const ADMIN_GITHUB_NAME: string_name;
|
|
18
32
|
/**
|
|
19
33
|
* Claim for the Promptbook
|
|
20
34
|
*
|
|
@@ -24,7 +24,7 @@ export declare const OPENAI_MODELS: ReadonlyArray<AvailableModel & {
|
|
|
24
24
|
* @see https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4
|
|
25
25
|
* @see https://openai.com/api/pricing/
|
|
26
26
|
* @see /other/playground/playground.ts
|
|
27
|
-
* TODO: [🍓] Make better
|
|
27
|
+
* TODO: [🍓][💩] Make better
|
|
28
28
|
* TODO: Change model titles to human eg: "gpt-4-turbo-2024-04-09" -> "GPT-4 Turbo (2024-04-09)"
|
|
29
29
|
* TODO: [🚸] Not all models are compatible with JSON mode, add this information here and use it
|
|
30
30
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -8,5 +8,5 @@ export type PipelineString = string & {
|
|
|
8
8
|
readonly _type: 'Promptbook';
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
|
-
* TODO:
|
|
11
|
+
* TODO: [💩] Better validation (validatePipelineString) or remove branded type and make it just string
|
|
12
12
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promptbook/pdf",
|
|
3
|
-
"version": "0.78.
|
|
3
|
+
"version": "0.78.3",
|
|
4
4
|
"description": "It's time for a paradigm shift. The future of software in plain English, French or Latin",
|
|
5
5
|
"--note-0": " <- [🐊]",
|
|
6
6
|
"private": false,
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"module": "./esm/index.es.js",
|
|
55
55
|
"typings": "./esm/typings/src/_packages/pdf.index.d.ts",
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@promptbook/core": "0.78.
|
|
57
|
+
"@promptbook/core": "0.78.3"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"crypto-js": "4.2.0",
|
package/umd/index.umd.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
*
|
|
23
23
|
* @see https://github.com/webgptorg/promptbook
|
|
24
24
|
*/
|
|
25
|
-
var PROMPTBOOK_ENGINE_VERSION = '0.
|
|
25
|
+
var PROMPTBOOK_ENGINE_VERSION = '0.78.2';
|
|
26
26
|
/**
|
|
27
27
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
28
28
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -448,198 +448,25 @@
|
|
|
448
448
|
}
|
|
449
449
|
|
|
450
450
|
/**
|
|
451
|
-
*
|
|
451
|
+
* Name for the Promptbook
|
|
452
452
|
*
|
|
453
|
-
*
|
|
454
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
455
|
-
*
|
|
456
|
-
* @returns The same object as the input, but deeply frozen
|
|
457
|
-
* @public exported from `@promptbook/utils`
|
|
458
|
-
*/
|
|
459
|
-
function $deepFreeze(objectValue) {
|
|
460
|
-
var e_1, _a;
|
|
461
|
-
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
462
|
-
try {
|
|
463
|
-
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
464
|
-
var propertyName = propertyNames_1_1.value;
|
|
465
|
-
var value = objectValue[propertyName];
|
|
466
|
-
if (value && typeof value === 'object') {
|
|
467
|
-
$deepFreeze(value);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
472
|
-
finally {
|
|
473
|
-
try {
|
|
474
|
-
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
475
|
-
}
|
|
476
|
-
finally { if (e_1) throw e_1.error; }
|
|
477
|
-
}
|
|
478
|
-
return Object.freeze(objectValue);
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
482
|
-
*/
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
453
|
+
* TODO: [🗽] Unite branding and make single place for it
|
|
486
454
|
*
|
|
487
455
|
* @public exported from `@promptbook/core`
|
|
488
456
|
*/
|
|
489
|
-
var
|
|
490
|
-
__extends(UnexpectedError, _super);
|
|
491
|
-
function UnexpectedError(message) {
|
|
492
|
-
var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
|
|
493
|
-
_this.name = 'UnexpectedError';
|
|
494
|
-
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
495
|
-
return _this;
|
|
496
|
-
}
|
|
497
|
-
return UnexpectedError;
|
|
498
|
-
}(Error));
|
|
499
|
-
|
|
457
|
+
var NAME = "Promptbook";
|
|
500
458
|
/**
|
|
501
|
-
*
|
|
502
|
-
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
459
|
+
* Email of the responsible person
|
|
503
460
|
*
|
|
504
|
-
*
|
|
505
|
-
* - `undefined` is not serializable
|
|
506
|
-
* - `NaN` is not serializable
|
|
507
|
-
* - Objects and arrays are serializable if all their properties are serializable
|
|
508
|
-
* - Functions are not serializable
|
|
509
|
-
* - Circular references are not serializable
|
|
510
|
-
* - `Date` objects are not serializable
|
|
511
|
-
* - `Map` and `Set` objects are not serializable
|
|
512
|
-
* - `RegExp` objects are not serializable
|
|
513
|
-
* - `Error` objects are not serializable
|
|
514
|
-
* - `Symbol` objects are not serializable
|
|
515
|
-
* - And much more...
|
|
516
|
-
*
|
|
517
|
-
* @throws UnexpectedError if the value is not serializable as JSON
|
|
518
|
-
* @public exported from `@promptbook/utils`
|
|
519
|
-
*/
|
|
520
|
-
function checkSerializableAsJson(name, value) {
|
|
521
|
-
var e_1, _a;
|
|
522
|
-
if (value === undefined) {
|
|
523
|
-
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
524
|
-
}
|
|
525
|
-
else if (value === null) {
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
else if (typeof value === 'boolean') {
|
|
529
|
-
return;
|
|
530
|
-
}
|
|
531
|
-
else if (typeof value === 'number' && !isNaN(value)) {
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
else if (typeof value === 'string') {
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
else if (typeof value === 'symbol') {
|
|
538
|
-
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
539
|
-
}
|
|
540
|
-
else if (typeof value === 'function') {
|
|
541
|
-
throw new UnexpectedError("".concat(name, " is function"));
|
|
542
|
-
}
|
|
543
|
-
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
544
|
-
for (var i = 0; i < value.length; i++) {
|
|
545
|
-
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
else if (typeof value === 'object') {
|
|
549
|
-
if (value instanceof Date) {
|
|
550
|
-
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
551
|
-
}
|
|
552
|
-
else if (value instanceof Map) {
|
|
553
|
-
throw new UnexpectedError("".concat(name, " is Map"));
|
|
554
|
-
}
|
|
555
|
-
else if (value instanceof Set) {
|
|
556
|
-
throw new UnexpectedError("".concat(name, " is Set"));
|
|
557
|
-
}
|
|
558
|
-
else if (value instanceof RegExp) {
|
|
559
|
-
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
560
|
-
}
|
|
561
|
-
else if (value instanceof Error) {
|
|
562
|
-
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
563
|
-
}
|
|
564
|
-
else {
|
|
565
|
-
try {
|
|
566
|
-
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
567
|
-
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
568
|
-
if (subValue === undefined) {
|
|
569
|
-
// Note: undefined in object is serializable - it is just omited
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
576
|
-
finally {
|
|
577
|
-
try {
|
|
578
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
579
|
-
}
|
|
580
|
-
finally { if (e_1) throw e_1.error; }
|
|
581
|
-
}
|
|
582
|
-
try {
|
|
583
|
-
JSON.stringify(value); // <- TODO: [0]
|
|
584
|
-
}
|
|
585
|
-
catch (error) {
|
|
586
|
-
if (!(error instanceof Error)) {
|
|
587
|
-
throw error;
|
|
588
|
-
}
|
|
589
|
-
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
590
|
-
}
|
|
591
|
-
/*
|
|
592
|
-
TODO: [0] Is there some more elegant way to check circular references?
|
|
593
|
-
const seen = new Set();
|
|
594
|
-
const stack = [{ value }];
|
|
595
|
-
while (stack.length > 0) {
|
|
596
|
-
const { value } = stack.pop()!;
|
|
597
|
-
if (typeof value === 'object' && value !== null) {
|
|
598
|
-
if (seen.has(value)) {
|
|
599
|
-
throw new UnexpectedError(`${name} has circular reference`);
|
|
600
|
-
}
|
|
601
|
-
seen.add(value);
|
|
602
|
-
if (Array.isArray(value)) {
|
|
603
|
-
stack.push(...value.map((value) => ({ value })));
|
|
604
|
-
} else {
|
|
605
|
-
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
*/
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
else {
|
|
614
|
-
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
619
|
-
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
620
|
-
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
461
|
+
* @public exported from `@promptbook/core`
|
|
621
462
|
*/
|
|
622
|
-
|
|
463
|
+
var ADMIN_EMAIL = 'me@pavolhejny.com';
|
|
623
464
|
/**
|
|
624
|
-
*
|
|
625
|
-
* @@@
|
|
626
|
-
*
|
|
627
|
-
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
465
|
+
* Name of the responsible person for the Promptbook on GitHub
|
|
628
466
|
*
|
|
629
|
-
* @
|
|
630
|
-
* @param objectValue - Object to be deeply frozen
|
|
631
|
-
* @returns The same object as the input, but deeply frozen
|
|
632
|
-
* @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
|
|
633
|
-
*/
|
|
634
|
-
function $asDeeplyFrozenSerializableJson(name, objectValue) {
|
|
635
|
-
checkSerializableAsJson(name, objectValue);
|
|
636
|
-
return $deepFreeze(objectValue);
|
|
637
|
-
}
|
|
638
|
-
/**
|
|
639
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
640
|
-
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
467
|
+
* @public exported from `@promptbook/core`
|
|
641
468
|
*/
|
|
642
|
-
|
|
469
|
+
var ADMIN_GITHUB_NAME = 'hejny';
|
|
643
470
|
// <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
|
|
644
471
|
/**
|
|
645
472
|
* The maximum number of iterations for a loops
|
|
@@ -691,7 +518,8 @@
|
|
|
691
518
|
*
|
|
692
519
|
* @public exported from `@promptbook/core`
|
|
693
520
|
*/
|
|
694
|
-
var RESERVED_PARAMETER_NAMES =
|
|
521
|
+
var RESERVED_PARAMETER_NAMES =
|
|
522
|
+
/* !!!!!! $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', _____ as const); */ [
|
|
695
523
|
'content',
|
|
696
524
|
'context',
|
|
697
525
|
'knowledge',
|
|
@@ -701,7 +529,7 @@
|
|
|
701
529
|
// <- TODO: list here all command names
|
|
702
530
|
// <- TODO: Add more like 'date', 'modelName',...
|
|
703
531
|
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
704
|
-
]
|
|
532
|
+
];
|
|
705
533
|
/**
|
|
706
534
|
* @@@
|
|
707
535
|
*
|
|
@@ -788,6 +616,40 @@
|
|
|
788
616
|
return PipelineLogicError;
|
|
789
617
|
}(Error));
|
|
790
618
|
|
|
619
|
+
/**
|
|
620
|
+
* Make error report URL for the given error
|
|
621
|
+
*
|
|
622
|
+
* @private !!!!!!
|
|
623
|
+
*/
|
|
624
|
+
function getErrorReportUrl(error) {
|
|
625
|
+
var report = {
|
|
626
|
+
title: "\uD83D\uDC1C Error report from ".concat(NAME),
|
|
627
|
+
body: spaceTrim__default["default"](function (block) { return "\n\n\n `".concat(error.name || 'Error', "` has occurred in the [").concat(NAME, "], please look into it @").concat(ADMIN_GITHUB_NAME, ".\n\n ```\n ").concat(block(error.message || '(no error message)'), "\n ```\n\n\n ## More info:\n\n - **Promptbook engine version:** ").concat(PROMPTBOOK_ENGINE_VERSION, "\n - **Book language version:** ").concat(BOOK_LANGUAGE_VERSION, "\n - **Time:** ").concat(new Date().toISOString(), "\n\n <details>\n <summary>Stack trace:</summary>\n\n ## Stack trace:\n\n ```stacktrace\n ").concat(block(error.stack || '(empty)'), "\n ```\n </details>\n\n "); }),
|
|
628
|
+
};
|
|
629
|
+
var reportUrl = new URL("https://github.com/webgptorg/promptbook/issues/new");
|
|
630
|
+
reportUrl.searchParams.set('labels', 'bug');
|
|
631
|
+
reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
|
|
632
|
+
reportUrl.searchParams.set('title', report.title);
|
|
633
|
+
reportUrl.searchParams.set('body', report.body);
|
|
634
|
+
return reportUrl;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
639
|
+
*
|
|
640
|
+
* @public exported from `@promptbook/core`
|
|
641
|
+
*/
|
|
642
|
+
var UnexpectedError = /** @class */ (function (_super) {
|
|
643
|
+
__extends(UnexpectedError, _super);
|
|
644
|
+
function UnexpectedError(message) {
|
|
645
|
+
var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n ").concat(block(getErrorReportUrl(new Error(message)).href), "\n\n Or contact us on ").concat(ADMIN_EMAIL, "\n\n "); })) || this;
|
|
646
|
+
_this.name = 'UnexpectedError';
|
|
647
|
+
Object.setPrototypeOf(_this, UnexpectedError.prototype);
|
|
648
|
+
return _this;
|
|
649
|
+
}
|
|
650
|
+
return UnexpectedError;
|
|
651
|
+
}(Error));
|
|
652
|
+
|
|
791
653
|
/**
|
|
792
654
|
* Tests if given string is valid semantic version
|
|
793
655
|
*
|
|
@@ -1268,6 +1130,183 @@
|
|
|
1268
1130
|
return parameterNames;
|
|
1269
1131
|
}
|
|
1270
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* @@@
|
|
1135
|
+
*
|
|
1136
|
+
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
1137
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1138
|
+
*
|
|
1139
|
+
* @returns The same object as the input, but deeply frozen
|
|
1140
|
+
* @public exported from `@promptbook/utils`
|
|
1141
|
+
*/
|
|
1142
|
+
function $deepFreeze(objectValue) {
|
|
1143
|
+
var e_1, _a;
|
|
1144
|
+
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
1145
|
+
try {
|
|
1146
|
+
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
1147
|
+
var propertyName = propertyNames_1_1.value;
|
|
1148
|
+
var value = objectValue[propertyName];
|
|
1149
|
+
if (value && typeof value === 'object') {
|
|
1150
|
+
$deepFreeze(value);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1155
|
+
finally {
|
|
1156
|
+
try {
|
|
1157
|
+
if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
|
|
1158
|
+
}
|
|
1159
|
+
finally { if (e_1) throw e_1.error; }
|
|
1160
|
+
}
|
|
1161
|
+
return Object.freeze(objectValue);
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1165
|
+
*/
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* Checks if the value is [🚉] serializable as JSON
|
|
1169
|
+
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
1170
|
+
*
|
|
1171
|
+
* - Almost all primitives are serializable BUT:
|
|
1172
|
+
* - `undefined` is not serializable
|
|
1173
|
+
* - `NaN` is not serializable
|
|
1174
|
+
* - Objects and arrays are serializable if all their properties are serializable
|
|
1175
|
+
* - Functions are not serializable
|
|
1176
|
+
* - Circular references are not serializable
|
|
1177
|
+
* - `Date` objects are not serializable
|
|
1178
|
+
* - `Map` and `Set` objects are not serializable
|
|
1179
|
+
* - `RegExp` objects are not serializable
|
|
1180
|
+
* - `Error` objects are not serializable
|
|
1181
|
+
* - `Symbol` objects are not serializable
|
|
1182
|
+
* - And much more...
|
|
1183
|
+
*
|
|
1184
|
+
* @throws UnexpectedError if the value is not serializable as JSON
|
|
1185
|
+
* @public exported from `@promptbook/utils`
|
|
1186
|
+
*/
|
|
1187
|
+
function checkSerializableAsJson(name, value) {
|
|
1188
|
+
var e_1, _a;
|
|
1189
|
+
if (value === undefined) {
|
|
1190
|
+
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
1191
|
+
}
|
|
1192
|
+
else if (value === null) {
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
else if (typeof value === 'boolean') {
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
else if (typeof value === 'number' && !isNaN(value)) {
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
else if (typeof value === 'string') {
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
else if (typeof value === 'symbol') {
|
|
1205
|
+
throw new UnexpectedError("".concat(name, " is symbol"));
|
|
1206
|
+
}
|
|
1207
|
+
else if (typeof value === 'function') {
|
|
1208
|
+
throw new UnexpectedError("".concat(name, " is function"));
|
|
1209
|
+
}
|
|
1210
|
+
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
1211
|
+
for (var i = 0; i < value.length; i++) {
|
|
1212
|
+
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
else if (typeof value === 'object') {
|
|
1216
|
+
if (value instanceof Date) {
|
|
1217
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
|
|
1218
|
+
}
|
|
1219
|
+
else if (value instanceof Map) {
|
|
1220
|
+
throw new UnexpectedError("".concat(name, " is Map"));
|
|
1221
|
+
}
|
|
1222
|
+
else if (value instanceof Set) {
|
|
1223
|
+
throw new UnexpectedError("".concat(name, " is Set"));
|
|
1224
|
+
}
|
|
1225
|
+
else if (value instanceof RegExp) {
|
|
1226
|
+
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
1227
|
+
}
|
|
1228
|
+
else if (value instanceof Error) {
|
|
1229
|
+
throw new UnexpectedError(spaceTrim__default["default"]("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
try {
|
|
1233
|
+
for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1234
|
+
var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
|
|
1235
|
+
if (subValue === undefined) {
|
|
1236
|
+
// Note: undefined in object is serializable - it is just omited
|
|
1237
|
+
continue;
|
|
1238
|
+
}
|
|
1239
|
+
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1243
|
+
finally {
|
|
1244
|
+
try {
|
|
1245
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1246
|
+
}
|
|
1247
|
+
finally { if (e_1) throw e_1.error; }
|
|
1248
|
+
}
|
|
1249
|
+
try {
|
|
1250
|
+
JSON.stringify(value); // <- TODO: [0]
|
|
1251
|
+
}
|
|
1252
|
+
catch (error) {
|
|
1253
|
+
if (!(error instanceof Error)) {
|
|
1254
|
+
throw error;
|
|
1255
|
+
}
|
|
1256
|
+
throw new UnexpectedError(spaceTrim__default["default"](function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
1257
|
+
}
|
|
1258
|
+
/*
|
|
1259
|
+
TODO: [0] Is there some more elegant way to check circular references?
|
|
1260
|
+
const seen = new Set();
|
|
1261
|
+
const stack = [{ value }];
|
|
1262
|
+
while (stack.length > 0) {
|
|
1263
|
+
const { value } = stack.pop()!;
|
|
1264
|
+
if (typeof value === 'object' && value !== null) {
|
|
1265
|
+
if (seen.has(value)) {
|
|
1266
|
+
throw new UnexpectedError(`${name} has circular reference`);
|
|
1267
|
+
}
|
|
1268
|
+
seen.add(value);
|
|
1269
|
+
if (Array.isArray(value)) {
|
|
1270
|
+
stack.push(...value.map((value) => ({ value })));
|
|
1271
|
+
} else {
|
|
1272
|
+
stack.push(...Object.values(value).map((value) => ({ value })));
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
*/
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
else {
|
|
1281
|
+
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1286
|
+
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
1287
|
+
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
1288
|
+
*/
|
|
1289
|
+
|
|
1290
|
+
/**
|
|
1291
|
+
* @@@
|
|
1292
|
+
* @@@
|
|
1293
|
+
*
|
|
1294
|
+
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
1295
|
+
*
|
|
1296
|
+
* @param name - Name of the object for debugging purposes
|
|
1297
|
+
* @param objectValue - Object to be deeply frozen
|
|
1298
|
+
* @returns The same object as the input, but deeply frozen
|
|
1299
|
+
* @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
|
|
1300
|
+
*/
|
|
1301
|
+
function $asDeeplyFrozenSerializableJson(name, objectValue) {
|
|
1302
|
+
checkSerializableAsJson(name, objectValue);
|
|
1303
|
+
return $deepFreeze(objectValue);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
1307
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
1308
|
+
*/
|
|
1309
|
+
|
|
1271
1310
|
/**
|
|
1272
1311
|
* Unprepare just strips the preparation data of the pipeline
|
|
1273
1312
|
*
|
|
@@ -3565,6 +3604,7 @@
|
|
|
3565
3604
|
*/
|
|
3566
3605
|
function extractVariablesFromScript(script) {
|
|
3567
3606
|
var variables = new Set();
|
|
3607
|
+
var originalScript = script;
|
|
3568
3608
|
script = "(()=>{".concat(script, "})()");
|
|
3569
3609
|
try {
|
|
3570
3610
|
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
@@ -3575,20 +3615,32 @@
|
|
|
3575
3615
|
if (!(error instanceof ReferenceError)) {
|
|
3576
3616
|
throw error;
|
|
3577
3617
|
}
|
|
3578
|
-
var undefinedName = error.message.split(' ')[0];
|
|
3579
3618
|
/*
|
|
3580
3619
|
Note: Parsing the error
|
|
3620
|
+
🌟 Most devices:
|
|
3581
3621
|
[PipelineUrlError: thing is not defined]
|
|
3622
|
+
|
|
3623
|
+
🍏 iPhone`s Safari:
|
|
3624
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
3582
3625
|
*/
|
|
3583
|
-
|
|
3626
|
+
var variableName = undefined;
|
|
3627
|
+
if (error.message.startsWith("Can't")) {
|
|
3628
|
+
// 🍏 Case
|
|
3629
|
+
variableName = error.message.split(' ').pop();
|
|
3630
|
+
}
|
|
3631
|
+
else {
|
|
3632
|
+
// 🌟 Case
|
|
3633
|
+
variableName = error.message.split(' ').shift();
|
|
3634
|
+
}
|
|
3635
|
+
if (variableName === undefined) {
|
|
3584
3636
|
throw error;
|
|
3585
3637
|
}
|
|
3586
|
-
if (script.includes(
|
|
3587
|
-
script = "const ".concat(
|
|
3638
|
+
if (script.includes(variableName + '(')) {
|
|
3639
|
+
script = "const ".concat(variableName, " = ()=>'';") + script;
|
|
3588
3640
|
}
|
|
3589
3641
|
else {
|
|
3590
|
-
variables.add(
|
|
3591
|
-
script = "const ".concat(
|
|
3642
|
+
variables.add(variableName);
|
|
3643
|
+
script = "const ".concat(variableName, " = '';") + script;
|
|
3592
3644
|
}
|
|
3593
3645
|
}
|
|
3594
3646
|
}
|
|
@@ -3596,7 +3648,9 @@
|
|
|
3596
3648
|
if (!(error instanceof Error)) {
|
|
3597
3649
|
throw error;
|
|
3598
3650
|
}
|
|
3599
|
-
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n
|
|
3651
|
+
throw new ParseError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n\n\n Found variables:\n\n ").concat(Array.from(variables)
|
|
3652
|
+
.map(function (variableName, i) { return "".concat(i + 1, ") ").concat(variableName); })
|
|
3653
|
+
.join('\n'), "\n\n\n The script:\n\n ```javascript\n ").concat(block(originalScript), "\n ```\n "); }));
|
|
3600
3654
|
}
|
|
3601
3655
|
return variables;
|
|
3602
3656
|
}
|