@promptbook/core 0.55.0-1 → 0.55.0

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
@@ -448,7 +448,7 @@ function union() {
448
448
  /**
449
449
  * The version of the Promptbook library
450
450
  */
451
- var PROMPTBOOK_VERSION = '0.55.0-0';
451
+ var PROMPTBOOK_VERSION = '0.55.0-2';
452
452
 
453
453
  /**
454
454
  * Parses the template and returns the list of all parameter names
@@ -2279,11 +2279,55 @@ var TemplateError = /** @class */ (function (_super) {
2279
2279
  }(Error));
2280
2280
 
2281
2281
  /**
2282
- * Function addPromptResultUsage will add multiple usages into one
2282
+ * Asserts that the execution of a promptnook is successful
2283
+ *
2284
+ * @param executionResult - The partial result of the promptnook execution
2285
+ * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
2286
+ */
2287
+ function assertsExecutionSuccessful(executionResult) {
2288
+ var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
2289
+ if (isSuccessful === true) {
2290
+ return;
2291
+ }
2292
+ if (errors.length === 0) {
2293
+ throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
2294
+ }
2295
+ else if (errors.length === 1) {
2296
+ throw errors[0];
2297
+ }
2298
+ else {
2299
+ 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 "); }));
2300
+ }
2301
+ }
2302
+ /**
2303
+ * TODO: [🧠] Can this return type be better typed than void
2304
+ */
2305
+
2306
+ /**
2307
+ * Function isValidJsonString will tell you if the string is valid JSON or not
2308
+ */
2309
+ function isValidJsonString(value) {
2310
+ try {
2311
+ JSON.parse(value);
2312
+ return true;
2313
+ }
2314
+ catch (error) {
2315
+ if (!(error instanceof Error)) {
2316
+ throw error;
2317
+ }
2318
+ if (error.message.includes('Unexpected token')) {
2319
+ return false;
2320
+ }
2321
+ return false;
2322
+ }
2323
+ }
2324
+
2325
+ /**
2326
+ * Function `addUsage` will add multiple usages into one
2283
2327
  *
2284
2328
  * Note: If you provide 0 values, it returns void usage
2285
2329
  */
2286
- function addPromptResultUsage() {
2330
+ function addUsage() {
2287
2331
  var usageItems = [];
2288
2332
  for (var _i = 0; _i < arguments.length; _i++) {
2289
2333
  usageItems[_i] = arguments[_i];
@@ -2369,50 +2413,6 @@ function addPromptResultUsage() {
2369
2413
  }, initialStructure);
2370
2414
  }
2371
2415
 
2372
- /**
2373
- * Asserts that the execution of a promptnook is successful
2374
- *
2375
- * @param executionResult - The partial result of the promptnook execution
2376
- * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
2377
- */
2378
- function assertsExecutionSuccessful(executionResult) {
2379
- var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
2380
- if (isSuccessful === true) {
2381
- return;
2382
- }
2383
- if (errors.length === 0) {
2384
- throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
2385
- }
2386
- else if (errors.length === 1) {
2387
- throw errors[0];
2388
- }
2389
- else {
2390
- 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 "); }));
2391
- }
2392
- }
2393
- /**
2394
- * TODO: [🧠] Can this return type be better typed than void
2395
- */
2396
-
2397
- /**
2398
- * Function isValidJsonString will tell you if the string is valid JSON or not
2399
- */
2400
- function isValidJsonString(value) {
2401
- try {
2402
- JSON.parse(value);
2403
- return true;
2404
- }
2405
- catch (error) {
2406
- if (!(error instanceof Error)) {
2407
- throw error;
2408
- }
2409
- if (error.message.includes('Unexpected token')) {
2410
- return false;
2411
- }
2412
- return false;
2413
- }
2414
- }
2415
-
2416
2416
  /**
2417
2417
  * Counts number of characters in the text
2418
2418
  */
@@ -3002,7 +3002,7 @@ function createPromptbookExecutor(options) {
3002
3002
  });
3003
3003
  });
3004
3004
  }
3005
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, _a, _b, parameter;
3005
+ var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
3006
3006
  var e_1, _c;
3007
3007
  return __generator(this, function (_d) {
3008
3008
  switch (_d.label) {
@@ -3081,9 +3081,14 @@ function createPromptbookExecutor(options) {
3081
3081
  if (!(error_1 instanceof Error)) {
3082
3082
  throw error_1;
3083
3083
  }
3084
+ usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
3085
+ var result = _a.result;
3086
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3087
+ })), false));
3084
3088
  return [2 /*return*/, {
3085
3089
  isSuccessful: false,
3086
3090
  errors: [error_1],
3091
+ usage: usage_1,
3087
3092
  executionReport: executionReport,
3088
3093
  outputParameters: parametersToPass,
3089
3094
  }];
@@ -3105,9 +3110,14 @@ function createPromptbookExecutor(options) {
3105
3110
  }
3106
3111
  finally { if (e_1) throw e_1.error; }
3107
3112
  }
3113
+ usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
3114
+ var result = _a.result;
3115
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3116
+ })), false));
3108
3117
  return [2 /*return*/, {
3109
3118
  isSuccessful: true,
3110
3119
  errors: [],
3120
+ usage: usage,
3111
3121
  executionReport: executionReport,
3112
3122
  outputParameters: parametersToPass,
3113
3123
  }];
@@ -3315,6 +3325,24 @@ var SimplePromptInterfaceTools = /** @class */ (function () {
3315
3325
  return SimplePromptInterfaceTools;
3316
3326
  }());
3317
3327
 
3328
+ /**
3329
+ * Function usageToWorktime will take usage and estimate saved worktime in hours of reading / writing
3330
+ *
3331
+ * Note: This is an estimate based of theese sources:
3332
+ * - https://jecas.cz/doba-cteni
3333
+ * - https://www.originalnitonery.cz/blog/psani-vsemi-deseti-se-muzete-naucit-i-sami-doma
3334
+ */
3335
+ function usageToWorktime(usage) {
3336
+ var value = usage.input.wordsCount.value / (200 /* words per minute */ * 60) +
3337
+ usage.output.wordsCount.value / (40 /* words per minute */ * 60);
3338
+ var isUncertain = usage.input.wordsCount.isUncertain || usage.output.wordsCount.isUncertain;
3339
+ var uncertainNumber = { value: value };
3340
+ if (isUncertain === true) {
3341
+ uncertainNumber.isUncertain = true;
3342
+ }
3343
+ return uncertainNumber;
3344
+ }
3345
+
3318
3346
  /**
3319
3347
  * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3320
3348
  */
@@ -4095,13 +4123,16 @@ function executionReportJsonToString(executionReportJson, options) {
4095
4123
  return ({
4096
4124
  title: promptExecution.prompt.title,
4097
4125
  from: 0,
4098
- to: (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price.value) || 0) /* uncertainNumber */ * (1 + taxRate),
4126
+ to: (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price.value) || 0) /* <- TODO: look at uncertain numbers */ *
4127
+ (1 + taxRate),
4099
4128
  });
4100
4129
  });
4101
4130
  var duration = moment.duration(completedAt.diff(startedAt));
4102
4131
  var llmDuration = moment.duration(countWorkingDuration(timingItems) * 1000);
4103
4132
  var executionsWithKnownCost = executionReportJson.promptExecutions.filter(function (promptExecution) { var _a, _b; return (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) || 'UNKNOWN') !== 'UNKNOWN'; });
4104
- var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) { return cost + ((promptExecution.result.usage.price.value /* uncertainNumber */) || 0); }, 0);
4133
+ var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) {
4134
+ return cost + (promptExecution.result.usage.price.value /* <- Look at uncertain number */ || 0);
4135
+ }, 0);
4105
4136
  headerList.push("STARTED AT ".concat(moment(startedAt).format("YYYY-MM-DD HH:mm:ss")));
4106
4137
  headerList.push("COMPLETED AT ".concat(moment(completedAt).format("YYYY-MM-DD HH:mm:ss")));
4107
4138
  headerList.push("TOTAL DURATION ".concat(duration.humanize(MOMENT_ARG_THRESHOLDS)));
@@ -4214,5 +4245,5 @@ function executionReportJsonToString(executionReportJson, options) {
4214
4245
  * TODO: [🧠] Allow to filter out some parts of the report by options
4215
4246
  */
4216
4247
 
4217
- export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, SimplePromptbookLibrary, TemplateError, UnexpectedError, addPromptResultUsage, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, validatePromptbookJson };
4248
+ export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, SimplePromptbookLibrary, TemplateError, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, createPromptbookExecutor, createPromptbookLibraryFromDirectory, createPromptbookLibraryFromSources, createPromptbookLibraryFromUrl, createPromptbookSublibrary, executionReportJsonToString, isPassingExpectations, justTestFsImport, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, usageToWorktime, validatePromptbookJson };
4218
4249
  //# sourceMappingURL=index.es.js.map