@promptbook/core 0.31.0-0 → 0.31.0-10

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.
Files changed (49) hide show
  1. package/esm/index.es.js +100 -5
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/_packages/types.index.d.ts +2 -1
  4. package/esm/typings/_packages/utils.index.d.ts +6 -2
  5. package/esm/typings/execution/PromptResult.d.ts +38 -1
  6. package/esm/typings/execution/plugins/natural-execution-tools/openai/computeOpenaiUsage.d.ts +3 -0
  7. package/esm/typings/types/execution-report/ExecutionReportStringOptions.d.ts +13 -0
  8. package/esm/typings/types/execution-report/config.d.ts +8 -0
  9. package/esm/typings/types/execution-report/countWorkingDuration.d.ts +7 -0
  10. package/esm/typings/types/execution-report/countWorkingDuration.test.d.ts +1 -0
  11. package/esm/typings/types/execution-report/executionReportJsonToString.d.ts +5 -1
  12. package/esm/typings/types/typeAliases.d.ts +16 -0
  13. package/esm/typings/utils/FromtoItems.d.ts +19 -0
  14. package/esm/typings/utils/extractParameters.d.ts +2 -0
  15. package/esm/typings/utils/extractVariables.d.ts +10 -0
  16. package/esm/typings/utils/extractVariables.test.d.ts +1 -0
  17. package/esm/typings/utils/formatNumber.d.ts +6 -0
  18. package/esm/typings/utils/formatNumber.test.d.ts +1 -0
  19. package/esm/typings/utils/getCurrentIsoDate.d.ts +7 -0
  20. package/esm/typings/utils/markdown/createMarkdownChart.d.ts +37 -0
  21. package/esm/typings/utils/markdown/createMarkdownChart.test.d.ts +1 -0
  22. package/esm/typings/utils/markdown/createMarkdownTable.d.ts +7 -0
  23. package/esm/typings/utils/markdown/createMarkdownTable.test.d.ts +1 -0
  24. package/esm/typings/utils/replaceParameters.d.ts +2 -0
  25. package/package.json +1 -1
  26. package/umd/index.umd.js +100 -5
  27. package/umd/index.umd.js.map +1 -1
  28. package/umd/typings/_packages/types.index.d.ts +2 -1
  29. package/umd/typings/_packages/utils.index.d.ts +6 -2
  30. package/umd/typings/execution/PromptResult.d.ts +38 -1
  31. package/umd/typings/execution/plugins/natural-execution-tools/openai/computeOpenaiUsage.d.ts +3 -0
  32. package/umd/typings/types/execution-report/ExecutionReportStringOptions.d.ts +13 -0
  33. package/umd/typings/types/execution-report/config.d.ts +8 -0
  34. package/umd/typings/types/execution-report/countWorkingDuration.d.ts +7 -0
  35. package/umd/typings/types/execution-report/countWorkingDuration.test.d.ts +1 -0
  36. package/umd/typings/types/execution-report/executionReportJsonToString.d.ts +5 -1
  37. package/umd/typings/types/typeAliases.d.ts +16 -0
  38. package/umd/typings/utils/FromtoItems.d.ts +19 -0
  39. package/umd/typings/utils/extractParameters.d.ts +2 -0
  40. package/umd/typings/utils/extractVariables.d.ts +10 -0
  41. package/umd/typings/utils/extractVariables.test.d.ts +1 -0
  42. package/umd/typings/utils/formatNumber.d.ts +6 -0
  43. package/umd/typings/utils/formatNumber.test.d.ts +1 -0
  44. package/umd/typings/utils/getCurrentIsoDate.d.ts +7 -0
  45. package/umd/typings/utils/markdown/createMarkdownChart.d.ts +37 -0
  46. package/umd/typings/utils/markdown/createMarkdownChart.test.d.ts +1 -0
  47. package/umd/typings/utils/markdown/createMarkdownTable.d.ts +7 -0
  48. package/umd/typings/utils/markdown/createMarkdownTable.test.d.ts +1 -0
  49. package/umd/typings/utils/replaceParameters.d.ts +2 -0
@@ -1,6 +1,10 @@
1
1
  import type { ExecutionReportJson } from './ExecutionReportJson';
2
2
  import type { ExecutionReportString } from './ExecutionReportString';
3
+ import type { ExecutionReportStringOptions } from './ExecutionReportStringOptions';
3
4
  /**
4
5
  * Converts execution report from JSON to string format
5
6
  */
6
- export declare function executionReportJsonToString(executionReportJson: ExecutionReportJson): ExecutionReportString;
7
+ export declare function executionReportJsonToString(executionReportJson: ExecutionReportJson, options?: Partial<ExecutionReportStringOptions>): ExecutionReportString;
8
+ /**
9
+ * TODO: [🧠] Allow to filter out some parts of the report by options
10
+ */
@@ -404,6 +404,22 @@ export type string_license_token = string_token;
404
404
  export type string_password = string;
405
405
  export type string_ssh_key = string;
406
406
  export type string_pgp_key = string;
407
+ /**
408
+ * Semantic helper for `Date.toISOString()` result
409
+ *
410
+ * @example "2011-10-05T14:48:00.000Z".
411
+ * @see https://en.wikipedia.org/wiki/ISO_8601
412
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
413
+ */
414
+ export type string_date_iso8601 = `${number}-${number}-${number}${string}${number}:${number}:${number}${string}`;
415
+ /**
416
+ * Semantic helper for US Dollars
417
+ */
418
+ export type number_usd = number;
419
+ /**
420
+ * Semantic helper for number of tokens
421
+ */
422
+ export type number_tokens = number_integer & number_positive_or_zero;
407
423
  export type number_positive = number;
408
424
  export type number_negative = number;
409
425
  export type number_positive_or_zero = number;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Represents items which are compared to each other
3
+ */
4
+ export type FromtoItems = Array<{
5
+ /**
6
+ * The title of the item
7
+ */
8
+ title: string;
9
+ /**
10
+ * The start of the item
11
+ *
12
+ * This can mean anything, like seconds, milliseconds, dollars, etc.
13
+ */
14
+ from: number;
15
+ /**
16
+ * The end of the item
17
+ */
18
+ to: number;
19
+ }>;
@@ -4,5 +4,7 @@ import { string_name, string_template } from '../types/typeAliases';
4
4
  *
5
5
  * @param template the template with parameters in {curly} braces
6
6
  * @returns the list of parameter names
7
+ *
8
+ * @private within the library
7
9
  */
8
10
  export declare function extractParameters(template: string_template): Array<string_name>;
@@ -0,0 +1,10 @@
1
+ import { string_javascript, string_javascript_name } from '../types/typeAliases';
2
+ /**
3
+ * Parses the given script and returns the list of all used variables that are not defined in the script
4
+ *
5
+ * @param script from which to extract the variables
6
+ * @returns the list of variable names
7
+ *
8
+ * @private within the library
9
+ */
10
+ export declare function extractVariables(script: string_javascript): Array<string_javascript_name>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Format either small or big number
3
+ *
4
+ * @private within the library
5
+ */
6
+ export declare function formatNumber(value: number): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { string_date_iso8601 } from '../types/typeAliases';
2
+ /**
3
+ * Get current date in ISO 8601 format
4
+ *
5
+ * @private This is internal util of the promptbook
6
+ */
7
+ export declare function getCurrentIsoDate(): string_date_iso8601;
@@ -0,0 +1,37 @@
1
+ import { string_markdown } from '../../types/typeAliases';
2
+ import { FromtoItems } from '../FromtoItems';
3
+ /**
4
+ * Options for creating a markdown chart
5
+ */
6
+ /**
7
+ * Options for creating a markdown chart.
8
+ */
9
+ type CreateMarkdownChartOptions = {
10
+ /**
11
+ * The header for the first column - the name of the item
12
+ */
13
+ nameHeader: string;
14
+ /**
15
+ * The header for the second column - the value of the item
16
+ */
17
+ valueHeader: string;
18
+ /**
19
+ * The items to be charted
20
+ */
21
+ items: FromtoItems;
22
+ /**
23
+ * The width of the chart in squares
24
+ */
25
+ width: number;
26
+ /**
27
+ * The name of the unit shown in the chart
28
+ */
29
+ unitName: string;
30
+ };
31
+ /**
32
+ * Function createMarkdownChart will draw a chart in markdown from ⬛+🟦 tiles
33
+ *
34
+ * @private within the library
35
+ */
36
+ export declare function createMarkdownChart(options: CreateMarkdownChartOptions): string_markdown;
37
+ export {};
@@ -0,0 +1,7 @@
1
+ import { string_markdown, string_markdown_text } from '../../types/typeAliases';
2
+ /**
3
+ * Create a markdown table from a 2D array of strings
4
+ *
5
+ * @private within the library
6
+ */
7
+ export declare function createMarkdownTable(table: Array<Array<string_markdown_text>>): string_markdown;
@@ -6,5 +6,7 @@ import { Parameters } from '../types/Parameters';
6
6
  * @param template the template with parameters in {curly} braces
7
7
  * @param parameters the object with parameters
8
8
  * @returns the template with replaced parameters
9
+ *
10
+ * @private within the library
9
11
  */
10
12
  export declare function replaceParameters(template: string_template, parameters: Parameters): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/core",
3
- "version": "0.31.0-0",
3
+ "version": "0.31.0-10",
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
@@ -156,6 +156,8 @@
156
156
  *
157
157
  * @param template the template with parameters in {curly} braces
158
158
  * @returns the list of parameter names
159
+ *
160
+ * @private within the library
159
161
  */
160
162
  function extractParameters(template) {
161
163
  var e_1, _a;
@@ -180,6 +182,53 @@
180
182
  return parameterNames;
181
183
  }
182
184
 
185
+ /**
186
+ * Parses the given script and returns the list of all used variables that are not defined in the script
187
+ *
188
+ * @param script from which to extract the variables
189
+ * @returns the list of variable names
190
+ *
191
+ * @private within the library
192
+ */
193
+ function extractVariables(script) {
194
+ var variables = [];
195
+ script = "(()=>{".concat(script, "})()");
196
+ try {
197
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
198
+ try {
199
+ eval(script);
200
+ }
201
+ catch (error) {
202
+ if (!(error instanceof ReferenceError)) {
203
+ throw error;
204
+ }
205
+ var undefinedName = error.message.split(' ')[0];
206
+ /*
207
+ Note: Remapping error
208
+ From: [ReferenceError: thing is not defined],
209
+ To: [Error: Parameter {thing} is not defined],
210
+ */
211
+ if (!undefinedName) {
212
+ throw error;
213
+ }
214
+ if (script.includes(undefinedName + '(')) {
215
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
216
+ }
217
+ else {
218
+ variables.push(undefinedName);
219
+ script = "const ".concat(undefinedName, " = '';") + script;
220
+ }
221
+ }
222
+ }
223
+ catch (error) {
224
+ if (!(error instanceof Error)) {
225
+ throw error;
226
+ }
227
+ throw new Error(spaceTrim__default["default"](function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
228
+ }
229
+ return variables;
230
+ }
231
+
183
232
  /**
184
233
  * Computes the deepness of the markdown structure.
185
234
  *
@@ -643,6 +692,7 @@
643
692
  type: 'EXPECT_FORMAT',
644
693
  format: 'JSON',
645
694
  };
695
+ // [🥤]
646
696
  }
647
697
  else if (type.startsWith('EXPECT')) {
648
698
  try {
@@ -814,7 +864,7 @@
814
864
  finally { if (e_1) throw e_1.error; }
815
865
  }
816
866
  var _loop_1 = function (section) {
817
- var e_3, _e, e_4, _f;
867
+ var e_3, _e, e_4, _f, e_5, _g;
818
868
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
819
869
  var templateModelRequirements = __assign({}, defaultModelRequirements);
820
870
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
@@ -887,7 +937,7 @@
887
937
  }
888
938
  finally { if (e_3) throw e_3.error; }
889
939
  }
890
- var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
940
+ var _h = extractOneBlockFromMarkdown(section.content), language = _h.language, content = _h.content;
891
941
  if (executionType === 'SCRIPT') {
892
942
  if (!language) {
893
943
  throw new Error('You must specify the language of the script in the prompt template');
@@ -927,18 +977,34 @@
927
977
  postprocessing = undefined;
928
978
  }
929
979
  try {
930
- for (var _h = (e_4 = void 0, __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(section.title)), false), __read(extractParameters(description_1 || '')), false), __read(extractParameters(content)), false))), _j = _h.next(); !_j.done; _j = _h.next()) {
931
- var parameterName = _j.value;
980
+ for (var _j = (e_4 = void 0, __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(section.title)), false), __read(extractParameters(description_1 || '')), false), __read(extractParameters(content)), false))), _k = _j.next(); !_k.done; _k = _j.next()) {
981
+ var parameterName = _k.value;
932
982
  dependentParameterNames.push(parameterName);
933
983
  }
934
984
  }
935
985
  catch (e_4_1) { e_4 = { error: e_4_1 }; }
936
986
  finally {
937
987
  try {
938
- if (_j && !_j.done && (_f = _h.return)) _f.call(_h);
988
+ if (_k && !_k.done && (_f = _j.return)) _f.call(_j);
939
989
  }
940
990
  finally { if (e_4) throw e_4.error; }
941
991
  }
992
+ if (executionType === 'SCRIPT') {
993
+ try {
994
+ for (var _l = (e_5 = void 0, __values(extractVariables(content))), _m = _l.next(); !_m.done; _m = _l.next()) {
995
+ var parameterName = _m.value;
996
+ dependentParameterNames.push(parameterName);
997
+ }
998
+ }
999
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1000
+ finally {
1001
+ try {
1002
+ if (_m && !_m.done && (_g = _l.return)) _g.call(_l);
1003
+ }
1004
+ finally { if (e_5) throw e_5.error; }
1005
+ }
1006
+ }
1007
+ dependentParameterNames = __spreadArray([], __read(new Set(dependentParameterNames)), false);
942
1008
  ptbJson.promptTemplates.push({
943
1009
  name: n12.normalizeTo_PascalCase(section.title),
944
1010
  title: section.title,
@@ -1267,6 +1333,8 @@
1267
1333
  * @param template the template with parameters in {curly} braces
1268
1334
  * @param parameters the object with parameters
1269
1335
  * @returns the template with replaced parameters
1336
+ *
1337
+ * @private within the library
1270
1338
  */
1271
1339
  function replaceParameters(template, parameters) {
1272
1340
  var e_1, _a;
@@ -1853,6 +1921,15 @@
1853
1921
  * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
1854
1922
  */
1855
1923
 
1924
+ /**
1925
+ * Get current date in ISO 8601 format
1926
+ *
1927
+ * @private This is internal util of the promptbook
1928
+ */
1929
+ function getCurrentIsoDate() {
1930
+ return new Date().toISOString();
1931
+ }
1932
+
1856
1933
  /**
1857
1934
  * Mocked execution Tools for just echoing the requests for testing purposes.
1858
1935
  */
@@ -1872,6 +1949,15 @@
1872
1949
  return [2 /*return*/, {
1873
1950
  content: spaceTrim__default["default"](function (block) { return "\n You said:\n ".concat(block(prompt.content), "\n "); }),
1874
1951
  model: 'mocked-echo',
1952
+ timing: {
1953
+ start: getCurrentIsoDate(),
1954
+ complete: getCurrentIsoDate(),
1955
+ },
1956
+ usage: {
1957
+ price: 0,
1958
+ inputTokens: 0,
1959
+ outputTokens: 0,
1960
+ },
1875
1961
  rawResponse: {
1876
1962
  note: 'This is mocked echo',
1877
1963
  },
@@ -1892,6 +1978,15 @@
1892
1978
  return [2 /*return*/, {
1893
1979
  content: spaceTrim__default["default"](function (block) { return "\n ".concat(block(prompt.content), "\n And so on...\n "); }),
1894
1980
  model: 'mocked-echo',
1981
+ timing: {
1982
+ start: getCurrentIsoDate(),
1983
+ complete: getCurrentIsoDate(),
1984
+ },
1985
+ usage: {
1986
+ price: 0,
1987
+ inputTokens: 0,
1988
+ outputTokens: 0,
1989
+ },
1895
1990
  rawResponse: {
1896
1991
  note: 'This is mocked echo',
1897
1992
  },