@promptbook/core 0.40.0-3 → 0.40.0-6

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.
@@ -6,9 +6,13 @@ import { CallbackInterfaceTools } from '../execution/plugins/user-interface-exec
6
6
  import { CallbackInterfaceToolsOptions } from '../execution/plugins/user-interface-execution-tools/callback/CallbackInterfaceToolsOptions';
7
7
  import { SimplePromptInterfaceTools } from '../execution/plugins/user-interface-execution-tools/simple-prompt/SimplePromptInterfaceTools';
8
8
  import { SimplePromptbookLibrary } from '../library/SimplePromptbookLibrary';
9
+ import { createPromptbookLibraryFromPromise } from '../library/constructors/createPromptbookLibraryFromPromise';
10
+ import { createPromptbookLibraryFromSources } from '../library/constructors/createPromptbookLibraryFromSources';
11
+ import { createPromptbookSublibrary } from '../library/constructors/createPromptbookSublibrary';
9
12
  import { ExecutionTypes } from '../types/ExecutionTypes';
10
13
  import { PROMPTBOOK_VERSION } from '../version';
11
- export { ExecutionTypes, PROMPTBOOK_VERSION, SimplePromptbookLibrary };
14
+ export { ExecutionTypes, PROMPTBOOK_VERSION };
15
+ export { SimplePromptbookLibrary, createPromptbookLibraryFromPromise, createPromptbookLibraryFromSources, createPromptbookSublibrary, };
12
16
  export { SimplePromptInterfaceTools };
13
17
  export { promptbookStringToJson, validatePromptbookJson };
14
18
  export { MockedEchoNaturalExecutionTools };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * This error indicates errors in referencing promptbooks between each other
3
+ */
4
+ export declare class PromptbookReferenceError extends Error {
5
+ readonly name = "PromptbookReferenceError";
6
+ constructor(message: string);
7
+ }
@@ -21,5 +21,5 @@ export type PromptbookLibrary = {
21
21
  /**
22
22
  * Checks whether given prompt was defined in any promptbook in the library
23
23
  */
24
- isResponsibleForPrompt(prompt: Prompt): boolean;
24
+ isResponsibleForPrompt(prompt: Prompt): Promisable<boolean>;
25
25
  };
@@ -1,28 +1,23 @@
1
1
  import type { Prompt } from '../types/Prompt';
2
2
  import type { PromptbookJson } from '../types/PromptbookJson/PromptbookJson';
3
- import type { PromptbookString } from '../types/PromptbookString';
4
3
  import type { string_promptbook_url } from '../types/typeAliases';
5
4
  import { PromptbookLibrary } from './PromptbookLibrary';
6
5
  /**
7
6
  * Library of promptbooks that groups together promptbooks for an application.
8
- * This implementation is a very thin wrapper around the Array / Set of promptbooks.
7
+ * This implementation is a very thin wrapper around the Array / Map of promptbooks.
9
8
  *
10
9
  * @see https://github.com/webgptorg/promptbook#promptbook-library
11
10
  */
12
11
  export declare class SimplePromptbookLibrary implements PromptbookLibrary {
13
- private readonly library;
14
- /**
15
- * Constructs Promptbook from any sources
12
+ private library;
13
+ /**!!!
16
14
  *
17
- * Note: During the construction syntax and logic of all sources are validated
18
- * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
15
+ * @param promptbooks !!!
19
16
  *
20
- * @param promptbookSources contents of .ptbk.md or .ptbk.json files
21
- * @param settings settings for creating executor functions
22
- * @returns PromptbookLibrary
17
+ * Note: During the construction logic of all promptbooks are validated
18
+ * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
23
19
  */
24
- static fromSources(...promptbookSources: Array<PromptbookJson | PromptbookString>): SimplePromptbookLibrary;
25
- private constructor();
20
+ constructor(...promptbooks: Array<PromptbookJson>);
26
21
  /**
27
22
  * Gets all promptbooks in the library
28
23
  */
@@ -0,0 +1,7 @@
1
+ import type { PromptbookJson } from '../../types/PromptbookJson/PromptbookJson';
2
+ import { PromptbookString } from '../../types/PromptbookString';
3
+ import { PromptbookLibrary } from '../PromptbookLibrary';
4
+ export declare function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory: Promise<Array<PromptbookJson | PromptbookString>> | (() => Promise<Array<PromptbookJson | PromptbookString>>)): PromptbookLibrary;
5
+ /***
6
+ * TODO: !!! Annotate all + all to README and samples
7
+ */
@@ -0,0 +1,16 @@
1
+ import { PromptbookJson, PromptbookString } from '../../_packages/types.index';
2
+ import { SimplePromptbookLibrary } from '../SimplePromptbookLibrary';
3
+ export declare function createPromptbookLibraryFromSources(...promptbookSources: Array<PromptbookJson | PromptbookString>): SimplePromptbookLibrary;
4
+ /**
5
+ * Constructs Promptbook from any sources
6
+ *
7
+ * Note: During the construction syntax and logic of all sources are validated
8
+ * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
9
+ *
10
+ * @param promptbookSources contents of .ptbk.md or .ptbk.json files
11
+ * @param settings settings for creating executor functions
12
+ * @returns PromptbookLibrary
13
+ */
14
+ /***
15
+ * TODO: !!! Annotate all + all to README and samples
16
+ */
@@ -0,0 +1,6 @@
1
+ import type { string_promptbook_url } from '../../types/typeAliases';
2
+ import { PromptbookLibrary } from '../PromptbookLibrary';
3
+ export declare function createPromptbookSublibrary(library: PromptbookLibrary, predicate: (url: string_promptbook_url) => boolean): PromptbookLibrary;
4
+ /***
5
+ * TODO: !!! Annotate all + all to README and samples
6
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/core",
3
- "version": "0.40.0-3",
3
+ "version": "0.40.0-6",
4
4
  "description": "Library to supercharge your use of large language models",
5
5
  "private": false,
6
6
  "sideEffects": false,
package/umd/index.umd.js CHANGED
@@ -427,7 +427,7 @@
427
427
  /**
428
428
  * The version of the Promptbook library
429
429
  */
430
- var PROMPTBOOK_VERSION = '0.40.0-2';
430
+ var PROMPTBOOK_VERSION = '0.40.0-5';
431
431
 
432
432
  /**
433
433
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -2085,61 +2085,59 @@
2085
2085
  return NotFoundError;
2086
2086
  }(Error));
2087
2087
 
2088
+ /**
2089
+ * This error indicates errors in referencing promptbooks between each other
2090
+ */
2091
+ var PromptbookReferenceError = /** @class */ (function (_super) {
2092
+ __extends(PromptbookReferenceError, _super);
2093
+ function PromptbookReferenceError(message) {
2094
+ var _this = _super.call(this, message) || this;
2095
+ _this.name = 'PromptbookReferenceError';
2096
+ Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
2097
+ return _this;
2098
+ }
2099
+ return PromptbookReferenceError;
2100
+ }(Error));
2101
+
2088
2102
  /**
2089
2103
  * Library of promptbooks that groups together promptbooks for an application.
2090
- * This implementation is a very thin wrapper around the Array / Set of promptbooks.
2104
+ * This implementation is a very thin wrapper around the Array / Map of promptbooks.
2091
2105
  *
2092
2106
  * @see https://github.com/webgptorg/promptbook#promptbook-library
2093
2107
  */
2094
2108
  var SimplePromptbookLibrary = /** @class */ (function () {
2095
- function SimplePromptbookLibrary(library) {
2096
- this.library = library;
2097
- }
2098
- /**
2099
- * Constructs Promptbook from any sources
2109
+ /**!!!
2100
2110
  *
2101
- * Note: During the construction syntax and logic of all sources are validated
2102
- * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
2111
+ * @param promptbooks !!!
2103
2112
  *
2104
- * @param promptbookSources contents of .ptbk.md or .ptbk.json files
2105
- * @param settings settings for creating executor functions
2106
- * @returns PromptbookLibrary
2113
+ * Note: During the construction logic of all promptbooks are validated
2114
+ * Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
2107
2115
  */
2108
- SimplePromptbookLibrary.fromSources = function () {
2116
+ function SimplePromptbookLibrary() {
2109
2117
  var e_1, _a;
2110
- var promptbookSources = [];
2118
+ var promptbooks = [];
2111
2119
  for (var _i = 0; _i < arguments.length; _i++) {
2112
- promptbookSources[_i] = arguments[_i];
2120
+ promptbooks[_i] = arguments[_i];
2113
2121
  }
2114
- var library = new Map();
2122
+ this.library = new Map();
2115
2123
  try {
2116
- for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2117
- var source = promptbookSources_1_1.value;
2118
- var promptbook = void 0;
2119
- if (typeof source === 'string') {
2120
- // Note: When directly creating from string, no need to validate the source
2121
- // The validation is performed always before execution
2122
- promptbook = promptbookStringToJson(source);
2123
- }
2124
- else {
2125
- promptbook = source;
2126
- }
2127
- validatePromptbookJson(promptbook);
2124
+ for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
2125
+ var promptbook = promptbooks_1_1.value;
2128
2126
  if (promptbook.promptbookUrl === undefined) {
2129
- throw new Error(spaceTrim__default["default"]("\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 ")));
2127
+ throw new PromptbookReferenceError(spaceTrim__default["default"]("\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 ")));
2130
2128
  }
2131
- library.set(promptbook.promptbookUrl, promptbook);
2129
+ validatePromptbookJson(promptbook);
2130
+ this.library.set(promptbook.promptbookUrl, promptbook);
2132
2131
  }
2133
2132
  }
2134
2133
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
2135
2134
  finally {
2136
2135
  try {
2137
- if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2136
+ if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
2138
2137
  }
2139
2138
  finally { if (e_1) throw e_1.error; }
2140
2139
  }
2141
- return new SimplePromptbookLibrary(library);
2142
- };
2140
+ }
2143
2141
  /**
2144
2142
  * Gets all promptbooks in the library
2145
2143
  */
@@ -2170,6 +2168,191 @@
2170
2168
  return SimplePromptbookLibrary;
2171
2169
  }());
2172
2170
 
2171
+ function createPromptbookLibraryFromSources() {
2172
+ var e_1, _a;
2173
+ var promptbookSources = [];
2174
+ for (var _i = 0; _i < arguments.length; _i++) {
2175
+ promptbookSources[_i] = arguments[_i];
2176
+ }
2177
+ var promptbooks = new Array();
2178
+ try {
2179
+ for (var promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next(); !promptbookSources_1_1.done; promptbookSources_1_1 = promptbookSources_1.next()) {
2180
+ var source = promptbookSources_1_1.value;
2181
+ var promptbook = void 0;
2182
+ if (typeof source === 'string') {
2183
+ // Note: When directly creating from string, no need to validate the source
2184
+ // The validation is performed always before execution
2185
+ promptbook = promptbookStringToJson(source);
2186
+ }
2187
+ else {
2188
+ promptbook = source;
2189
+ }
2190
+ promptbooks.push(promptbook);
2191
+ }
2192
+ }
2193
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2194
+ finally {
2195
+ try {
2196
+ if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
2197
+ }
2198
+ finally { if (e_1) throw e_1.error; }
2199
+ }
2200
+ return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
2201
+ }
2202
+ /**
2203
+ * Constructs Promptbook from any sources
2204
+ *
2205
+ * Note: During the construction syntax and logic of all sources are validated
2206
+ * Note: You can combine .ptbk.md and .ptbk.json files BUT it is not recommended
2207
+ *
2208
+ * @param promptbookSources contents of .ptbk.md or .ptbk.json files
2209
+ * @param settings settings for creating executor functions
2210
+ * @returns PromptbookLibrary
2211
+ */
2212
+ /***
2213
+ * TODO: !!! Annotate all + all to README and samples
2214
+ */
2215
+
2216
+ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
2217
+ var library;
2218
+ function forSources() {
2219
+ return __awaiter(this, void 0, void 0, function () {
2220
+ var promptbookSources;
2221
+ return __generator(this, function (_a) {
2222
+ switch (_a.label) {
2223
+ case 0:
2224
+ if (typeof promptbookSourcesPromiseOrFactory === 'function') {
2225
+ // Note: Calling factory function only once despite multiple calls to resolveSources
2226
+ promptbookSourcesPromiseOrFactory = promptbookSourcesPromiseOrFactory();
2227
+ }
2228
+ return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
2229
+ case 1:
2230
+ promptbookSources = _a.sent();
2231
+ library = createPromptbookLibraryFromSources.apply(void 0, __spreadArray([], __read(promptbookSources), false));
2232
+ return [2 /*return*/];
2233
+ }
2234
+ });
2235
+ });
2236
+ }
2237
+ function listPromptbooks() {
2238
+ return __awaiter(this, void 0, void 0, function () {
2239
+ return __generator(this, function (_a) {
2240
+ switch (_a.label) {
2241
+ case 0: return [4 /*yield*/, forSources()];
2242
+ case 1:
2243
+ _a.sent();
2244
+ return [2 /*return*/, /* not await */ library.listPromptbooks()];
2245
+ }
2246
+ });
2247
+ });
2248
+ }
2249
+ function getPromptbookByUrl(url) {
2250
+ return __awaiter(this, void 0, void 0, function () {
2251
+ return __generator(this, function (_a) {
2252
+ switch (_a.label) {
2253
+ case 0: return [4 /*yield*/, forSources()];
2254
+ case 1:
2255
+ _a.sent();
2256
+ return [2 /*return*/, /* not await */ library.getPromptbookByUrl(url)];
2257
+ }
2258
+ });
2259
+ });
2260
+ }
2261
+ function isResponsibleForPrompt(prompt) {
2262
+ return __awaiter(this, void 0, void 0, function () {
2263
+ return __generator(this, function (_a) {
2264
+ switch (_a.label) {
2265
+ case 0: return [4 /*yield*/, forSources()];
2266
+ case 1:
2267
+ _a.sent();
2268
+ return [2 /*return*/, /* not await */ library.isResponsibleForPrompt(prompt)];
2269
+ }
2270
+ });
2271
+ });
2272
+ }
2273
+ return {
2274
+ listPromptbooks: listPromptbooks,
2275
+ getPromptbookByUrl: getPromptbookByUrl,
2276
+ isResponsibleForPrompt: isResponsibleForPrompt,
2277
+ };
2278
+ }
2279
+ /***
2280
+ * TODO: !!! Annotate all + all to README and samples
2281
+ */
2282
+
2283
+ function createPromptbookSublibrary(library, predicate) {
2284
+ function listPromptbooks() {
2285
+ return __awaiter(this, void 0, void 0, function () {
2286
+ var promptbooks;
2287
+ return __generator(this, function (_a) {
2288
+ switch (_a.label) {
2289
+ case 0: return [4 /*yield*/, library.listPromptbooks()];
2290
+ case 1:
2291
+ promptbooks = _a.sent();
2292
+ promptbooks = promptbooks.filter(predicate);
2293
+ return [2 /*return*/, promptbooks];
2294
+ }
2295
+ });
2296
+ });
2297
+ }
2298
+ function getPromptbookByUrl(url) {
2299
+ return __awaiter(this, void 0, void 0, function () {
2300
+ var _a, promptbook;
2301
+ var _this = this;
2302
+ return __generator(this, function (_b) {
2303
+ switch (_b.label) {
2304
+ case 0:
2305
+ if (!!predicate(url)) return [3 /*break*/, 2];
2306
+ _a = NotFoundError.bind;
2307
+ return [4 /*yield*/, spaceTrim__default["default"](function (block) { return __awaiter(_this, void 0, void 0, function () {
2308
+ var _a, _b, _c, _d, _e, _f;
2309
+ return __generator(this, function (_g) {
2310
+ switch (_g.label) {
2311
+ case 0:
2312
+ _c = (_b = "\n Promptbook with url \"".concat(url, "\" not found or not accessible\n\n Available promptbooks:\n ")).concat;
2313
+ _d = block;
2314
+ return [4 /*yield*/, listPromptbooks()];
2315
+ case 1:
2316
+ _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;
2317
+ _f = block;
2318
+ return [4 /*yield*/, library.listPromptbooks()];
2319
+ case 2: return [2 /*return*/, _e.apply(_a, [_f.apply(void 0, [(_g.sent()).map(function (promptbookUrl) { return "- ".concat(promptbookUrl); }).join('\n')]), "\n\n "])];
2320
+ }
2321
+ });
2322
+ }); })];
2323
+ case 1: throw new (_a.apply(NotFoundError, [void 0, _b.sent()]))();
2324
+ case 2: return [4 /*yield*/, library.getPromptbookByUrl(url)];
2325
+ case 3:
2326
+ promptbook = _b.sent();
2327
+ return [2 /*return*/, promptbook];
2328
+ }
2329
+ });
2330
+ });
2331
+ }
2332
+ function isResponsibleForPrompt(prompt) {
2333
+ return __awaiter(this, void 0, void 0, function () {
2334
+ var isResponsible;
2335
+ return __generator(this, function (_a) {
2336
+ switch (_a.label) {
2337
+ case 0: return [4 /*yield*/, library.isResponsibleForPrompt(prompt)];
2338
+ case 1:
2339
+ isResponsible = _a.sent();
2340
+ // TODO: !! Only if responsible, check if predicate is true
2341
+ return [2 /*return*/, isResponsible];
2342
+ }
2343
+ });
2344
+ });
2345
+ }
2346
+ return {
2347
+ listPromptbooks: listPromptbooks,
2348
+ getPromptbookByUrl: getPromptbookByUrl,
2349
+ isResponsibleForPrompt: isResponsibleForPrompt,
2350
+ };
2351
+ }
2352
+ /***
2353
+ * TODO: !!! Annotate all + all to README and samples
2354
+ */
2355
+
2173
2356
  exports.CallbackInterfaceTools = CallbackInterfaceTools;
2174
2357
  exports.ExecutionTypes = ExecutionTypes;
2175
2358
  exports.MockedEchoNaturalExecutionTools = MockedEchoNaturalExecutionTools;
@@ -2177,6 +2360,9 @@
2177
2360
  exports.SimplePromptInterfaceTools = SimplePromptInterfaceTools;
2178
2361
  exports.SimplePromptbookLibrary = SimplePromptbookLibrary;
2179
2362
  exports.createPromptbookExecutor = createPromptbookExecutor;
2363
+ exports.createPromptbookLibraryFromPromise = createPromptbookLibraryFromPromise;
2364
+ exports.createPromptbookLibraryFromSources = createPromptbookLibraryFromSources;
2365
+ exports.createPromptbookSublibrary = createPromptbookSublibrary;
2180
2366
  exports.promptbookStringToJson = promptbookStringToJson;
2181
2367
  exports.validatePromptbookJson = validatePromptbookJson;
2182
2368