@promptbook/core 0.40.0-2 → 0.40.0-5

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 CHANGED
@@ -420,7 +420,7 @@ function removeContentComments(content) {
420
420
  /**
421
421
  * The version of the Promptbook library
422
422
  */
423
- var PROMPTBOOK_VERSION = '0.40.0-1';
423
+ var PROMPTBOOK_VERSION = '0.40.0-3';
424
424
 
425
425
  /**
426
426
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -2078,62 +2078,64 @@ var NotFoundError = /** @class */ (function (_super) {
2078
2078
  return NotFoundError;
2079
2079
  }(Error));
2080
2080
 
2081
+ /**
2082
+ * This error indicates errors in referencing promptbooks between each other
2083
+ */
2084
+ var PromptbookReferenceError = /** @class */ (function (_super) {
2085
+ __extends(PromptbookReferenceError, _super);
2086
+ function PromptbookReferenceError(message) {
2087
+ var _this = _super.call(this, message) || this;
2088
+ _this.name = 'PromptbookReferenceError';
2089
+ Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
2090
+ return _this;
2091
+ }
2092
+ return PromptbookReferenceError;
2093
+ }(Error));
2094
+
2081
2095
  /**
2082
2096
  * Library of promptbooks that groups together promptbooks for an application.
2083
- * This implementation is a very thin wrapper around the Array / Set of promptbooks.
2097
+ * This implementation is a very thin wrapper around the Array / Map of promptbooks.
2084
2098
  *
2085
2099
  * @see https://github.com/webgptorg/promptbook#promptbook-library
2086
2100
  */
2087
2101
  var SimplePromptbookLibrary = /** @class */ (function () {
2088
- function SimplePromptbookLibrary(options) {
2089
- this.options = options;
2090
- }
2091
- /**
2092
- * Constructs Promptbook from any sources
2102
+ /**!!!
2093
2103
  *
2094
- * Note: During the construction syntax and logic of all sources are validated
2095
- * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
2104
+ * @param promptbooks !!!
2096
2105
  *
2097
- * @param promptbookSources contents of .ptbk.md or .ptbk.json files
2098
- * @param settings settings for creating executor functions
2099
- * @returns PromptbookLibrary
2106
+ * Note: During the construction logic of all promptbooks are validated
2107
+ * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
2100
2108
  */
2101
- SimplePromptbookLibrary.fromSources = function (promptbookSources, settings) {
2109
+ function SimplePromptbookLibrary() {
2102
2110
  var e_1, _a;
2103
- var library = {};
2111
+ var promptbooks = [];
2112
+ for (var _i = 0; _i < arguments.length; _i++) {
2113
+ promptbooks[_i] = arguments[_i];
2114
+ }
2115
+ this.library = new Map();
2104
2116
  try {
2105
- for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2106
- var source = promptbookSources_1_1.value;
2107
- var promptbook = void 0;
2108
- if (typeof source === 'string') {
2109
- // Note: When directly creating from string, no need to validate the source
2110
- // The validation is performed always before execution
2111
- promptbook = promptbookStringToJson(source);
2112
- }
2113
- else {
2114
- promptbook = source;
2115
- }
2116
- validatePromptbookJson(promptbook);
2117
+ for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
2118
+ var promptbook = promptbooks_1_1.value;
2117
2119
  if (promptbook.promptbookUrl === undefined) {
2118
- throw new Error(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 ")));
2120
+ 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 ")));
2119
2121
  }
2120
- library[promptbook.promptbookUrl] = promptbook;
2122
+ validatePromptbookJson(promptbook);
2123
+ this.library.set(promptbook.promptbookUrl, promptbook);
2121
2124
  }
2122
2125
  }
2123
2126
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2124
2127
  finally {
2125
2128
  try {
2126
- if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2129
+ if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
2127
2130
  }
2128
2131
  finally { if (e_1) throw e_1.error; }
2129
2132
  }
2130
- return new SimplePromptbookLibrary({ library: library, settings: settings });
2131
- };
2133
+ }
2132
2134
  /**
2133
2135
  * Gets all promptbooks in the library
2134
2136
  */
2135
2137
  SimplePromptbookLibrary.prototype.listPromptbooks = function () {
2136
- return Object.keys(this.options.library);
2138
+ return Array.from(this.library.keys());
2137
2139
  };
2138
2140
  /**
2139
2141
  * Gets promptbook by its URL
@@ -2142,7 +2144,7 @@ var SimplePromptbookLibrary = /** @class */ (function () {
2142
2144
  */
2143
2145
  SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
2144
2146
  var _this = this;
2145
- var promptbook = this.options.library[url];
2147
+ var promptbook = this.library.get(url);
2146
2148
  if (!promptbook) {
2147
2149
  throw new NotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
2148
2150
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
@@ -2159,5 +2161,190 @@ var SimplePromptbookLibrary = /** @class */ (function () {
2159
2161
  return SimplePromptbookLibrary;
2160
2162
  }());
2161
2163
 
2162
- export { CallbackInterfaceTools, ExecutionTypes, MockedEchoNaturalExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, createPromptbookExecutor, promptbookStringToJson, validatePromptbookJson };
2164
+ function createPromptbookLibraryFromSources() {
2165
+ var e_1, _a;
2166
+ var promptbookSources = [];
2167
+ for (var _i = 0; _i < arguments.length; _i++) {
2168
+ promptbookSources[_i] = arguments[_i];
2169
+ }
2170
+ var promptbooks = new Array();
2171
+ try {
2172
+ for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2173
+ var source = promptbookSources_1_1.value;
2174
+ var promptbook = void 0;
2175
+ if (typeof source === 'string') {
2176
+ // Note: When directly creating from string, no need to validate the source
2177
+ // The validation is performed always before execution
2178
+ promptbook = promptbookStringToJson(source);
2179
+ }
2180
+ else {
2181
+ promptbook = source;
2182
+ }
2183
+ promptbooks.push(promptbook);
2184
+ }
2185
+ }
2186
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2187
+ finally {
2188
+ try {
2189
+ if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2190
+ }
2191
+ finally { if (e_1) throw e_1.error; }
2192
+ }
2193
+ return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
2194
+ }
2195
+ /**
2196
+ * Constructs Promptbook from any sources
2197
+ *
2198
+ * Note: During the construction syntax and logic of all sources are validated
2199
+ * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
2200
+ *
2201
+ * @param promptbookSources contents of .ptbk.md or .ptbk.json files
2202
+ * @param settings settings for creating executor functions
2203
+ * @returns PromptbookLibrary
2204
+ */
2205
+ /***
2206
+ * TODO: !!! Annotate all + all to README and samples
2207
+ */
2208
+
2209
+ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2210
+ var library;
2211
+ function forSources() {
2212
+ return __awaiter(this, void 0, void 0, function () {
2213
+ var promptbookSources;
2214
+ return __generator(this, function (_a) {
2215
+ switch (_a.label) {
2216
+ case 0:
2217
+ if (typeof promptbookSourcesPromiseOrFactory === 'function') {
2218
+ // Note: Calling factory function only once despite multiple calls to resolveSources
2219
+ promptbookSourcesPromiseOrFactory = promptbookSourcesPromiseOrFactory();
2220
+ }
2221
+ return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
2222
+ case 1:
2223
+ promptbookSources = _a.sent();
2224
+ library = createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false));
2225
+ return [2 /*return*/];
2226
+ }
2227
+ });
2228
+ });
2229
+ }
2230
+ function listPromptbooks() {
2231
+ return __awaiter(this, void 0, void 0, function () {
2232
+ return __generator(this, function (_a) {
2233
+ switch (_a.label) {
2234
+ case 0: return [4 /*yield*/, forSources()];
2235
+ case 1:
2236
+ _a.sent();
2237
+ return [2 /*return*/, /* not await */ library.listPromptbooks()];
2238
+ }
2239
+ });
2240
+ });
2241
+ }
2242
+ function getPromptbookByUrl(url) {
2243
+ return __awaiter(this, void 0, void 0, function () {
2244
+ return __generator(this, function (_a) {
2245
+ switch (_a.label) {
2246
+ case 0: return [4 /*yield*/, forSources()];
2247
+ case 1:
2248
+ _a.sent();
2249
+ return [2 /*return*/, /* not await */ library.getPromptbookByUrl(url)];
2250
+ }
2251
+ });
2252
+ });
2253
+ }
2254
+ function isResponsibleForPrompt(prompt) {
2255
+ return __awaiter(this, void 0, void 0, function () {
2256
+ return __generator(this, function (_a) {
2257
+ switch (_a.label) {
2258
+ case 0: return [4 /*yield*/, forSources()];
2259
+ case 1:
2260
+ _a.sent();
2261
+ return [2 /*return*/, /* not await */ library.isResponsibleForPrompt(prompt)];
2262
+ }
2263
+ });
2264
+ });
2265
+ }
2266
+ return {
2267
+ listPromptbooks: listPromptbooks,
2268
+ getPromptbookByUrl: getPromptbookByUrl,
2269
+ isResponsibleForPrompt: isResponsibleForPrompt,
2270
+ };
2271
+ }
2272
+ /***
2273
+ * TODO: !!! Annotate all + all to README and samples
2274
+ */
2275
+
2276
+ function createPromptbookSublibrary(library, predicate) {
2277
+ function listPromptbooks() {
2278
+ return __awaiter(this, void 0, void 0, function () {
2279
+ var promptbooks;
2280
+ return __generator(this, function (_a) {
2281
+ switch (_a.label) {
2282
+ case 0: return [4 /*yield*/, library.listPromptbooks()];
2283
+ case 1:
2284
+ promptbooks = _a.sent();
2285
+ promptbooks = promptbooks.filter(predicate);
2286
+ return [2 /*return*/, promptbooks];
2287
+ }
2288
+ });
2289
+ });
2290
+ }
2291
+ function getPromptbookByUrl(url) {
2292
+ return __awaiter(this, void 0, void 0, function () {
2293
+ var _a, promptbook;
2294
+ var _this = this;
2295
+ return __generator(this, function (_b) {
2296
+ switch (_b.label) {
2297
+ case 0:
2298
+ if (!!predicate(url)) return [3 /*break*/, 2];
2299
+ _a = NotFoundError.bind;
2300
+ return [4 /*yield*/, spaceTrim(function (block) { return __awaiter(_this, void 0, void 0, function () {
2301
+ var _a, _b, _c, _d, _e, _f;
2302
+ return __generator(this, function (_g) {
2303
+ switch (_g.label) {
2304
+ case 0:
2305
+ _c = (_b = "\n Promptbook with url \"".concat(url, "\" not found or not accessible\n\n Available promptbooks:\n ")).concat;
2306
+ _d = block;
2307
+ return [4 /*yield*/, listPromptbooks()];
2308
+ case 1:
2309
+ _e = (_a = _c.apply(_b, [_d.apply(void 0, [(_g.sent()).map(function (promptbookUrl) { return "- ".concat(promptbookUrl); }).join('\n')]), "\n\n All available promptbooks in parent library:\n "])).concat;
2310
+ _f = block;
2311
+ return [4 /*yield*/, library.listPromptbooks()];
2312
+ case 2: return [2 /*return*/, _e.apply(_a, [_f.apply(void 0, [(_g.sent()).map(function (promptbookUrl) { return "- ".concat(promptbookUrl); }).join('\n')]), "\n\n "])];
2313
+ }
2314
+ });
2315
+ }); })];
2316
+ case 1: throw new (_a.apply(NotFoundError, [void 0, _b.sent()]))();
2317
+ case 2: return [4 /*yield*/, library.getPromptbookByUrl(url)];
2318
+ case 3:
2319
+ promptbook = _b.sent();
2320
+ return [2 /*return*/, promptbook];
2321
+ }
2322
+ });
2323
+ });
2324
+ }
2325
+ function isResponsibleForPrompt(prompt) {
2326
+ return __awaiter(this, void 0, void 0, function () {
2327
+ var isResponsible;
2328
+ return __generator(this, function (_a) {
2329
+ switch (_a.label) {
2330
+ case 0: return [4 /*yield*/, library.isResponsibleForPrompt(prompt)];
2331
+ case 1:
2332
+ isResponsible = _a.sent();
2333
+ // TODO: !! Only if responsible, check if predicate is true
2334
+ return [2 /*return*/, isResponsible];
2335
+ }
2336
+ });
2337
+ });
2338
+ }
2339
+ return {
2340
+ listPromptbooks: listPromptbooks,
2341
+ getPromptbookByUrl: getPromptbookByUrl,
2342
+ isResponsibleForPrompt: isResponsibleForPrompt,
2343
+ };
2344
+ }
2345
+ /***
2346
+ * TODO: !!! Annotate all + all to README and samples
2347
+ */
2348
+
2349
+ export { CallbackInterfaceTools, ExecutionTypes, MockedEchoNaturalExecutionTools, PROMPTBOOK_VERSION, SimplePromptInterfaceTools, SimplePromptbookLibrary, createPromptbookExecutor, createPromptbookLibraryFromPromise, createPromptbookLibraryFromSources, createPromptbookSublibrary, promptbookStringToJson, validatePromptbookJson };
2163
2350
  //# sourceMappingURL=index.es.js.map