@promptbook/core 0.61.0-9 → 0.62.0-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.
Files changed (372) hide show
  1. package/README.md +3 -3
  2. package/esm/index.es.js +2189 -1046
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/promptbook-collection/index.d.ts +200 -30
  5. package/esm/typings/src/_packages/core.index.d.ts +9 -3
  6. package/esm/typings/src/_packages/execute-javascript.index.d.ts +1 -1
  7. package/esm/typings/src/_packages/node.index.d.ts +2 -1
  8. package/esm/typings/src/_packages/types.index.d.ts +12 -8
  9. package/esm/typings/src/_packages/utils.index.d.ts +11 -8
  10. package/esm/typings/src/cli/cli-commands/hello.d.ts +3 -0
  11. package/esm/typings/src/cli/cli-commands/make.d.ts +3 -0
  12. package/esm/typings/src/cli/cli-commands/prettify.d.ts +3 -0
  13. package/esm/typings/src/cli/promptbookCli.d.ts +1 -0
  14. package/esm/typings/src/collection/PipelineCollection.d.ts +1 -1
  15. package/esm/typings/src/collection/SimplePipelineCollection.d.ts +10 -10
  16. package/esm/typings/src/collection/constructors/createCollectionFromDirectory.d.ts +7 -7
  17. package/esm/typings/src/collection/constructors/createCollectionFromUrl.d.ts +2 -2
  18. package/esm/typings/src/commands/KNOWLEDGE/KnowledgeCommand.d.ts +2 -3
  19. package/esm/typings/src/commands/MODEL/ModelCommand.d.ts +2 -2
  20. package/esm/typings/src/commands/PERSONA/PersonaCommand.d.ts +4 -2
  21. package/esm/typings/src/commands/_BOILERPLATE/boilerplateCommandParser.d.ts +1 -0
  22. package/esm/typings/src/commands/_common/types/CommandParser.d.ts +42 -14
  23. package/esm/typings/src/config.d.ts +46 -1
  24. package/esm/typings/src/config.test.d.ts +4 -0
  25. package/esm/typings/src/conversion/pipelineJsonToString.d.ts +3 -2
  26. package/esm/typings/src/conversion/pipelineStringToJson.d.ts +9 -5
  27. package/esm/typings/src/conversion/pipelineStringToJsonSync.d.ts +6 -3
  28. package/esm/typings/src/conversion/prettify/PrettifyOptions.d.ts +2 -2
  29. package/{umd/typings/src/conversion/utils/extractParametersFromPromptTemplate.d.ts → esm/typings/src/conversion/utils/extractParameterNamesFromPromptTemplate.d.ts} +2 -2
  30. package/esm/typings/src/conversion/utils/renameParameter.d.ts +3 -3
  31. package/esm/typings/src/conversion/utils/stringifyPipelineJson.d.ts +14 -0
  32. package/esm/typings/src/conversion/validation/_importPipeline.d.ts +12 -3
  33. package/esm/typings/src/conversion/validation/validatePipeline.d.ts +10 -1
  34. package/esm/typings/src/errors/EnvironmentMismatchError.d.ts +7 -0
  35. package/esm/typings/src/errors/LimitReachedError.d.ts +7 -0
  36. package/esm/typings/src/errors/VersionMismatchError.d.ts +8 -0
  37. package/esm/typings/src/execution/CommonExecutionToolsOptions.d.ts +1 -0
  38. package/esm/typings/src/execution/LlmExecutionTools.d.ts +10 -8
  39. package/esm/typings/src/execution/PipelineExecutor.d.ts +48 -26
  40. package/esm/typings/src/execution/PromptResult.d.ts +39 -54
  41. package/esm/typings/src/execution/PromptResultUsage.d.ts +29 -0
  42. package/esm/typings/src/execution/ScriptExecutionTools.d.ts +5 -4
  43. package/esm/typings/src/execution/UncertainNumber.d.ts +18 -0
  44. package/esm/typings/src/execution/UserInterfaceTools.d.ts +5 -5
  45. package/esm/typings/src/execution/createPipelineExecutor.d.ts +31 -3
  46. package/esm/typings/src/execution/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +2 -2
  47. package/esm/typings/src/execution/utils/addUsage.d.ts +58 -2
  48. package/esm/typings/src/execution/utils/computeUsageCounts.d.ts +1 -1
  49. package/esm/typings/src/execution/utils/forEachAsync.d.ts +1 -1
  50. package/esm/typings/src/execution/utils/uncertainNumber.d.ts +1 -1
  51. package/esm/typings/src/execution/utils/usageToHuman.d.ts +15 -0
  52. package/esm/typings/src/execution/utils/usageToWorktime.d.ts +2 -2
  53. package/esm/typings/src/formats/_common/FormatDefinition.d.ts +1 -1
  54. package/esm/typings/src/formats/csv/ListFormatDefinition.d.ts +2 -1
  55. package/esm/typings/src/formats/json/JsonFormatDefinition.d.ts +2 -1
  56. package/esm/typings/src/formats/list/ListFormatDefinition.d.ts +2 -1
  57. package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +1 -0
  58. package/esm/typings/src/knowledge/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +1 -1
  59. package/esm/typings/src/knowledge/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +3 -0
  60. package/esm/typings/src/knowledge/prepare-knowledge/_common/Scraper.d.ts +27 -0
  61. package/esm/typings/src/knowledge/prepare-knowledge/_common/prepareKnowledgePieces.d.ts +18 -0
  62. package/esm/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +9 -23
  63. package/esm/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts +3 -0
  64. package/esm/typings/src/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +8 -14
  65. package/esm/typings/src/llm-providers/_common/createLlmToolsFromEnv.d.ts +31 -0
  66. package/esm/typings/src/llm-providers/_common/getLlmToolsForCli.d.ts +11 -0
  67. package/esm/typings/src/llm-providers/_common/getLlmToolsForTestingAndScriptsAndPlayground.d.ts +12 -0
  68. package/esm/typings/src/llm-providers/_common/utils/cache/CacheItem.d.ts +29 -0
  69. package/esm/typings/src/llm-providers/_common/utils/cache/CacheLlmToolsOptions.d.ts +10 -0
  70. package/esm/typings/src/llm-providers/_common/utils/cache/cacheLlmTools.d.ts +19 -0
  71. package/esm/typings/src/llm-providers/_common/utils/count-total-usage/LlmExecutionToolsWithTotalUsage.d.ts +14 -0
  72. package/esm/typings/src/llm-providers/_common/utils/count-total-usage/countTotalUsage.d.ts +17 -0
  73. package/esm/typings/src/llm-providers/_common/utils/count-total-usage/limitTotalUsage.d.ts +35 -0
  74. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +4 -3
  75. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +1 -1
  76. package/esm/typings/src/llm-providers/anthropic-claude/anthropic-claude-models.d.ts +3 -3
  77. package/esm/typings/src/llm-providers/anthropic-claude/playground/playground.d.ts +3 -0
  78. package/esm/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionTools.d.ts +5 -4
  79. package/esm/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +5 -5
  80. package/esm/typings/src/llm-providers/azure-openai/playground/playground.d.ts +3 -0
  81. package/esm/typings/src/llm-providers/langtail/LangtailExecutionTools.d.ts +3 -0
  82. package/esm/typings/src/llm-providers/langtail/LangtailExecutionToolsOptions.d.ts +1 -1
  83. package/esm/typings/src/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +5 -5
  84. package/esm/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +7 -7
  85. package/esm/typings/src/llm-providers/multiple/MultipleLlmExecutionTools.d.ts +12 -8
  86. package/esm/typings/src/llm-providers/multiple/joinLlmExecutionTools.d.ts +3 -0
  87. package/esm/typings/src/llm-providers/openai/OpenAiExecutionTools.d.ts +7 -6
  88. package/esm/typings/src/llm-providers/openai/OpenAiExecutionToolsOptions.d.ts +1 -1
  89. package/esm/typings/src/llm-providers/openai/computeOpenaiUsage.d.ts +1 -1
  90. package/esm/typings/src/llm-providers/openai/computeUsage.d.ts +2 -2
  91. package/esm/typings/src/llm-providers/openai/openai-models.d.ts +3 -2
  92. package/esm/typings/src/llm-providers/openai/playground/playground.d.ts +3 -0
  93. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +10 -8
  94. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionToolsOptions.d.ts +1 -1
  95. package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +2 -2
  96. package/esm/typings/src/personas/preparePersona.d.ts +15 -0
  97. package/esm/typings/src/prepare/PrepareOptions.d.ts +22 -0
  98. package/esm/typings/src/prepare/isPipelinePrepared.d.ts +14 -0
  99. package/esm/typings/src/prepare/preparePipeline.d.ts +16 -0
  100. package/esm/typings/src/prepare/prepareTemplates.d.ts +31 -0
  101. package/esm/typings/src/prepare/unpreparePipeline.d.ts +11 -0
  102. package/esm/typings/src/scripting/javascript/JavascriptExecutionToolsOptions.d.ts +1 -1
  103. package/esm/typings/src/scripting/javascript/utils/preserve.d.ts +1 -1
  104. package/esm/typings/src/scripting/javascript/utils/unknownToString.d.ts +2 -1
  105. package/esm/typings/src/storage/_common/PromptbookStorage.d.ts +25 -0
  106. package/esm/typings/src/storage/_common/PromptbookStorage.test-type.d.ts +5 -0
  107. package/esm/typings/src/storage/files-storage/FilesStorage.d.ts +30 -0
  108. package/esm/typings/src/storage/files-storage/FilesStorageOptions.d.ts +13 -0
  109. package/esm/typings/src/storage/files-storage/utils/nameToSubfolderPath.d.ts +7 -0
  110. package/esm/typings/src/storage/local-storage/getLocalStorage.d.ts +9 -0
  111. package/esm/typings/src/storage/local-storage/getSessionStorage.d.ts +9 -0
  112. package/esm/typings/src/storage/memory/MemoryStorage.d.ts +34 -0
  113. package/esm/typings/src/storage/utils/PrefixStorage.d.ts +26 -0
  114. package/esm/typings/src/storage/utils/makePromptbookStorageFromWebStorage.d.ts +11 -0
  115. package/esm/typings/src/types/ModelRequirements.d.ts +71 -11
  116. package/esm/typings/src/types/ModelVariant.d.ts +14 -0
  117. package/esm/typings/src/types/PipelineJson/Expectations.d.ts +6 -2
  118. package/esm/typings/src/types/PipelineJson/KnowledgePieceJson.d.ts +70 -0
  119. package/esm/typings/src/types/PipelineJson/KnowledgeSourceJson.d.ts +27 -0
  120. package/esm/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +10 -1
  121. package/esm/typings/src/types/PipelineJson/PersonaJson.d.ts +41 -0
  122. package/esm/typings/src/types/PipelineJson/PipelineJson.d.ts +31 -5
  123. package/esm/typings/src/types/PipelineJson/PreparationJson.d.ts +22 -0
  124. package/esm/typings/src/types/PipelineJson/PromptDialogJson.d.ts +8 -2
  125. package/esm/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +5 -1
  126. package/esm/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +23 -4
  127. package/esm/typings/src/types/PipelineJson/PromptTemplateParameterJson.d.ts +12 -2
  128. package/esm/typings/src/types/PipelineJson/ScriptJson.d.ts +7 -2
  129. package/esm/typings/src/types/PipelineJson/SimpleTemplateJson.d.ts +7 -2
  130. package/esm/typings/src/types/PipelineString.d.ts +1 -1
  131. package/esm/typings/src/types/Prompt.d.ts +54 -10
  132. package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +4 -4
  133. package/esm/typings/src/types/execution-report/ExecutionReportStringOptions.d.ts +3 -3
  134. package/esm/typings/src/types/execution-report/countWorkingDuration.d.ts +1 -1
  135. package/esm/typings/src/types/typeAliases.d.ts +114 -15
  136. package/esm/typings/src/utils/FromtoItems.d.ts +3 -3
  137. package/esm/typings/src/utils/currentDate.d.ts +7 -0
  138. package/esm/typings/src/utils/deepClone.d.ts +9 -0
  139. package/esm/typings/src/utils/deepFreeze.d.ts +22 -0
  140. package/esm/typings/src/utils/emojis.d.ts +2 -2
  141. package/esm/typings/src/utils/{extractParameters.d.ts → extractParameterNames.d.ts} +2 -2
  142. package/esm/typings/src/utils/formatNumber.d.ts +1 -1
  143. package/esm/typings/src/utils/isRunningInWhatever.d.ts +3 -0
  144. package/esm/typings/src/utils/markdown/addAutoGeneratedSection.d.ts +3 -3
  145. package/esm/typings/src/utils/markdown/createMarkdownChart.d.ts +7 -10
  146. package/esm/typings/src/utils/markdown/createMarkdownTable.d.ts +1 -1
  147. package/esm/typings/src/utils/markdown/extractAllBlocksFromMarkdown-real.test.d.ts +1 -0
  148. package/esm/typings/src/utils/markdown/extractAllBlocksFromMarkdown.d.ts +9 -2
  149. package/esm/typings/src/utils/markdown/extractOneBlockFromMarkdown.d.ts +1 -1
  150. package/esm/typings/src/utils/markdown/parseMarkdownSection.d.ts +3 -3
  151. package/esm/typings/src/utils/normalization/parseKeywords.d.ts +2 -1
  152. package/esm/typings/src/utils/organization/TODO_USE.d.ts +12 -0
  153. package/esm/typings/src/utils/organization/TODO_any.d.ts +6 -0
  154. package/esm/typings/src/utils/organization/TODO_object.d.ts +6 -0
  155. package/esm/typings/src/utils/organization/TODO_unknown.d.ts +6 -0
  156. package/esm/typings/src/utils/organization/___.d.ts +6 -0
  157. package/{umd/typings/src/utils → esm/typings/src/utils/organization}/just.d.ts +5 -1
  158. package/esm/typings/src/utils/organization/keepUnused.d.ts +16 -0
  159. package/esm/typings/src/utils/organization/really_any.d.ts +6 -0
  160. package/esm/typings/src/utils/organization/really_unknown.d.ts +6 -0
  161. package/esm/typings/src/utils/random/randomSeed.d.ts +7 -0
  162. package/esm/typings/src/{execution/utils → utils}/replaceParameters.d.ts +2 -4
  163. package/esm/typings/src/utils/replaceParameters.test.d.ts +1 -0
  164. package/esm/typings/src/utils/sets/difference.d.ts +3 -0
  165. package/esm/typings/src/utils/validators/email/isValidEmail.d.ts +2 -1
  166. package/esm/typings/src/utils/validators/filePath/isValidFilePath.d.ts +2 -1
  167. package/esm/typings/src/utils/validators/javascriptName/isValidJavascriptName.d.ts +2 -1
  168. package/esm/typings/src/utils/validators/semanticVersion/isValidPromptbookVersion.d.ts +2 -1
  169. package/esm/typings/src/utils/validators/semanticVersion/isValidSemanticVersion.d.ts +2 -1
  170. package/esm/typings/src/utils/validators/url/isValidPipelineUrl.d.ts +2 -1
  171. package/esm/typings/src/utils/validators/url/isValidUrl.d.ts +2 -1
  172. package/esm/typings/src/utils/validators/uuid/isValidUuid.d.ts +2 -1
  173. package/package.json +2 -2
  174. package/umd/index.umd.js +2193 -1045
  175. package/umd/index.umd.js.map +1 -1
  176. package/umd/typings/promptbook-collection/index.d.ts +200 -30
  177. package/umd/typings/src/_packages/core.index.d.ts +9 -3
  178. package/umd/typings/src/_packages/execute-javascript.index.d.ts +1 -1
  179. package/umd/typings/src/_packages/node.index.d.ts +2 -1
  180. package/umd/typings/src/_packages/types.index.d.ts +12 -8
  181. package/umd/typings/src/_packages/utils.index.d.ts +11 -8
  182. package/umd/typings/src/cli/cli-commands/hello.d.ts +3 -0
  183. package/umd/typings/src/cli/cli-commands/make.d.ts +3 -0
  184. package/umd/typings/src/cli/cli-commands/prettify.d.ts +3 -0
  185. package/umd/typings/src/cli/promptbookCli.d.ts +1 -0
  186. package/umd/typings/src/collection/PipelineCollection.d.ts +1 -1
  187. package/umd/typings/src/collection/SimplePipelineCollection.d.ts +10 -10
  188. package/umd/typings/src/collection/constructors/createCollectionFromDirectory.d.ts +7 -7
  189. package/umd/typings/src/collection/constructors/createCollectionFromUrl.d.ts +2 -2
  190. package/umd/typings/src/commands/KNOWLEDGE/KnowledgeCommand.d.ts +2 -3
  191. package/umd/typings/src/commands/MODEL/ModelCommand.d.ts +2 -2
  192. package/umd/typings/src/commands/PERSONA/PersonaCommand.d.ts +4 -2
  193. package/umd/typings/src/commands/_BOILERPLATE/boilerplateCommandParser.d.ts +1 -0
  194. package/umd/typings/src/commands/_common/types/CommandParser.d.ts +42 -14
  195. package/umd/typings/src/config.d.ts +46 -1
  196. package/umd/typings/src/config.test.d.ts +4 -0
  197. package/umd/typings/src/conversion/pipelineJsonToString.d.ts +3 -2
  198. package/umd/typings/src/conversion/pipelineStringToJson.d.ts +9 -5
  199. package/umd/typings/src/conversion/pipelineStringToJsonSync.d.ts +6 -3
  200. package/umd/typings/src/conversion/prettify/PrettifyOptions.d.ts +2 -2
  201. package/{esm/typings/src/conversion/utils/extractParametersFromPromptTemplate.d.ts → umd/typings/src/conversion/utils/extractParameterNamesFromPromptTemplate.d.ts} +2 -2
  202. package/umd/typings/src/conversion/utils/extractParameterNamesFromPromptTemplate.test.d.ts +1 -0
  203. package/umd/typings/src/conversion/utils/renameParameter.d.ts +3 -3
  204. package/umd/typings/src/conversion/utils/stringifyPipelineJson.d.ts +14 -0
  205. package/umd/typings/src/conversion/utils/stringifyPipelineJson.test.d.ts +1 -0
  206. package/umd/typings/src/conversion/validation/_importPipeline.d.ts +12 -3
  207. package/umd/typings/src/conversion/validation/validatePipeline.d.ts +10 -1
  208. package/umd/typings/src/errors/EnvironmentMismatchError.d.ts +7 -0
  209. package/umd/typings/src/errors/LimitReachedError.d.ts +7 -0
  210. package/umd/typings/src/errors/VersionMismatchError.d.ts +8 -0
  211. package/umd/typings/src/execution/CommonExecutionToolsOptions.d.ts +1 -0
  212. package/umd/typings/src/execution/LlmExecutionTools.d.ts +10 -8
  213. package/umd/typings/src/execution/PipelineExecutor.d.ts +48 -26
  214. package/umd/typings/src/execution/PromptResult.d.ts +39 -54
  215. package/umd/typings/src/execution/PromptResultUsage.d.ts +29 -0
  216. package/umd/typings/src/execution/ScriptExecutionTools.d.ts +5 -4
  217. package/umd/typings/src/execution/UncertainNumber.d.ts +18 -0
  218. package/umd/typings/src/execution/UserInterfaceTools.d.ts +5 -5
  219. package/umd/typings/src/execution/createPipelineExecutor.d.ts +31 -3
  220. package/umd/typings/src/execution/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +2 -2
  221. package/umd/typings/src/execution/utils/addUsage.d.ts +58 -2
  222. package/umd/typings/src/execution/utils/computeUsageCounts.d.ts +1 -1
  223. package/umd/typings/src/execution/utils/forEachAsync.d.ts +1 -1
  224. package/umd/typings/src/execution/utils/uncertainNumber.d.ts +1 -1
  225. package/umd/typings/src/execution/utils/usageToHuman.d.ts +15 -0
  226. package/umd/typings/src/execution/utils/usageToHuman.test.d.ts +1 -0
  227. package/umd/typings/src/execution/utils/usageToWorktime.d.ts +2 -2
  228. package/umd/typings/src/formats/_common/FormatDefinition.d.ts +1 -1
  229. package/umd/typings/src/formats/csv/ListFormatDefinition.d.ts +2 -1
  230. package/umd/typings/src/formats/json/JsonFormatDefinition.d.ts +2 -1
  231. package/umd/typings/src/formats/list/ListFormatDefinition.d.ts +2 -1
  232. package/umd/typings/src/formats/xml/XmlFormatDefinition.d.ts +1 -0
  233. package/umd/typings/src/knowledge/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +1 -1
  234. package/umd/typings/src/knowledge/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +3 -0
  235. package/umd/typings/src/knowledge/prepare-knowledge/_common/Scraper.d.ts +27 -0
  236. package/umd/typings/src/knowledge/prepare-knowledge/_common/prepareKnowledgePieces.d.ts +18 -0
  237. package/umd/typings/src/knowledge/prepare-knowledge/_common/prepareKnowledgePieces.test.d.ts +1 -0
  238. package/umd/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +9 -23
  239. package/umd/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts +3 -0
  240. package/umd/typings/src/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +8 -14
  241. package/umd/typings/src/llm-providers/_common/createLlmToolsFromEnv.d.ts +31 -0
  242. package/umd/typings/src/llm-providers/_common/getLlmToolsForCli.d.ts +11 -0
  243. package/umd/typings/src/llm-providers/_common/getLlmToolsForTestingAndScriptsAndPlayground.d.ts +12 -0
  244. package/umd/typings/src/llm-providers/_common/utils/cache/CacheItem.d.ts +29 -0
  245. package/umd/typings/src/llm-providers/_common/utils/cache/CacheLlmToolsOptions.d.ts +10 -0
  246. package/umd/typings/src/llm-providers/_common/utils/cache/cacheLlmTools.d.ts +19 -0
  247. package/umd/typings/src/llm-providers/_common/utils/count-total-usage/LlmExecutionToolsWithTotalUsage.d.ts +14 -0
  248. package/umd/typings/src/llm-providers/_common/utils/count-total-usage/countTotalUsage.d.ts +17 -0
  249. package/umd/typings/src/llm-providers/_common/utils/count-total-usage/limitTotalUsage.d.ts +35 -0
  250. package/umd/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +4 -3
  251. package/umd/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +1 -1
  252. package/umd/typings/src/llm-providers/anthropic-claude/anthropic-claude-models.d.ts +3 -3
  253. package/umd/typings/src/llm-providers/anthropic-claude/playground/playground.d.ts +3 -0
  254. package/umd/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionTools.d.ts +5 -4
  255. package/umd/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +5 -5
  256. package/umd/typings/src/llm-providers/azure-openai/playground/playground.d.ts +3 -0
  257. package/umd/typings/src/llm-providers/langtail/LangtailExecutionTools.d.ts +3 -0
  258. package/umd/typings/src/llm-providers/langtail/LangtailExecutionToolsOptions.d.ts +1 -1
  259. package/umd/typings/src/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +5 -5
  260. package/umd/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +7 -7
  261. package/umd/typings/src/llm-providers/multiple/MultipleLlmExecutionTools.d.ts +12 -8
  262. package/umd/typings/src/llm-providers/multiple/joinLlmExecutionTools.d.ts +3 -0
  263. package/umd/typings/src/llm-providers/openai/OpenAiExecutionTools.d.ts +7 -6
  264. package/umd/typings/src/llm-providers/openai/OpenAiExecutionToolsOptions.d.ts +1 -1
  265. package/umd/typings/src/llm-providers/openai/computeOpenaiUsage.d.ts +1 -1
  266. package/umd/typings/src/llm-providers/openai/computeUsage.d.ts +2 -2
  267. package/umd/typings/src/llm-providers/openai/openai-models.d.ts +3 -2
  268. package/umd/typings/src/llm-providers/openai/playground/playground.d.ts +3 -0
  269. package/umd/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +10 -8
  270. package/umd/typings/src/llm-providers/remote/RemoteLlmExecutionToolsOptions.d.ts +1 -1
  271. package/umd/typings/src/llm-providers/remote/startRemoteServer.d.ts +2 -2
  272. package/umd/typings/src/personas/preparePersona.d.ts +15 -0
  273. package/umd/typings/src/personas/preparePersona.test.d.ts +1 -0
  274. package/umd/typings/src/prepare/PrepareOptions.d.ts +22 -0
  275. package/umd/typings/src/prepare/isPipelinePrepared.d.ts +14 -0
  276. package/umd/typings/src/prepare/isPipelinePrepared.test.d.ts +1 -0
  277. package/umd/typings/src/prepare/preparePipeline.d.ts +16 -0
  278. package/umd/typings/src/prepare/prepareTemplates.d.ts +31 -0
  279. package/umd/typings/src/prepare/unpreparePipeline.d.ts +11 -0
  280. package/umd/typings/src/scripting/javascript/JavascriptExecutionToolsOptions.d.ts +1 -1
  281. package/umd/typings/src/scripting/javascript/utils/preserve.d.ts +1 -1
  282. package/umd/typings/src/scripting/javascript/utils/unknownToString.d.ts +2 -1
  283. package/umd/typings/src/storage/_common/PromptbookStorage.d.ts +25 -0
  284. package/umd/typings/src/storage/_common/PromptbookStorage.test-type.d.ts +5 -0
  285. package/umd/typings/src/storage/files-storage/FilesStorage.d.ts +30 -0
  286. package/umd/typings/src/storage/files-storage/FilesStorageOptions.d.ts +13 -0
  287. package/umd/typings/src/storage/files-storage/utils/nameToSubfolderPath.d.ts +7 -0
  288. package/umd/typings/src/storage/files-storage/utils/nameToSubfolderPath.test.d.ts +1 -0
  289. package/umd/typings/src/storage/local-storage/getLocalStorage.d.ts +9 -0
  290. package/umd/typings/src/storage/local-storage/getSessionStorage.d.ts +9 -0
  291. package/umd/typings/src/storage/memory/MemoryStorage.d.ts +34 -0
  292. package/umd/typings/src/storage/utils/PrefixStorage.d.ts +26 -0
  293. package/umd/typings/src/storage/utils/makePromptbookStorageFromWebStorage.d.ts +11 -0
  294. package/umd/typings/src/types/ModelRequirements.d.ts +71 -11
  295. package/umd/typings/src/types/ModelVariant.d.ts +14 -0
  296. package/umd/typings/src/types/PipelineJson/Expectations.d.ts +6 -2
  297. package/umd/typings/src/types/PipelineJson/KnowledgePieceJson.d.ts +70 -0
  298. package/umd/typings/src/types/PipelineJson/KnowledgeSourceJson.d.ts +27 -0
  299. package/umd/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +10 -1
  300. package/umd/typings/src/types/PipelineJson/PersonaJson.d.ts +41 -0
  301. package/umd/typings/src/types/PipelineJson/PipelineJson.d.ts +31 -5
  302. package/umd/typings/src/types/PipelineJson/PreparationJson.d.ts +22 -0
  303. package/umd/typings/src/types/PipelineJson/PromptDialogJson.d.ts +8 -2
  304. package/umd/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +5 -1
  305. package/umd/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +23 -4
  306. package/umd/typings/src/types/PipelineJson/PromptTemplateParameterJson.d.ts +12 -2
  307. package/umd/typings/src/types/PipelineJson/ScriptJson.d.ts +7 -2
  308. package/umd/typings/src/types/PipelineJson/SimpleTemplateJson.d.ts +7 -2
  309. package/umd/typings/src/types/PipelineString.d.ts +1 -1
  310. package/umd/typings/src/types/Prompt.d.ts +54 -10
  311. package/umd/typings/src/types/execution-report/ExecutionReportJson.d.ts +4 -4
  312. package/umd/typings/src/types/execution-report/ExecutionReportStringOptions.d.ts +3 -3
  313. package/umd/typings/src/types/execution-report/countWorkingDuration.d.ts +1 -1
  314. package/umd/typings/src/types/typeAliases.d.ts +114 -15
  315. package/umd/typings/src/utils/FromtoItems.d.ts +3 -3
  316. package/umd/typings/src/utils/currentDate.d.ts +7 -0
  317. package/umd/typings/src/utils/deepClone.d.ts +9 -0
  318. package/umd/typings/src/utils/deepFreeze.d.ts +22 -0
  319. package/umd/typings/src/utils/emojis.d.ts +2 -2
  320. package/umd/typings/src/utils/{extractParameters.d.ts → extractParameterNames.d.ts} +2 -2
  321. package/umd/typings/src/utils/extractParameterNames.test.d.ts +1 -0
  322. package/umd/typings/src/utils/formatNumber.d.ts +1 -1
  323. package/umd/typings/src/utils/isRunningInWhatever.d.ts +3 -0
  324. package/umd/typings/src/utils/markdown/addAutoGeneratedSection.d.ts +3 -3
  325. package/umd/typings/src/utils/markdown/createMarkdownChart.d.ts +7 -10
  326. package/umd/typings/src/utils/markdown/createMarkdownTable.d.ts +1 -1
  327. package/umd/typings/src/utils/markdown/extractAllBlocksFromMarkdown-real.test.d.ts +1 -0
  328. package/umd/typings/src/utils/markdown/extractAllBlocksFromMarkdown.d.ts +9 -2
  329. package/umd/typings/src/utils/markdown/extractOneBlockFromMarkdown.d.ts +1 -1
  330. package/umd/typings/src/utils/markdown/parseMarkdownSection.d.ts +3 -3
  331. package/umd/typings/src/utils/normalization/parseKeywords.d.ts +2 -1
  332. package/umd/typings/src/utils/organization/TODO_USE.d.ts +12 -0
  333. package/umd/typings/src/utils/organization/TODO_any.d.ts +6 -0
  334. package/umd/typings/src/utils/organization/TODO_object.d.ts +6 -0
  335. package/umd/typings/src/utils/organization/TODO_unknown.d.ts +6 -0
  336. package/umd/typings/src/utils/organization/___.d.ts +6 -0
  337. package/{esm/typings/src/utils → umd/typings/src/utils/organization}/just.d.ts +5 -1
  338. package/umd/typings/src/utils/organization/keepUnused.d.ts +16 -0
  339. package/umd/typings/src/utils/organization/really_any.d.ts +6 -0
  340. package/umd/typings/src/utils/organization/really_unknown.d.ts +6 -0
  341. package/umd/typings/src/utils/random/randomSeed.d.ts +7 -0
  342. package/umd/typings/src/{execution/utils → utils}/replaceParameters.d.ts +2 -4
  343. package/umd/typings/src/utils/replaceParameters.test.d.ts +1 -0
  344. package/umd/typings/src/utils/sets/difference.d.ts +3 -0
  345. package/umd/typings/src/utils/validators/email/isValidEmail.d.ts +2 -1
  346. package/umd/typings/src/utils/validators/filePath/isValidFilePath.d.ts +2 -1
  347. package/umd/typings/src/utils/validators/javascriptName/isValidJavascriptName.d.ts +2 -1
  348. package/umd/typings/src/utils/validators/semanticVersion/isValidPromptbookVersion.d.ts +2 -1
  349. package/umd/typings/src/utils/validators/semanticVersion/isValidSemanticVersion.d.ts +2 -1
  350. package/umd/typings/src/utils/validators/url/isValidPipelineUrl.d.ts +2 -1
  351. package/umd/typings/src/utils/validators/url/isValidUrl.d.ts +2 -1
  352. package/umd/typings/src/utils/validators/uuid/isValidUuid.d.ts +2 -1
  353. package/esm/typings/src/collection/constructors/justTestFsImport.d.ts +0 -7
  354. package/esm/typings/src/personas/personaToModelRequirements.d.ts +0 -6
  355. package/esm/typings/src/types/Parameters.d.ts +0 -14
  356. package/esm/typings/src/types/PipelineJson/KnowledgeJson.d.ts +0 -6
  357. package/esm/typings/src/types/PipelineJson/MaterialKnowledgePieceJson.d.ts +0 -28
  358. package/umd/typings/src/collection/constructors/justTestFsImport.d.ts +0 -7
  359. package/umd/typings/src/personas/personaToModelRequirements.d.ts +0 -6
  360. package/umd/typings/src/types/Parameters.d.ts +0 -14
  361. package/umd/typings/src/types/PipelineJson/KnowledgeJson.d.ts +0 -6
  362. package/umd/typings/src/types/PipelineJson/MaterialKnowledgePieceJson.d.ts +0 -28
  363. /package/esm/typings/src/conversion/utils/{extractParametersFromPromptTemplate.test.d.ts → extractParameterNamesFromPromptTemplate.test.d.ts} +0 -0
  364. /package/esm/typings/src/{execution/addPromptResultUsage.test.d.ts → conversion/utils/stringifyPipelineJson.test.d.ts} +0 -0
  365. /package/esm/typings/src/execution/utils/{replaceParameters.test.d.ts → usageToHuman.test.d.ts} +0 -0
  366. /package/esm/typings/src/{utils/extractParameters.test.d.ts → knowledge/prepare-knowledge/_common/prepareKnowledgePieces.test.d.ts} +0 -0
  367. /package/{umd/typings/src/conversion/utils/extractParametersFromPromptTemplate.test.d.ts → esm/typings/src/personas/preparePersona.test.d.ts} +0 -0
  368. /package/esm/typings/src/{utils/postprocessing → postprocessing/utils}/extractBlock.d.ts +0 -0
  369. /package/{umd/typings/src/execution/addPromptResultUsage.test.d.ts → esm/typings/src/prepare/isPipelinePrepared.test.d.ts} +0 -0
  370. /package/{umd/typings/src/execution/utils/replaceParameters.test.d.ts → esm/typings/src/storage/files-storage/utils/nameToSubfolderPath.test.d.ts} +0 -0
  371. /package/{umd/typings/src/utils/extractParameters.test.d.ts → esm/typings/src/utils/extractParameterNames.test.d.ts} +0 -0
  372. /package/umd/typings/src/{utils/postprocessing → postprocessing/utils}/extractBlock.d.ts +0 -0
package/umd/index.umd.js CHANGED
@@ -258,7 +258,7 @@
258
258
  /* Note: Not using:> name, */
259
259
  title_1 = promptTemplate.title, description_1 = promptTemplate.description,
260
260
  /* Note: dependentParameterNames, */
261
- jokers = promptTemplate.jokers, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
261
+ jokers = promptTemplate.jokerParameterNames, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessingFunctionNames, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
262
262
  pipelineString += '\n\n';
263
263
  pipelineString += "## ".concat(title_1);
264
264
  if (description_1) {
@@ -392,23 +392,120 @@
392
392
  return parameterString;
393
393
  }
394
394
  /**
395
- * TODO: !!!!! Implement new features and commands into `promptTemplateParameterJsonToString`
395
+ * TODO: !!!! Implement new features and commands into `promptTemplateParameterJsonToString`
396
396
  * TODO: [🧠] Is there a way to auto-detect missing features in pipelineJsonToString
397
- * TODO: Escape all
397
+ * TODO: [🏛] Maybe make some markdown builder
398
+ * TODO: [🏛] Escape all
398
399
  */
399
400
 
401
+ /**
402
+ * @@@
403
+ *
404
+ * @returns The same object as the input, but deeply frozen
405
+ *
406
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
407
+ */
408
+ function deepFreeze(objectValue) {
409
+ var e_1, _a;
410
+ var propertyNames = Object.getOwnPropertyNames(objectValue);
411
+ try {
412
+ for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
413
+ var propertyName = propertyNames_1_1.value;
414
+ var value = objectValue[propertyName];
415
+ if (value && typeof value === 'object') {
416
+ deepFreeze(value);
417
+ }
418
+ }
419
+ }
420
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
421
+ finally {
422
+ try {
423
+ if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
424
+ }
425
+ finally { if (e_1) throw e_1.error; }
426
+ }
427
+ return Object.freeze(objectValue);
428
+ }
429
+ /**
430
+ * @@@
431
+ * @@@
432
+ *
433
+ * @returns The same object as the input, but deeply frozen
434
+ *
435
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
436
+ */
437
+ function deepFreezeWithSameType(objectValue) {
438
+ return deepFreeze(objectValue);
439
+ }
440
+ /**
441
+ * TODO: [🔼] Export from `@promptbook/utils`
442
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
443
+ */
444
+
445
+ /**
446
+ * Returns the same value that is passed as argument.
447
+ * No side effects.
448
+ *
449
+ * Note: It can be usefull for:
450
+ *
451
+ * 1) Leveling indentation
452
+ * 2) Putting always-true or always-false conditions without getting eslint errors
453
+ *
454
+ * @param value any values
455
+ * @returns the same values
456
+ * @private within the repository
457
+ */
458
+ function just(value) {
459
+ if (value === undefined) {
460
+ return undefined;
461
+ }
462
+ return value;
463
+ }
464
+
400
465
  /**
401
466
  * The maximum number of iterations for a loops
402
467
  */
403
468
  var LOOP_LIMIT = 1000;
469
+ /**
470
+ * The maximum number of (LLM) tasks running in parallel
471
+ */
472
+ var MAX_PARALLEL_COUNT = 5;
473
+ /**
474
+ * The maximum number of attempts to execute LLM task before giving up
475
+ */
476
+ var MAX_EXECUTION_ATTEMPTS = 3;
477
+ /**
478
+ * Nonce which is used for replacing things in strings
479
+ */
480
+ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
404
481
  /**
405
482
  * The names of the parameters that are reserved for special purposes
406
483
  */
407
- var RESERVED_PARAMETER_NAMES = [
484
+ var RESERVED_PARAMETER_NAMES = deepFreeze([
485
+ 'content',
408
486
  'context',
487
+ 'knowledge',
488
+ 'samples',
489
+ 'modelName',
490
+ 'currentDate',
409
491
  // <- TODO: Add more like 'date', 'modelName',...
410
492
  // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
411
- ];
493
+ ]);
494
+ /**
495
+ * @@@
496
+ */
497
+ var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
498
+ /**
499
+ * @@@
500
+ */
501
+ var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
502
+ /*
503
+ TODO: !!! Just testing false-negative detection of [🟡][🟢][🔵][⚪] leak
504
+ */
505
+ // [🟡][🟢][🔵][⚪]
506
+ /**
507
+ * TODO: [🔼] Export all to core
508
+ */
412
509
 
413
510
  /**
414
511
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -486,7 +583,7 @@
486
583
  if ( /* version === '1.0.0' || */version === '2.0.0' || version === '3.0.0') {
487
584
  return false;
488
585
  }
489
- // <- TODO: !!!! Check isValidPromptbookVersion against PROMPTBOOK_VERSIONS
586
+ // <- TODO: !!! Check isValidPromptbookVersion against PROMPTBOOK_VERSIONS
490
587
  return true;
491
588
  }
492
589
 
@@ -611,56 +708,67 @@
611
708
  */
612
709
  function validatePipeline(pipeline) {
613
710
  // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
614
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
711
+ var e_1, _a, e_2, _b, e_3, _c;
712
+ var pipelineIdentification = (function () {
713
+ // Note: This is a 😐 implementation of [🚞]
714
+ var _ = [];
715
+ if (pipeline.sourceFile !== undefined) {
716
+ _.push("File: ".concat(pipeline.sourceFile));
717
+ }
718
+ if (pipeline.pipelineUrl !== undefined) {
719
+ _.push("Url: ".concat(pipeline.pipelineUrl));
720
+ }
721
+ return _.join('\n');
722
+ })();
615
723
  if (pipeline.pipelineUrl !== undefined && !isValidPipelineUrl(pipeline.pipelineUrl)) {
616
724
  // <- Note: [🚲]
617
- throw new PipelineLogicError("Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\""));
725
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
618
726
  }
619
727
  if (!isValidPromptbookVersion(pipeline.promptbookVersion)) {
620
728
  // <- Note: [🚲]
621
- throw new PipelineLogicError("Invalid promptbook Version \"".concat(pipeline.pipelineUrl, "\""));
729
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Invalid Promptbook Version \"".concat(pipeline.promptbookVersion, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
622
730
  }
623
731
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
624
732
  if (!Array.isArray(pipeline.parameters)) {
625
733
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
626
- throw new ParsingError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof pipeline.parameters, "\n ")));
734
+ throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.parameters` expected to be an array, but got ".concat(typeof pipeline.parameters, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
627
735
  }
628
736
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
629
737
  if (!Array.isArray(pipeline.promptTemplates)) {
630
738
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
631
- throw new ParsingError(spaceTrim.spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof pipeline.promptTemplates, "\n ")));
739
+ throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.promptTemplates` expected to be an array, but got ".concat(typeof pipeline.promptTemplates, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
632
740
  }
633
741
  var _loop_1 = function (parameter) {
634
742
  if (parameter.isInput && parameter.isOutput) {
635
- throw new PipelineLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
743
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n\n Parameter {".concat(parameter.name, "} can not be both input and output\n\n ").concat(block(pipelineIdentification), "\n "); }));
636
744
  }
637
745
  // Note: Testing that parameter is either intermediate or output BUT not created and unused
638
746
  if (!parameter.isInput &&
639
747
  !parameter.isOutput &&
640
748
  !pipeline.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
641
- throw new PipelineLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
749
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
642
750
  }
643
751
  // Note: Testing that parameter is either input or result of some template
644
752
  if (!parameter.isInput &&
645
753
  !pipeline.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
646
- throw new PipelineLogicError(spaceTrim.spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
754
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ").concat(block(pipelineIdentification), "\n "); }));
647
755
  }
648
756
  };
649
757
  try {
650
758
  // Note: Check each parameter individually
651
- for (var _e = __values(pipeline.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
652
- var parameter = _f.value;
759
+ for (var _d = __values(pipeline.parameters), _e = _d.next(); !_e.done; _e = _d.next()) {
760
+ var parameter = _e.value;
653
761
  _loop_1(parameter);
654
762
  }
655
763
  }
656
764
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
657
765
  finally {
658
766
  try {
659
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
767
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
660
768
  }
661
769
  finally { if (e_1) throw e_1.error; }
662
770
  }
663
- // Note: Check each template individually
771
+ // Note: All input parameters are defined - so that they can be used as result of some template
664
772
  var definedParameters = new Set(pipeline.parameters.filter(function (_a) {
665
773
  var isInput = _a.isInput;
666
774
  return isInput;
@@ -668,66 +776,80 @@
668
776
  var name = _a.name;
669
777
  return name;
670
778
  }));
671
- try {
672
- for (var _g = __values(pipeline.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
673
- var template = _h.value;
674
- if (definedParameters.has(template.resultingParameterName)) {
675
- throw new PipelineLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
779
+ var _loop_2 = function (template) {
780
+ var e_4, _h, e_5, _j;
781
+ if (definedParameters.has(template.resultingParameterName)) {
782
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(template.resultingParameterName, "} is defined multiple times\n\n ").concat(block(pipelineIdentification), "\n "); }));
783
+ }
784
+ if (RESERVED_PARAMETER_NAMES.includes(template.resultingParameterName)) {
785
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Parameter name {".concat(template.resultingParameterName, "} is reserved, please use different name\n\n ").concat(block(pipelineIdentification), "\n "); }));
786
+ }
787
+ definedParameters.add(template.resultingParameterName);
788
+ if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
789
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`").concat(/* <- TODO: Dynamic listing of command examples */ '', "\n\n ").concat(block(pipelineIdentification), "\n "); }));
790
+ }
791
+ if (template.jokerParameterNames && template.jokerParameterNames.length > 0) {
792
+ if (!template.expectFormat &&
793
+ !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
794
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
676
795
  }
677
- definedParameters.add(template.resultingParameterName);
678
- if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
679
- throw new PipelineLogicError(spaceTrim.spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
680
- }
681
- if (template.jokers && template.jokers.length > 0) {
682
- if (!template.expectFormat &&
683
- !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
684
- throw new PipelineLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
796
+ var _loop_4 = function (joker) {
797
+ if (!template.dependentParameterNames.includes(joker)) {
798
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in `dependentParameterNames`\n\n ").concat(block(pipelineIdentification), "\n "); }));
799
+ }
800
+ };
801
+ try {
802
+ for (var _k = (e_4 = void 0, __values(template.jokerParameterNames)), _l = _k.next(); !_l.done; _l = _k.next()) {
803
+ var joker = _l.value;
804
+ _loop_4(joker);
685
805
  }
806
+ }
807
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
808
+ finally {
686
809
  try {
687
- for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
688
- var joker = _k.value;
689
- if (!template.dependentParameterNames.includes(joker)) {
690
- throw new PipelineLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
691
- }
692
- }
810
+ if (_l && !_l.done && (_h = _k.return)) _h.call(_k);
693
811
  }
694
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
695
- finally {
696
- try {
697
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
698
- }
699
- finally { if (e_3) throw e_3.error; }
812
+ finally { if (e_4) throw e_4.error; }
813
+ }
814
+ }
815
+ if (template.expectations) {
816
+ var _loop_5 = function (unit, min, max) {
817
+ if (min !== undefined && max !== undefined && min > max) {
818
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")\n\n ").concat(block(pipelineIdentification), "\n "); }));
819
+ }
820
+ if (min !== undefined && min < 0) {
821
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Min expectation of ".concat(unit, " must be zero or positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
822
+ }
823
+ if (max !== undefined && max <= 0) {
824
+ throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n Max expectation of ".concat(unit, " must be positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
825
+ }
826
+ };
827
+ try {
828
+ for (var _m = (e_5 = void 0, __values(Object.entries(template.expectations))), _o = _m.next(); !_o.done; _o = _m.next()) {
829
+ var _p = __read(_o.value, 2), unit = _p[0], _q = _p[1], min = _q.min, max = _q.max;
830
+ _loop_5(unit, min, max);
700
831
  }
701
832
  }
702
- if (template.expectations) {
833
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
834
+ finally {
703
835
  try {
704
- for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
705
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
706
- if (min !== undefined && max !== undefined && min > max) {
707
- throw new PipelineLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
708
- }
709
- if (min !== undefined && min < 0) {
710
- throw new PipelineLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
711
- }
712
- if (max !== undefined && max <= 0) {
713
- throw new PipelineLogicError("Max expectation of ".concat(unit, " must be positive"));
714
- }
715
- }
716
- }
717
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
718
- finally {
719
- try {
720
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
721
- }
722
- finally { if (e_4) throw e_4.error; }
836
+ if (_o && !_o.done && (_j = _m.return)) _j.call(_m);
723
837
  }
838
+ finally { if (e_5) throw e_5.error; }
724
839
  }
725
840
  }
841
+ };
842
+ try {
843
+ // Note: Checking each template individually
844
+ for (var _f = __values(pipeline.promptTemplates), _g = _f.next(); !_g.done; _g = _f.next()) {
845
+ var template = _g.value;
846
+ _loop_2(template);
847
+ }
726
848
  }
727
849
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
728
850
  finally {
729
851
  try {
730
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
852
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
731
853
  }
732
854
  finally { if (e_2) throw e_2.error; }
733
855
  }
@@ -741,24 +863,42 @@
741
863
  var name = _a.name;
742
864
  return name;
743
865
  });
866
+ try {
867
+ // Note: All reserved parameters are resolved
868
+ for (var RESERVED_PARAMETER_NAMES_1 = __values(RESERVED_PARAMETER_NAMES), RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next(); !RESERVED_PARAMETER_NAMES_1_1.done; RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next()) {
869
+ var reservedParameterName = RESERVED_PARAMETER_NAMES_1_1.value;
870
+ resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), [reservedParameterName], false);
871
+ }
872
+ }
873
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
874
+ finally {
875
+ try {
876
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_c = RESERVED_PARAMETER_NAMES_1.return)) _c.call(RESERVED_PARAMETER_NAMES_1);
877
+ }
878
+ finally { if (e_3) throw e_3.error; }
879
+ }
744
880
  var unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
881
+ // <- TODO: [🧠][🥜]
745
882
  var loopLimit = LOOP_LIMIT;
746
- var _loop_2 = function () {
883
+ var _loop_3 = function () {
747
884
  if (loopLimit-- < 0) {
748
- throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePipeline`');
885
+ // Note: Really UnexpectedError not LimitReachedError - this should not happen and be caught below
886
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Loop limit reached during detection of circular dependencies in `validatePipeline`\n\n ".concat(block(pipelineIdentification), "\n "); }));
749
887
  }
750
888
  var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
751
889
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
752
890
  });
753
891
  if (currentlyResovedTemplates.length === 0) {
754
- throw new PipelineLogicError(spaceTrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters:\n Either you are using a parameter that is not defined, or there are some circular dependencies.\n\n Can not resolve:\n ".concat(block(unresovedTemplates
892
+ throw new PipelineLogicError(
893
+ // TODO: [🐎] DRY
894
+ spaceTrim.spaceTrim(function (block) { return "\n\n Can not resolve some parameters:\n Either you are using a parameter that is not defined, or there are some circular dependencies.\n\n Can not resolve:\n ".concat(block(unresovedTemplates
755
895
  .map(function (_a) {
756
896
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
757
- return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
897
+ return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
758
898
  .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
759
- .join(', '));
899
+ .join(' and '));
760
900
  })
761
- .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
901
+ .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- Parameter {".concat(name, "}"); }).join('\n')), "\n\n ").concat(block(pipelineIdentification), "\n "); }));
762
902
  }
763
903
  resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
764
904
  var resultingParameterName = _a.resultingParameterName;
@@ -767,7 +907,7 @@
767
907
  unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
768
908
  };
769
909
  while (unresovedTemplates.length > 0) {
770
- _loop_2();
910
+ _loop_3();
771
911
  }
772
912
  return pipeline;
773
913
  }
@@ -780,7 +920,16 @@
780
920
  * > * It checks:
781
921
  * > * - it has a valid structure
782
922
  * > * - ...
783
- * > ex port function validatePipeline(promptbook: unknown): asserts promptbook is PipelineJson {
923
+ * > ex port function validatePipeline(promptbook: really_unknown): asserts promptbook is PipelineJson {
924
+ */
925
+ /**
926
+ * TODO: [🐣] !!!! Validate that all samples match expectations
927
+ * TODO: [🐣][🐝] !!!! Validate that knowledge is valid (non-void)
928
+ * TODO: [🐣] !!!! Validate that persona can be used only with CHAT variant
929
+ * TODO: [🐣] !!!! Validate that parameter with reserved name not used RESERVED_PARAMETER_NAMES
930
+ * TODO: [🐣] !!!! Validate that reserved parameter is not used as joker
931
+ * TODO: [🧠] Validation not only logic itself but imports around - files and websites and rerefenced pipelines exists
932
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
784
933
  */
785
934
 
786
935
  /**
@@ -812,79 +961,137 @@
812
961
  }(Error));
813
962
 
814
963
  /**
815
- * Library of promptbooks that groups together promptbooks for an application.
816
- * This implementation is a very thin wrapper around the Array / Map of promptbooks.
964
+ * Parses the template and returns the list of all parameter names
965
+ *
966
+ * @param template the template with parameters in {curly} braces
967
+ * @returns the list of parameter names
968
+ */
969
+ function extractParameterNames(template) {
970
+ var e_1, _a;
971
+ var matches = template.matchAll(/{\w+}/g);
972
+ var parameterNames = new Set();
973
+ try {
974
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
975
+ var match = matches_1_1.value;
976
+ var parameterName = match[0].slice(1, -1);
977
+ parameterNames.add(parameterName);
978
+ }
979
+ }
980
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
981
+ finally {
982
+ try {
983
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
984
+ }
985
+ finally { if (e_1) throw e_1.error; }
986
+ }
987
+ return parameterNames;
988
+ }
989
+
990
+ /**
991
+ * Unprepare just strips the preparation data of the pipeline
992
+ */
993
+ function unpreparePipeline(pipeline) {
994
+ var personas = pipeline.personas, knowledgeSources = pipeline.knowledgeSources, promptTemplates = pipeline.promptTemplates;
995
+ personas = personas.map(function (persona) { return (__assign(__assign({}, persona), { modelRequirements: undefined, preparationIds: undefined })); });
996
+ knowledgeSources = knowledgeSources.map(function (knowledgeSource) { return (__assign(__assign({}, knowledgeSource), { preparationIds: undefined })); });
997
+ promptTemplates = promptTemplates.map(function (promptTemplate) {
998
+ var dependentParameterNames = promptTemplate.dependentParameterNames;
999
+ var parameterNames = extractParameterNames(promptTemplate.preparedContent || '');
1000
+ dependentParameterNames = dependentParameterNames.filter(function (dependentParameterName) { return !parameterNames.has(dependentParameterName); });
1001
+ var promptTemplateUnprepared = __assign(__assign({}, promptTemplate), { dependentParameterNames: dependentParameterNames });
1002
+ delete promptTemplateUnprepared.preparedContent;
1003
+ return promptTemplateUnprepared;
1004
+ });
1005
+ return __assign(__assign({}, pipeline), { promptTemplates: promptTemplates, knowledgeSources: knowledgeSources, knowledgePieces: [], personas: personas, preparations: [] });
1006
+ }
1007
+ /**
1008
+ * TODO: [🔼] !!! Export via `@promptbook/core`
1009
+ * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
1010
+ * TODO: Write tests for `preparePipeline`
1011
+ * TODO: [🍙] Make some standart order of json properties
1012
+ */
1013
+
1014
+ /**
1015
+ * Library of pipelines that groups together pipelines for an application.
1016
+ * This implementation is a very thin wrapper around the Array / Map of pipelines.
817
1017
  *
818
1018
  * @private use `createCollectionFromJson` instead
819
- * @see https://github.com/webgptorg/promptbook#promptbook-collection
1019
+ * @see https://github.com/webgptorg/pipeline#pipeline-collection
820
1020
  */
821
1021
  var SimplePipelineCollection = /** @class */ (function () {
822
1022
  /**
823
- * Constructs a pipeline collection from promptbooks
1023
+ * Constructs a pipeline collection from pipelines
824
1024
  *
825
- * @param promptbooks !!!
1025
+ * @param pipelines @@@
826
1026
  *
827
1027
  * @private Use instead `createCollectionFromJson`
828
- * Note: During the construction logic of all promptbooks are validated
1028
+ * Note: During the construction logic of all pipelines are validated
829
1029
  * Note: It is not recommended to use this constructor directly, use `createCollectionFromJson` *(or other variant)* instead
830
1030
  */
831
1031
  function SimplePipelineCollection() {
832
1032
  var e_1, _a;
833
- var promptbooks = [];
1033
+ var pipelines = [];
834
1034
  for (var _i = 0; _i < arguments.length; _i++) {
835
- promptbooks[_i] = arguments[_i];
1035
+ pipelines[_i] = arguments[_i];
836
1036
  }
837
1037
  this.collection = new Map();
838
1038
  try {
839
- for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
840
- var promptbook = promptbooks_1_1.value;
841
- if (promptbook.pipelineUrl === undefined) {
842
- throw new ReferenceError$1(spaceTrim.spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n File:\n ").concat(promptbook.sourceFile || 'Unknown', "\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 pipeline collection\n\n ")));
1039
+ for (var pipelines_1 = __values(pipelines), pipelines_1_1 = pipelines_1.next(); !pipelines_1_1.done; pipelines_1_1 = pipelines_1.next()) {
1040
+ var pipeline = pipelines_1_1.value;
1041
+ // TODO: [👠] DRY
1042
+ if (pipeline.pipelineUrl === undefined) {
1043
+ throw new ReferenceError$1(spaceTrim.spaceTrim("\n Pipeline with name \"".concat(pipeline.title, "\" does not have defined URL\n\n File:\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: Pipelines without URLs are called anonymous pipelines\n They can be used as standalone pipelines, but they cannot be referenced by other pipelines\n And also they cannot be used in the pipeline collection\n\n ")));
843
1044
  }
844
- validatePipeline(promptbook);
1045
+ // Note: [🐨]
1046
+ validatePipeline(pipeline);
1047
+ // TODO: [🦄] DRY
845
1048
  // Note: [🦄]
846
- if (this.collection.has(promptbook.pipelineUrl) &&
847
- pipelineJsonToString(promptbook) !== pipelineJsonToString(this.collection.get(promptbook.pipelineUrl))) {
848
- var existing = this.collection.get(promptbook.pipelineUrl);
849
- throw new ReferenceError$1(spaceTrim.spaceTrim("\n Promptbook with URL \"".concat(promptbook.pipelineUrl, "\" is already in the collection\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(promptbook.sourceFile || 'Unknown', "\n\n Note: Promptbooks with the same URL are not allowed\n Only exepction is when the promptbooks are identical\n\n ")));
1049
+ if (
1050
+ // TODO: [🐽]
1051
+ this.collection.has(pipeline.pipelineUrl) &&
1052
+ pipelineJsonToString(unpreparePipeline(pipeline)) !==
1053
+ pipelineJsonToString(unpreparePipeline(this.collection.get(pipeline.pipelineUrl)))) {
1054
+ var existing = this.collection.get(pipeline.pipelineUrl);
1055
+ throw new ReferenceError$1(spaceTrim.spaceTrim("\n Pipeline with URL \"".concat(pipeline.pipelineUrl, "\" is already in the collection\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: Pipelines with the same URL are not allowed\n Only exepction is when the pipelines are identical\n\n ")));
850
1056
  }
851
- this.collection.set(promptbook.pipelineUrl, promptbook);
1057
+ // Note: [🧠] Overwrite existing pipeline with the same URL
1058
+ this.collection.set(pipeline.pipelineUrl, pipeline);
852
1059
  }
853
1060
  }
854
1061
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
855
1062
  finally {
856
1063
  try {
857
- if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
1064
+ if (pipelines_1_1 && !pipelines_1_1.done && (_a = pipelines_1.return)) _a.call(pipelines_1);
858
1065
  }
859
1066
  finally { if (e_1) throw e_1.error; }
860
1067
  }
861
1068
  }
862
1069
  /**
863
- * Gets all promptbooks in the collection
1070
+ * Gets all pipelines in the collection
864
1071
  */
865
1072
  SimplePipelineCollection.prototype.listPipelines = function () {
866
1073
  return Array.from(this.collection.keys());
867
1074
  };
868
1075
  /**
869
- * Gets promptbook by its URL
1076
+ * Gets pipeline by its URL
870
1077
  *
871
1078
  * Note: This is not a direct fetching from the URL, but a lookup in the collection
872
1079
  */
873
1080
  SimplePipelineCollection.prototype.getPipelineByUrl = function (url) {
874
1081
  var _this = this;
875
- var promptbook = this.collection.get(url);
876
- if (!promptbook) {
1082
+ var pipeline = this.collection.get(url);
1083
+ if (!pipeline) {
877
1084
  if (this.listPipelines().length === 0) {
878
- throw new NotFoundError(spaceTrim.spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
1085
+ throw new NotFoundError(spaceTrim.spaceTrim("\n Pipeline with url \"".concat(url, "\" not found\n\n No pipelines available\n ")));
879
1086
  }
880
- throw new NotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPipelines()
1087
+ throw new NotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Pipeline with url \"".concat(url, "\" not found\n\n Available pipelines:\n ").concat(block(_this.listPipelines()
881
1088
  .map(function (pipelineUrl) { return "- ".concat(pipelineUrl); })
882
1089
  .join('\n')), "\n\n "); }));
883
1090
  }
884
- return promptbook;
1091
+ return pipeline;
885
1092
  };
886
1093
  /**
887
- * Checks whether given prompt was defined in any promptbook in the collection
1094
+ * Checks whether given prompt was defined in any pipeline in the collection
888
1095
  */
889
1096
  SimplePipelineCollection.prototype.isResponsibleForPrompt = function (prompt) {
890
1097
  return true;
@@ -1126,7 +1333,199 @@
1126
1333
  // <- [🩻]
1127
1334
  ];
1128
1335
 
1129
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.61.0-8",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledge:[{name:"u",title:"U",content:"I",keywords:["E"],index:[{modelName:"mocked-facked",position:[-0.3453973828882342,-0.3141628639413514,-0.8603436743004793,0.6728018673301928,-0.9605637165286502,0.7088842482142073,-0.34494577682844785,0.6010842971317221,-0.04295184681439013,0.8007417470733849,-0.23697328816097052,-0.017493253490395322,-0.9090876357692248,0.74125281768158,-0.9076135469899738,0.9952187932256056,0.008511414426171982,0.5998098130334704,0.045404859001831,-0.42206311138329067,-0.5595152486651154,-0.47725505806219415,-0.5436970583373815,0.35363956333229396,-0.12693763438108796]}],sources:[{title:"Markdown document",href:"#"}]}],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.61.0-8",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"keywords"}],knowledge:[{name:"i",title:"I",content:"E",keywords:["M"],index:[{modelName:"mocked-facked",position:[0.19147483113917518,-0.7177848535117057,0.3683965389877679,0.6207547823892003,0.7072367024009303,-0.7579307453180899,-0.5822315355632282,-0.0416838366681902,-0.4651057611764511,-0.611441597920364,-0.601121121299252,0.05822098791382446,0.2250330827083613,0.4148105817390326,0.18938319562858874,0.6690313746510337,0.10932010447666451,0.1045178968783178,0.1588601067610882,0.28034513708196984,0.0025333686672688316,-0.04321089965612668,0.4704061207491228,-0.1911083145243362,0.5679872213373209]}],sources:[{title:"Markdown document",href:"#"}]}],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.61.0-8",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],blockType:"PROMPT_TEMPLATE",expectations:{words:{min:1,max:8}},modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {content}",resultingParameterName:"title"}],knowledge:[{name:"n",title:"N",content:"C",keywords:["D"],index:[{modelName:"mocked-facked",position:[0.10612659748079523,-0.7856803833631183,0.1409428165697526,0.8467355715298632,0.6864582688662693,-0.4921408940660612,0.14478588507439571,-0.4758184993922012,-0.23667915152261454,0.7956308792170788,0.11585260435556588,-0.26155156872769236,-0.6564981977915396,-0.609952147469647,0.31272094148873597,0.6145702220978193,-0.4267046006405488,0.024211888036476115,0.5375719207498166,0.6604556025691424,-0.7850520501178093,0.06782180782693414,-0.6939426176190544,-0.3795848182232091,0.7308270240270351]}],sources:[{title:"Markdown document",href:"#"}]}],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"}];
1336
+ /**
1337
+ * @@@
1338
+ */
1339
+ function deepClone(objectValue) {
1340
+ return JSON.parse(JSON.stringify(objectValue));
1341
+ /*
1342
+ TODO: [🧠] Is there a better implementation?
1343
+ > const propertyNames = Object.getOwnPropertyNames(objectValue);
1344
+ > for (const propertyName of propertyNames) {
1345
+ > const value = (objectValue as really_any)[propertyName];
1346
+ > if (value && typeof value === 'object') {
1347
+ > deepClone(value);
1348
+ > }
1349
+ > }
1350
+ > return Object.assign({}, objectValue);
1351
+ */
1352
+ }
1353
+ /**
1354
+ * TODO: [🔼] Export from `@promptbook/utils`
1355
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
1356
+ */
1357
+
1358
+ /**
1359
+ * @@@
1360
+ *
1361
+ * TODO: [🔼] Export with addUsage
1362
+ */
1363
+ var ZERO_USAGE = deepFreeze({
1364
+ price: { value: 0 },
1365
+ input: {
1366
+ tokensCount: { value: 0 },
1367
+ charactersCount: { value: 0 },
1368
+ wordsCount: { value: 0 },
1369
+ sentencesCount: { value: 0 },
1370
+ linesCount: { value: 0 },
1371
+ paragraphsCount: { value: 0 },
1372
+ pagesCount: { value: 0 },
1373
+ },
1374
+ output: {
1375
+ tokensCount: { value: 0 },
1376
+ charactersCount: { value: 0 },
1377
+ wordsCount: { value: 0 },
1378
+ sentencesCount: { value: 0 },
1379
+ linesCount: { value: 0 },
1380
+ paragraphsCount: { value: 0 },
1381
+ pagesCount: { value: 0 },
1382
+ },
1383
+ });
1384
+ /**
1385
+ * Function `addUsage` will add multiple usages into one
1386
+ *
1387
+ * Note: If you provide 0 values, it returns ZERO_USAGE
1388
+ */
1389
+ function addUsage() {
1390
+ var usageItems = [];
1391
+ for (var _i = 0; _i < arguments.length; _i++) {
1392
+ usageItems[_i] = arguments[_i];
1393
+ }
1394
+ return usageItems.reduce(function (acc, item) {
1395
+ var e_1, _a, e_2, _b;
1396
+ var _c;
1397
+ acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
1398
+ try {
1399
+ for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
1400
+ var key = _e.value;
1401
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1402
+ //@ts-ignore
1403
+ if (item.input[key]) {
1404
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1405
+ //@ts-ignore
1406
+ acc.input[key].value += item.input[key].value || 0;
1407
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1408
+ //@ts-ignore
1409
+ if (item.input[key].isUncertain) {
1410
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1411
+ //@ts-ignore
1412
+ acc.input[key].isUncertain = true;
1413
+ }
1414
+ }
1415
+ }
1416
+ }
1417
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1418
+ finally {
1419
+ try {
1420
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1421
+ }
1422
+ finally { if (e_1) throw e_1.error; }
1423
+ }
1424
+ try {
1425
+ for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
1426
+ var key = _g.value;
1427
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1428
+ //@ts-ignore
1429
+ if (item.output[key]) {
1430
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1431
+ //@ts-ignore
1432
+ acc.output[key].value += item.output[key].value || 0;
1433
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1434
+ //@ts-ignore
1435
+ if (item.output[key].isUncertain) {
1436
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1437
+ //@ts-ignore
1438
+ acc.output[key].isUncertain = true;
1439
+ }
1440
+ }
1441
+ }
1442
+ }
1443
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1444
+ finally {
1445
+ try {
1446
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1447
+ }
1448
+ finally { if (e_2) throw e_2.error; }
1449
+ }
1450
+ return acc;
1451
+ }, deepClone(ZERO_USAGE));
1452
+ }
1453
+
1454
+ /**
1455
+ * Async version of Array.forEach
1456
+ *
1457
+ * @param array - Array to iterate over
1458
+ * @param options - Options for the function
1459
+ * @param callbackfunction - Function to call for each item
1460
+ */
1461
+ function forEachAsync(array, options, callbackfunction) {
1462
+ return __awaiter(this, void 0, void 0, function () {
1463
+ var _a, maxParallelCount, index, runningTasks, tasks, _loop_1, _b, _c, item, e_1_1;
1464
+ var e_1, _d;
1465
+ return __generator(this, function (_e) {
1466
+ switch (_e.label) {
1467
+ case 0:
1468
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? Infinity : _a;
1469
+ index = 0;
1470
+ runningTasks = [];
1471
+ tasks = [];
1472
+ _loop_1 = function (item) {
1473
+ var currentIndex, task;
1474
+ return __generator(this, function (_f) {
1475
+ switch (_f.label) {
1476
+ case 0:
1477
+ currentIndex = index++;
1478
+ task = callbackfunction(item, currentIndex, array);
1479
+ tasks.push(task);
1480
+ runningTasks.push(task);
1481
+ /* not await */ Promise.resolve(task).then(function () {
1482
+ runningTasks = runningTasks.filter(function (t) { return t !== task; });
1483
+ });
1484
+ if (!(maxParallelCount < runningTasks.length)) return [3 /*break*/, 2];
1485
+ return [4 /*yield*/, Promise.race(runningTasks)];
1486
+ case 1:
1487
+ _f.sent();
1488
+ _f.label = 2;
1489
+ case 2: return [2 /*return*/];
1490
+ }
1491
+ });
1492
+ };
1493
+ _e.label = 1;
1494
+ case 1:
1495
+ _e.trys.push([1, 6, 7, 8]);
1496
+ _b = __values(array), _c = _b.next();
1497
+ _e.label = 2;
1498
+ case 2:
1499
+ if (!!_c.done) return [3 /*break*/, 5];
1500
+ item = _c.value;
1501
+ return [5 /*yield**/, _loop_1(item)];
1502
+ case 3:
1503
+ _e.sent();
1504
+ _e.label = 4;
1505
+ case 4:
1506
+ _c = _b.next();
1507
+ return [3 /*break*/, 2];
1508
+ case 5: return [3 /*break*/, 8];
1509
+ case 6:
1510
+ e_1_1 = _e.sent();
1511
+ e_1 = { error: e_1_1 };
1512
+ return [3 /*break*/, 8];
1513
+ case 7:
1514
+ try {
1515
+ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
1516
+ }
1517
+ finally { if (e_1) throw e_1.error; }
1518
+ return [7 /*endfinally*/];
1519
+ case 8: return [4 /*yield*/, Promise.all(tasks)];
1520
+ case 9:
1521
+ _e.sent();
1522
+ return [2 /*return*/];
1523
+ }
1524
+ });
1525
+ });
1526
+ }
1527
+
1528
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.61.0",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.61.0",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.61.0",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.61.0",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[{id:1,promptbookVersion:"0.61.0",modelUsage:{price:{value:0},input:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}},output:{tokensCount:{value:0},charactersCount:{value:0},wordsCount:{value:0},sentencesCount:{value:0},linesCount:{value:0},paragraphsCount:{value:0},pagesCount:{value:0}}}}],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1130
1529
 
1131
1530
  var defaultDiacriticsRemovalMap = [
1132
1531
  {
@@ -1453,6 +1852,14 @@
1453
1852
  * Function normalizes title to name which can be used as identifier
1454
1853
  */
1455
1854
  function titleToName(value) {
1855
+ if (value.startsWith('http://') || value.startsWith('https://')) {
1856
+ // TODO: Maybe check against some list unallowed characters
1857
+ return value;
1858
+ }
1859
+ if (value.startsWith('./') || value.startsWith('../')) {
1860
+ // TODO: Maybe check against some list unallowed characters
1861
+ return value;
1862
+ }
1456
1863
  value = removeEmojis(value);
1457
1864
  value = normalizeToKebabCase(value);
1458
1865
  // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
@@ -1491,7 +1898,11 @@
1491
1898
  throw errors[0];
1492
1899
  }
1493
1900
  else {
1494
- throw new PipelineExecutionError(spaceTrim.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 "); }));
1901
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors
1902
+ .map(function (error, index) {
1903
+ return spaceTrim.spaceTrim(function (block) { return "\n Error ".concat(index + 1, ":\n ").concat(block(error.stack || error.message), "\n "); });
1904
+ })
1905
+ .join('\n')), "\n "); }));
1495
1906
  }
1496
1907
  }
1497
1908
  /**
@@ -1499,26 +1910,134 @@
1499
1910
  */
1500
1911
 
1501
1912
  /**
1502
- * This error occurs when some expectation is not met in the execution of the pipeline
1913
+ * Parses the given script and returns the list of all used variables that are not defined in the script
1503
1914
  *
1504
- * @private Always catched and rethrown as `PipelineExecutionError`
1505
- * Note: This is a kindof subtype of PipelineExecutionError
1915
+ * @param script from which to extract the variables
1916
+ * @returns the list of variable names
1917
+ * @throws {ParsingError} if the script is invalid
1506
1918
  */
1507
- var ExpectError = /** @class */ (function (_super) {
1508
- __extends(ExpectError, _super);
1509
- function ExpectError(message) {
1510
- var _this = _super.call(this, message) || this;
1511
- _this.name = 'ExpectError';
1512
- Object.setPrototypeOf(_this, ExpectError.prototype);
1513
- return _this;
1514
- }
1515
- return ExpectError;
1516
- }(Error));
1919
+ function extractVariables(script) {
1920
+ var variables = new Set();
1921
+ script = "(()=>{".concat(script, "})()");
1922
+ try {
1923
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1924
+ try {
1925
+ eval(script);
1926
+ }
1927
+ catch (error) {
1928
+ if (!(error instanceof ReferenceError)) {
1929
+ throw error;
1930
+ }
1931
+ var undefinedName = error.message.split(' ')[0];
1932
+ /*
1933
+ Note: Parsing the error
1934
+ [ReferenceError: thing is not defined]
1935
+ */
1936
+ if (!undefinedName) {
1937
+ throw error;
1938
+ }
1939
+ if (script.includes(undefinedName + '(')) {
1940
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1941
+ }
1942
+ else {
1943
+ variables.add(undefinedName);
1944
+ script = "const ".concat(undefinedName, " = '';") + script;
1945
+ }
1946
+ }
1947
+ }
1948
+ catch (error) {
1949
+ if (!(error instanceof Error)) {
1950
+ throw error;
1951
+ }
1952
+ throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
1953
+ }
1954
+ return variables;
1955
+ }
1956
+ /**
1957
+ * TODO: [🔣] Support for multiple languages - python, java,...
1958
+ */
1959
+
1960
+ /**
1961
+ * Parses the prompt template and returns the set of all used parameters
1962
+ *
1963
+ * @param promptTemplate the template with used parameters
1964
+ * @returns the set of parameter names
1965
+ * @throws {ParsingError} if the script is invalid
1966
+ */
1967
+ function extractParameterNamesFromPromptTemplate(promptTemplate) {
1968
+ var e_1, _a, e_2, _b, e_3, _c;
1969
+ var title = promptTemplate.title, description = promptTemplate.description, blockType = promptTemplate.blockType, content = promptTemplate.content, preparedContent = promptTemplate.preparedContent, jokerParameterNames = promptTemplate.jokerParameterNames;
1970
+ var parameterNames = new Set();
1971
+ try {
1972
+ for (var _d = __values(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameterNames(title)), false), __read(extractParameterNames(description || '')), false), __read(extractParameterNames(content)), false), __read(extractParameterNames(preparedContent || '')), false)), _e = _d.next(); !_e.done; _e = _d.next()) {
1973
+ var parameterName = _e.value;
1974
+ parameterNames.add(parameterName);
1975
+ }
1976
+ }
1977
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1978
+ finally {
1979
+ try {
1980
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1981
+ }
1982
+ finally { if (e_1) throw e_1.error; }
1983
+ }
1984
+ if (blockType === 'SCRIPT') {
1985
+ try {
1986
+ for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
1987
+ var parameterName = _g.value;
1988
+ parameterNames.add(parameterName);
1989
+ }
1990
+ }
1991
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1992
+ finally {
1993
+ try {
1994
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1995
+ }
1996
+ finally { if (e_2) throw e_2.error; }
1997
+ }
1998
+ }
1999
+ try {
2000
+ for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
2001
+ var jokerName = _j.value;
2002
+ parameterNames.add(jokerName);
2003
+ }
2004
+ }
2005
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2006
+ finally {
2007
+ try {
2008
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
2009
+ }
2010
+ finally { if (e_3) throw e_3.error; }
2011
+ }
2012
+ parameterNames.delete('content');
2013
+ // <- Note {websiteContent} is used in `preparedContent`
2014
+ return parameterNames;
2015
+ }
2016
+ /**
2017
+ * TODO: [🔣] If script require contentLanguage
2018
+ */
2019
+
2020
+ /**
2021
+ * This error occurs when some expectation is not met in the execution of the pipeline
2022
+ *
2023
+ * @private Always catched and rethrown as `PipelineExecutionError`
2024
+ * Note: This is a kindof subtype of PipelineExecutionError
2025
+ */
2026
+ var ExpectError = /** @class */ (function (_super) {
2027
+ __extends(ExpectError, _super);
2028
+ function ExpectError(message) {
2029
+ var _this = _super.call(this, message) || this;
2030
+ _this.name = 'ExpectError';
2031
+ Object.setPrototypeOf(_this, ExpectError.prototype);
2032
+ return _this;
2033
+ }
2034
+ return ExpectError;
2035
+ }(Error));
1517
2036
 
1518
2037
  /**
1519
2038
  * Function isValidJsonString will tell you if the string is valid JSON or not
1520
2039
  */
1521
- function isValidJsonString(value /* <-[👨‍⚖️] */) {
2040
+ function isValidJsonString(value /* <- [👨‍⚖️] */) {
1522
2041
  try {
1523
2042
  JSON.parse(value);
1524
2043
  return true;
@@ -1661,10 +2180,17 @@
1661
2180
  throw errors[0];
1662
2181
  }
1663
2182
  else if (errors.length > 1) {
1664
- throw new PipelineExecutionError(spaceTrim__default["default"](function (block) { return "\n All execution tools failed:\n\n ".concat(block(errors.map(function (error) { return "- ".concat(error.name || 'Error', ": ").concat(error.message); }).join('\n')), "\n\n "); }));
2183
+ throw new PipelineExecutionError(
2184
+ // TODO: Tell which execution tools failed like
2185
+ // 1) OpenAI throw PipelineExecutionError: Parameter {knowledge} is not defined
2186
+ // 2) AnthropicClaude throw PipelineExecutionError: Parameter {knowledge} is not defined
2187
+ // 3) ...
2188
+ spaceTrim__default["default"](function (block) { return "\n All execution tools failed:\n\n ".concat(block(errors
2189
+ .map(function (error, i) { return "".concat(i + 1, ") **").concat(error.name || 'Error', ":** ").concat(error.message); })
2190
+ .join('\n')), "\n\n "); }));
1665
2191
  }
1666
2192
  else {
1667
- throw new PipelineExecutionError(spaceTrim__default["default"](function (block) { return "\n No execution tools available for model variant \"".concat(prompt.modelRequirements.modelVariant, "\".\n\n tl;dr\n\n You have provided no LLM Execution Tools that support model variant \"").concat(prompt.modelRequirements.modelVariant, ":\n ").concat(block(_this.llmExecutionTools
2193
+ throw new PipelineExecutionError(spaceTrim__default["default"](function (block) { return "\n You have not provided any `LlmExecutionTools` that support model variant \"".concat(prompt.modelRequirements.modelVariant, "\n\n Available `LlmExecutionTools`:\n ").concat(block(_this.llmExecutionTools
1668
2194
  .map(function (tools) { return "- ".concat(tools.title, " ").concat(tools.description || ''); })
1669
2195
  .join('\n')), "\n\n "); }));
1670
2196
  }
@@ -1674,7 +2200,7 @@
1674
2200
  };
1675
2201
  /**
1676
2202
  * List all available models that can be used
1677
- * This liost is a combination of all available models from all execution tools
2203
+ * This lists is a combination of all available models from all execution tools
1678
2204
  */
1679
2205
  MultipleLlmExecutionTools.prototype.listModels = function () {
1680
2206
  return __awaiter(this, void 0, void 0, function () {
@@ -1720,6 +2246,8 @@
1720
2246
  }());
1721
2247
  /**
1722
2248
  * TODO: [🧠][🎛] Aggregating multiple models - have result not only from one first aviable model BUT all of them
2249
+ * TODO: [🏖] If no llmTools have for example not defined `callCompletionModel` this will still return object with defined `callCompletionModel` which just throws `PipelineExecutionError`, make it undefined instead
2250
+ * Look how `countTotalUsage` (and `cacheLlmTools`) implements it
1723
2251
  */
1724
2252
 
1725
2253
  /**
@@ -1741,7 +2269,7 @@
1741
2269
  llmExecutionTools[_i] = arguments[_i];
1742
2270
  }
1743
2271
  if (llmExecutionTools.length === 0) {
1744
- var warningMessage = spaceTrim__default["default"]("\n You have provided no LLM Execution Tools.\n This means that you won't be able to execute any prompts that require large language models like GPT-4 or Anthropic's Claude.\n\n Technically, it's not an error, but it's probably not what you want because it does not make sense to use Promptbook without language models.\n ");
2272
+ var warningMessage = spaceTrim__default["default"]("\n You have not provided any `LlmExecutionTools`\n This means that you won't be able to execute any prompts that require large language models like GPT-4 or Anthropic's Claude.\n\n Technically, it's not an error, but it's probably not what you want because it does not make sense to use Promptbook without language models.\n ");
1745
2273
  // TODO: [🟥] Detect browser / node and make it colorfull
1746
2274
  console.warn(warningMessage);
1747
2275
  /*
@@ -1767,6 +2295,39 @@
1767
2295
  }
1768
2296
  return new (MultipleLlmExecutionTools.bind.apply(MultipleLlmExecutionTools, __spreadArray([void 0], __read(llmExecutionTools), false)))();
1769
2297
  }
2298
+ /**
2299
+ * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
2300
+ */
2301
+
2302
+ /**
2303
+ * Determine if the pipeline is fully prepared
2304
+ */
2305
+ function isPipelinePrepared(pipeline) {
2306
+ // Note: Ignoring `pipeline.preparations` @@@
2307
+ // Note: Ignoring `pipeline.knowledgePieces` @@@
2308
+ if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
2309
+ return false;
2310
+ }
2311
+ if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
2312
+ return false;
2313
+ }
2314
+ /*
2315
+ TODO: [🧠][🍫] `promptTemplates` can not be determined if they are fully prepared SO ignoring them
2316
+ > if (!pipeline.promptTemplates.every(({ preparedContent }) => preparedContent === undefined)) {
2317
+ > return false;
2318
+ > }
2319
+ */
2320
+ return true;
2321
+ }
2322
+ /**
2323
+ * TODO: [🐠] Maybe base this on `makeValidator`
2324
+ * TODO: [🔼] Export via core or utils
2325
+ * TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
2326
+ * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
2327
+ * - [🏍] ? Is context in each template
2328
+ * - [♨] Are samples prepared
2329
+ * - [♨] Are templates prepared
2330
+ */
1770
2331
 
1771
2332
  /**
1772
2333
  * Takes an item or an array of items and returns an array of items
@@ -1788,106 +2349,197 @@
1788
2349
  }
1789
2350
 
1790
2351
  /**
1791
- * The version of the Promptbook library
2352
+ * Just marks a place of place where should be something implemented
2353
+ * No side effects.
2354
+ *
2355
+ * Note: It can be usefull suppressing eslint errors of unused variables
2356
+ *
2357
+ * @param value any values
2358
+ * @returns void
2359
+ * @private within the repository
1792
2360
  */
1793
- var PROMPTBOOK_VERSION = '0.61.0-8';
1794
- // TODO: !!!! List here all the versions and annotate + put into script
2361
+ function TODO_USE() {
2362
+ var value = [];
2363
+ for (var _i = 0; _i < arguments.length; _i++) {
2364
+ value[_i] = arguments[_i];
2365
+ }
2366
+ }
1795
2367
 
1796
2368
  /**
1797
- * Function `addUsage` will add multiple usages into one
1798
- *
1799
- * Note: If you provide 0 values, it returns void usage
2369
+ * This error type indicates that some limit was reached
1800
2370
  */
1801
- function addUsage() {
1802
- var usageItems = [];
1803
- for (var _i = 0; _i < arguments.length; _i++) {
1804
- usageItems[_i] = arguments[_i];
2371
+ var LimitReachedError = /** @class */ (function (_super) {
2372
+ __extends(LimitReachedError, _super);
2373
+ function LimitReachedError(message) {
2374
+ var _this = _super.call(this, message) || this;
2375
+ _this.name = 'LimitReachedError';
2376
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
2377
+ return _this;
1805
2378
  }
1806
- var initialStructure = {
1807
- price: { value: 0 },
1808
- input: {
1809
- tokensCount: { value: 0 },
1810
- charactersCount: { value: 0 },
1811
- wordsCount: { value: 0 },
1812
- sentencesCount: { value: 0 },
1813
- linesCount: { value: 0 },
1814
- paragraphsCount: { value: 0 },
1815
- pagesCount: { value: 0 },
1816
- },
1817
- output: {
1818
- tokensCount: { value: 0 },
1819
- charactersCount: { value: 0 },
1820
- wordsCount: { value: 0 },
1821
- sentencesCount: { value: 0 },
1822
- linesCount: { value: 0 },
1823
- paragraphsCount: { value: 0 },
1824
- pagesCount: { value: 0 },
1825
- },
1826
- };
1827
- return usageItems.reduce(function (acc, item) {
1828
- var e_1, _a, e_2, _b;
1829
- var _c;
1830
- acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
1831
- try {
1832
- for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
1833
- var key = _e.value;
1834
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1835
- //@ts-ignore
1836
- if (item.input[key]) {
1837
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1838
- //@ts-ignore
1839
- acc.input[key].value += item.input[key].value || 0;
1840
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1841
- //@ts-ignore
1842
- if (item.input[key].isUncertain) {
1843
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1844
- //@ts-ignore
1845
- acc.input[key].isUncertain = true;
1846
- }
1847
- }
2379
+ return LimitReachedError;
2380
+ }(Error));
2381
+
2382
+ /**
2383
+ * Replaces parameters in template with values from parameters object
2384
+ *
2385
+ * @param template the template with parameters in {curly} braces
2386
+ * @param parameters the object with parameters
2387
+ * @returns the template with replaced parameters
2388
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2389
+ */
2390
+ function replaceParameters(template, parameters) {
2391
+ var e_1, _a;
2392
+ try {
2393
+ for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
2394
+ var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
2395
+ if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
2396
+ throw new UnexpectedError("Parameter {".concat(parameterName, "} has missing value"));
1848
2397
  }
1849
- }
1850
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1851
- finally {
1852
- try {
1853
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
2398
+ else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
2399
+ // TODO: [🍵]
2400
+ throw new UnexpectedError("Parameter {".concat(parameterName, "} is restricted to use"));
1854
2401
  }
1855
- finally { if (e_1) throw e_1.error; }
1856
2402
  }
2403
+ }
2404
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2405
+ finally {
1857
2406
  try {
1858
- for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
1859
- var key = _g.value;
1860
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1861
- //@ts-ignore
1862
- if (item.output[key]) {
1863
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1864
- //@ts-ignore
1865
- acc.output[key].value += item.output[key].value || 0;
1866
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1867
- //@ts-ignore
1868
- if (item.output[key].isUncertain) {
1869
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1870
- //@ts-ignore
1871
- acc.output[key].isUncertain = true;
1872
- }
1873
- }
1874
- }
2407
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1875
2408
  }
1876
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1877
- finally {
1878
- try {
1879
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1880
- }
1881
- finally { if (e_2) throw e_2.error; }
2409
+ finally { if (e_1) throw e_1.error; }
2410
+ }
2411
+ var replacedTemplate = template;
2412
+ var match;
2413
+ var loopLimit = LOOP_LIMIT;
2414
+ var _loop_1 = function () {
2415
+ if (loopLimit-- < 0) {
2416
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
1882
2417
  }
1883
- return acc;
1884
- }, initialStructure);
2418
+ var precol = match.groups.precol;
2419
+ var parameterName = match.groups.parameterName;
2420
+ if (parameterName === '') {
2421
+ return "continue";
2422
+ }
2423
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2424
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
2425
+ }
2426
+ if (parameters[parameterName] === undefined) {
2427
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2428
+ }
2429
+ var parameterValue = parameters[parameterName];
2430
+ if (parameterValue === undefined) {
2431
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2432
+ }
2433
+ parameterValue = parameterValue.toString();
2434
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2435
+ parameterValue = parameterValue
2436
+ .split('\n')
2437
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2438
+ .join('\n');
2439
+ }
2440
+ replacedTemplate =
2441
+ replacedTemplate.substring(0, match.index + precol.length) +
2442
+ parameterValue +
2443
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2444
+ };
2445
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2446
+ .exec(replacedTemplate))) {
2447
+ _loop_1();
2448
+ }
2449
+ // [💫] Check if there are parameters that are not closed properly
2450
+ if (/{\w+$/.test(replacedTemplate)) {
2451
+ throw new PipelineExecutionError('Parameter is not closed');
2452
+ }
2453
+ // [💫] Check if there are parameters that are not opened properly
2454
+ if (/^\w+}/.test(replacedTemplate)) {
2455
+ throw new PipelineExecutionError('Parameter is not opened');
2456
+ }
2457
+ return replacedTemplate;
1885
2458
  }
1886
2459
 
1887
2460
  /**
1888
- * Counts number of characters in the text
1889
- */
1890
- function countCharacters(text) {
2461
+ * Create difference set of two sets.
2462
+ *
2463
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
2464
+ */
2465
+ function difference(a, b, isEqual) {
2466
+ var e_1, _a;
2467
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
2468
+ var diff = new Set();
2469
+ var _loop_1 = function (itemA) {
2470
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
2471
+ diff.add(itemA);
2472
+ }
2473
+ };
2474
+ try {
2475
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
2476
+ var itemA = _c.value;
2477
+ _loop_1(itemA);
2478
+ }
2479
+ }
2480
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2481
+ finally {
2482
+ try {
2483
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2484
+ }
2485
+ finally { if (e_1) throw e_1.error; }
2486
+ }
2487
+ return diff;
2488
+ }
2489
+ /**
2490
+ * TODO: [🧠][💯] Maybe also implement symmetricDifference
2491
+ */
2492
+
2493
+ /**
2494
+ * Creates a new set with all elements that are present in either set
2495
+ *
2496
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
2497
+ */
2498
+ function union() {
2499
+ var e_1, _a, e_2, _b;
2500
+ var sets = [];
2501
+ for (var _i = 0; _i < arguments.length; _i++) {
2502
+ sets[_i] = arguments[_i];
2503
+ }
2504
+ var union = new Set();
2505
+ try {
2506
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
2507
+ var set = sets_1_1.value;
2508
+ try {
2509
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
2510
+ var item = _d.value;
2511
+ union.add(item);
2512
+ }
2513
+ }
2514
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2515
+ finally {
2516
+ try {
2517
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
2518
+ }
2519
+ finally { if (e_2) throw e_2.error; }
2520
+ }
2521
+ }
2522
+ }
2523
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2524
+ finally {
2525
+ try {
2526
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
2527
+ }
2528
+ finally { if (e_1) throw e_1.error; }
2529
+ }
2530
+ return union;
2531
+ }
2532
+
2533
+ /**
2534
+ * The version of the Promptbook library
2535
+ */
2536
+ var PROMPTBOOK_VERSION = '0.61.0';
2537
+ // TODO: !!!! List here all the versions and annotate + put into script
2538
+
2539
+ /**
2540
+ * Counts number of characters in the text
2541
+ */
2542
+ function countCharacters(text) {
1891
2543
  // Remove null characters
1892
2544
  text = text.replace(/\0/g, '');
1893
2545
  // Replace emojis (and also ZWJ sequence) with hyphens
@@ -2015,66 +2667,6 @@
2015
2667
  * TODO: [💝] Unite object for expecting amount and format
2016
2668
  */
2017
2669
 
2018
- /**
2019
- * Replaces parameters in template with values from parameters object
2020
- *
2021
- * @param template the template with parameters in {curly} braces
2022
- * @param parameters the object with parameters
2023
- * @returns the template with replaced parameters
2024
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2025
- *
2026
- * @private within the createPipelineExecutor
2027
- */
2028
- function replaceParameters(template, parameters) {
2029
- var replacedTemplate = template;
2030
- var match;
2031
- var loopLimit = LOOP_LIMIT;
2032
- var _loop_1 = function () {
2033
- if (loopLimit-- < 0) {
2034
- throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
2035
- }
2036
- var precol = match.groups.precol;
2037
- var parameterName = match.groups.parameterName;
2038
- if (parameterName === '') {
2039
- return "continue";
2040
- }
2041
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2042
- throw new PipelineExecutionError('Parameter is already opened or not closed');
2043
- }
2044
- if (parameters[parameterName] === undefined) {
2045
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2046
- }
2047
- var parameterValue = parameters[parameterName];
2048
- if (parameterValue === undefined) {
2049
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2050
- }
2051
- parameterValue = parameterValue.toString();
2052
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2053
- parameterValue = parameterValue
2054
- .split('\n')
2055
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2056
- .join('\n');
2057
- }
2058
- replacedTemplate =
2059
- replacedTemplate.substring(0, match.index + precol.length) +
2060
- parameterValue +
2061
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2062
- };
2063
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2064
- .exec(replacedTemplate))) {
2065
- _loop_1();
2066
- }
2067
- // [💫] Check if there are parameters that are not closed properly
2068
- if (/{\w+$/.test(replacedTemplate)) {
2069
- throw new PipelineExecutionError('Parameter is not closed');
2070
- }
2071
- // [💫] Check if there are parameters that are not opened properly
2072
- if (/^\w+}/.test(replacedTemplate)) {
2073
- throw new PipelineExecutionError('Parameter is not opened');
2074
- }
2075
- return replacedTemplate;
2076
- }
2077
-
2078
2670
  /**
2079
2671
  * Creates executor function from pipeline and execution tools.
2080
2672
  *
@@ -2084,24 +2676,107 @@
2084
2676
  function createPipelineExecutor(options) {
2085
2677
  var _this = this;
2086
2678
  var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2087
- var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
2088
- // TODO: !!!!! Implement new commands
2679
+ var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? MAX_EXECUTION_ATTEMPTS : _b, _c = settings.maxParallelCount, maxParallelCount = _c === void 0 ? MAX_PARALLEL_COUNT : _c, _d = settings.isVerbose, isVerbose = _d === void 0 ? false : _d, _e = settings.isNotPreparedWarningSupressed, isNotPreparedWarningSupressed = _e === void 0 ? false : _e;
2089
2680
  validatePipeline(pipeline);
2090
- // TODO: !!!!! Do here materialization of still unmaterialized or dynamic knowledge
2091
- // TODO: !!!!! Do here personaToModelRequirements
2092
2681
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
2682
+ var preparedPipeline;
2683
+ if (isPipelinePrepared(pipeline)) {
2684
+ preparedPipeline = pipeline;
2685
+ }
2686
+ else if (isNotPreparedWarningSupressed !== true) {
2687
+ console.warn(spaceTrim.spaceTrim("\n Pipeline ".concat(pipeline.pipelineUrl || pipeline.sourceFile || pipeline.title, " is not prepared\n\n ").concat(pipeline.sourceFile, "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n ")));
2688
+ }
2093
2689
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2690
+ // TODO: !!! Extract to separate functions and files - ALL FUNCTIONS BELOW
2691
+ function getContextForTemplate(// <- TODO: [🧠][🥜]
2692
+ template) {
2693
+ return __awaiter(this, void 0, void 0, function () {
2694
+ return __generator(this, function (_a) {
2695
+ TODO_USE(template);
2696
+ return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [🏍] Implement */];
2697
+ });
2698
+ });
2699
+ }
2700
+ function getKnowledgeForTemplate(// <- TODO: [🧠][🥜]
2701
+ template) {
2702
+ return __awaiter(this, void 0, void 0, function () {
2703
+ return __generator(this, function (_a) {
2704
+ // TODO: [♨] Implement Better - use real index and keyword search from `template` and {samples}
2705
+ TODO_USE(template);
2706
+ return [2 /*return*/, preparedPipeline.knowledgePieces.map(function (_a) {
2707
+ var content = _a.content;
2708
+ return "- ".concat(content);
2709
+ }).join('\n')];
2710
+ });
2711
+ });
2712
+ }
2713
+ function getSamplesForTemplate(// <- TODO: [🧠][🥜]
2714
+ template) {
2715
+ return __awaiter(this, void 0, void 0, function () {
2716
+ return __generator(this, function (_a) {
2717
+ // TODO: [♨] Implement Better - use real index and keyword search
2718
+ TODO_USE(template);
2719
+ return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */];
2720
+ });
2721
+ });
2722
+ }
2723
+ function getReservedParametersForTemplate(template) {
2724
+ return __awaiter(this, void 0, void 0, function () {
2725
+ var context, knowledge, samples, currentDate, modelName, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2726
+ var e_3, _a;
2727
+ return __generator(this, function (_b) {
2728
+ switch (_b.label) {
2729
+ case 0: return [4 /*yield*/, getContextForTemplate(template)];
2730
+ case 1:
2731
+ context = _b.sent();
2732
+ return [4 /*yield*/, getKnowledgeForTemplate(template)];
2733
+ case 2:
2734
+ knowledge = _b.sent();
2735
+ return [4 /*yield*/, getSamplesForTemplate(template)];
2736
+ case 3:
2737
+ samples = _b.sent();
2738
+ currentDate = new Date().toISOString();
2739
+ modelName = RESERVED_PARAMETER_MISSING_VALUE;
2740
+ reservedParameters = {
2741
+ content: RESERVED_PARAMETER_RESTRICTED,
2742
+ context: context,
2743
+ knowledge: knowledge,
2744
+ samples: samples,
2745
+ currentDate: currentDate,
2746
+ modelName: modelName,
2747
+ };
2748
+ try {
2749
+ // Note: Doublecheck that ALL reserved parameters are defined:
2750
+ for (RESERVED_PARAMETER_NAMES_1 = __values(RESERVED_PARAMETER_NAMES), RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next(); !RESERVED_PARAMETER_NAMES_1_1.done; RESERVED_PARAMETER_NAMES_1_1 = RESERVED_PARAMETER_NAMES_1.next()) {
2751
+ parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
2752
+ if (reservedParameters[parameterName] === undefined) {
2753
+ throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
2754
+ }
2755
+ }
2756
+ }
2757
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2758
+ finally {
2759
+ try {
2760
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_a = RESERVED_PARAMETER_NAMES_1.return)) _a.call(RESERVED_PARAMETER_NAMES_1);
2761
+ }
2762
+ finally { if (e_3) throw e_3.error; }
2763
+ }
2764
+ return [2 /*return*/, reservedParameters];
2765
+ }
2766
+ });
2767
+ });
2768
+ }
2094
2769
  function executeSingleTemplate(currentTemplate) {
2095
2770
  return __awaiter(this, void 0, void 0, function () {
2096
- var name, title, priority, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
2097
- var e_2, _j, e_4, _k, e_3, _l, _m;
2771
+ var name, title, priority, usedParameterNames, dependentParameterNames, definedParameters, _a, _b, _c, definedParameterNames, parameters, _d, _e, parameterName, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokerParameterNames, preparedContent, attempt, isJokerAttempt, jokerParameterName, _f, _g, _h, _j, scriptTools, error_2, e_4_1, _k, _l, functionName, postprocessingError, _m, _o, scriptTools, error_3, e_5_1, e_6_1, error_4;
2772
+ var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2098
2773
  var _this = this;
2099
- return __generator(this, function (_o) {
2100
- switch (_o.label) {
2774
+ return __generator(this, function (_u) {
2775
+ switch (_u.label) {
2101
2776
  case 0:
2102
2777
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
2103
2778
  title = currentTemplate.title;
2104
- priority = pipeline.promptTemplates.length - pipeline.promptTemplates.indexOf(currentTemplate);
2779
+ priority = preparedPipeline.promptTemplates.length - preparedPipeline.promptTemplates.indexOf(currentTemplate);
2105
2780
  if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
2106
2781
  return [4 /*yield*/, onProgress({
2107
2782
  name: name,
@@ -2114,61 +2789,111 @@
2114
2789
  // <- [3]
2115
2790
  })];
2116
2791
  case 1:
2117
- _o.sent();
2118
- _o.label = 2;
2792
+ _u.sent();
2793
+ _u.label = 2;
2119
2794
  case 2:
2795
+ usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
2796
+ dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2797
+ if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2798
+ throw new UnexpectedError(spaceTrim.spaceTrim("\n Dependent parameters are not consistent used parameters:\n\n Dependent parameters:\n ".concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n ")));
2799
+ }
2800
+ _b = (_a = Object).freeze;
2801
+ _c = [{}];
2802
+ return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
2803
+ case 3:
2804
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
2805
+ definedParameterNames = new Set(Object.keys(definedParameters));
2806
+ parameters = {};
2807
+ try {
2808
+ // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
2809
+ for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
2810
+ parameterName = _e.value;
2811
+ // Situation: Parameter is defined and used
2812
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2813
+ parameters[parameterName] = definedParameters[parameterName];
2814
+ }
2815
+ // Situation: Parameter is defined but NOT used
2816
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
2817
+ // Do not pass this parameter to prompt
2818
+ }
2819
+ // Situation: Parameter is NOT defined BUT used
2820
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2821
+ // Houston, we have a problem
2822
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2823
+ throw new UnexpectedError(spaceTrim.spaceTrim("\n Parameter {".concat(parameterName, "} is NOT defined\n BUT used in template \"").concat(currentTemplate.title || currentTemplate.name, "\"\n\n This should be catched in `validatePipeline`\n\n ")));
2824
+ }
2825
+ }
2826
+ }
2827
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2828
+ finally {
2829
+ try {
2830
+ if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
2831
+ }
2832
+ finally { if (e_7) throw e_7.error; }
2833
+ }
2834
+ // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
2835
+ Object.freeze(parameters);
2120
2836
  result = null;
2121
2837
  resultString = null;
2122
2838
  expectError = null;
2123
2839
  maxAttempts = currentTemplate.blockType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
2124
- jokers = currentTemplate.jokers || [];
2125
- attempt = -jokers.length;
2126
- _o.label = 3;
2127
- case 3:
2128
- if (!(attempt < maxAttempts)) return [3 /*break*/, 51];
2840
+ jokerParameterNames = currentTemplate.jokerParameterNames || [];
2841
+ preparedContent = (currentTemplate.preparedContent || '{content}')
2842
+ .split('{content}')
2843
+ .join(currentTemplate.content);
2844
+ attempt = -jokerParameterNames.length;
2845
+ _u.label = 4;
2846
+ case 4:
2847
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
2129
2848
  isJokerAttempt = attempt < 0;
2130
- joker = jokers[jokers.length + attempt];
2131
- if (isJokerAttempt && !joker) {
2849
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2850
+ if (isJokerAttempt && !jokerParameterName) {
2132
2851
  throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
2133
2852
  }
2134
2853
  result = null;
2135
2854
  resultString = null;
2136
2855
  expectError = null;
2137
2856
  if (isJokerAttempt) {
2138
- if (typeof parametersToPass[joker] === 'undefined') {
2139
- throw new PipelineExecutionError("Joker parameter {".concat(joker, "} not defined"));
2857
+ if (parameters[jokerParameterName] === undefined) {
2858
+ throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
2859
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
2860
+ }
2861
+ else {
2862
+ resultString = parameters[jokerParameterName];
2140
2863
  }
2141
- resultString = parametersToPass[joker];
2142
- }
2143
- _o.label = 4;
2144
- case 4:
2145
- _o.trys.push([4, 47, 48, 49]);
2146
- if (!!isJokerAttempt) return [3 /*break*/, 29];
2147
- _a = currentTemplate.blockType;
2148
- switch (_a) {
2149
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
2150
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
2151
- case 'SCRIPT': return [3 /*break*/, 15];
2152
- case 'PROMPT_DIALOG': return [3 /*break*/, 26];
2153
2864
  }
2154
- return [3 /*break*/, 28];
2865
+ _u.label = 5;
2155
2866
  case 5:
2156
- resultString = replaceParameters(currentTemplate.content, parametersToPass);
2867
+ _u.trys.push([5, 48, 49, 50]);
2868
+ if (!!isJokerAttempt) return [3 /*break*/, 30];
2869
+ _f = currentTemplate.blockType;
2870
+ switch (_f) {
2871
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
2872
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
2873
+ case 'SCRIPT': return [3 /*break*/, 16];
2874
+ case 'PROMPT_DIALOG': return [3 /*break*/, 27];
2875
+ }
2157
2876
  return [3 /*break*/, 29];
2158
2877
  case 6:
2878
+ resultString = replaceParameters(preparedContent, parameters);
2879
+ return [3 /*break*/, 30];
2880
+ case 7:
2159
2881
  prompt = {
2160
2882
  title: currentTemplate.title,
2161
- pipelineUrl: "".concat(pipeline.pipelineUrl
2162
- ? pipeline.pipelineUrl
2883
+ pipelineUrl: "".concat(preparedPipeline.pipelineUrl
2884
+ ? preparedPipeline.pipelineUrl
2163
2885
  : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2164
- parameters: parametersToPass,
2165
- content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2886
+ parameters: parameters,
2887
+ content: preparedContent,
2166
2888
  modelRequirements: currentTemplate.modelRequirements,
2167
- expectations: currentTemplate.expectations,
2889
+ expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
2890
+ var name = _a.name;
2891
+ return name === currentTemplate.personaName;
2892
+ }) || {})), currentTemplate.expectations),
2168
2893
  expectFormat: currentTemplate.expectFormat,
2169
- postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2170
- var errors, _a, _b, scriptTools, error_5, e_5_1;
2171
- var e_5, _c;
2894
+ postprocessing: (currentTemplate.postprocessingFunctionNames || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2895
+ var errors, _a, _b, scriptTools, error_5, e_8_1;
2896
+ var e_8, _c;
2172
2897
  return __generator(this, function (_d) {
2173
2898
  switch (_d.label) {
2174
2899
  case 0:
@@ -2189,7 +2914,7 @@
2189
2914
  script: "".concat(functionName, "(result)"),
2190
2915
  parameters: {
2191
2916
  result: result || '',
2192
- // Note: No ...parametersToPass, because working with result only
2917
+ // Note: No ...parametersForTemplate, because working with result only
2193
2918
  },
2194
2919
  })];
2195
2920
  case 4: return [2 /*return*/, _d.sent()];
@@ -2198,6 +2923,9 @@
2198
2923
  if (!(error_5 instanceof Error)) {
2199
2924
  throw error_5;
2200
2925
  }
2926
+ if (error_5 instanceof UnexpectedError) {
2927
+ throw error_5;
2928
+ }
2201
2929
  errors.push(error_5);
2202
2930
  return [3 /*break*/, 6];
2203
2931
  case 6:
@@ -2205,14 +2933,14 @@
2205
2933
  return [3 /*break*/, 2];
2206
2934
  case 7: return [3 /*break*/, 10];
2207
2935
  case 8:
2208
- e_5_1 = _d.sent();
2209
- e_5 = { error: e_5_1 };
2936
+ e_8_1 = _d.sent();
2937
+ e_8 = { error: e_8_1 };
2210
2938
  return [3 /*break*/, 10];
2211
2939
  case 9:
2212
2940
  try {
2213
2941
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2214
2942
  }
2215
- finally { if (e_5) throw e_5.error; }
2943
+ finally { if (e_8) throw e_8.error; }
2216
2944
  return [7 /*endfinally*/];
2217
2945
  case 10:
2218
2946
  if (errors.length === 0) {
@@ -2228,35 +2956,35 @@
2228
2956
  });
2229
2957
  }); }; }),
2230
2958
  };
2231
- _b = currentTemplate.modelRequirements.modelVariant;
2232
- switch (_b) {
2233
- case 'CHAT': return [3 /*break*/, 7];
2234
- case 'COMPLETION': return [3 /*break*/, 9];
2235
- case 'EMBEDDING': return [3 /*break*/, 11];
2959
+ _g = currentTemplate.modelRequirements.modelVariant;
2960
+ switch (_g) {
2961
+ case 'CHAT': return [3 /*break*/, 8];
2962
+ case 'COMPLETION': return [3 /*break*/, 10];
2963
+ case 'EMBEDDING': return [3 /*break*/, 12];
2236
2964
  }
2237
- return [3 /*break*/, 13];
2238
- case 7: return [4 /*yield*/, llmTools.callChatModel(prompt)];
2239
- case 8:
2240
- chatResult = _o.sent();
2965
+ return [3 /*break*/, 14];
2966
+ case 8: return [4 /*yield*/, llmTools.callChatModel(deepFreeze(prompt))];
2967
+ case 9:
2968
+ chatResult = _u.sent();
2241
2969
  // TODO: [🍬] Destroy chatThread
2242
2970
  result = chatResult;
2243
2971
  resultString = chatResult.content;
2244
- return [3 /*break*/, 14];
2245
- case 9: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
2246
- case 10:
2247
- completionResult = _o.sent();
2972
+ return [3 /*break*/, 15];
2973
+ case 10: return [4 /*yield*/, llmTools.callCompletionModel(deepFreeze(prompt))];
2974
+ case 11:
2975
+ completionResult = _u.sent();
2248
2976
  result = completionResult;
2249
2977
  resultString = completionResult.content;
2250
- return [3 /*break*/, 14];
2251
- case 11: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
2252
- case 12:
2253
- embeddingResult = _o.sent();
2978
+ return [3 /*break*/, 15];
2979
+ case 12: return [4 /*yield*/, llmTools.callEmbeddingModel(deepFreeze(prompt))];
2980
+ case 13:
2981
+ embeddingResult = _u.sent();
2254
2982
  result = embeddingResult;
2255
2983
  resultString = embeddingResult.content.join(',');
2256
- return [3 /*break*/, 14];
2257
- case 13: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2258
- case 14: return [3 /*break*/, 29];
2259
- case 15:
2984
+ return [3 /*break*/, 15];
2985
+ case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2986
+ case 15: return [3 /*break*/, 30];
2987
+ case 16:
2260
2988
  if (arrayableToArray(tools.script).length === 0) {
2261
2989
  throw new PipelineExecutionError('No script execution tools are available');
2262
2990
  }
@@ -2265,49 +2993,52 @@
2265
2993
  }
2266
2994
  // TODO: DRY [1]
2267
2995
  scriptPipelineExecutionErrors = [];
2268
- _o.label = 16;
2269
- case 16:
2270
- _o.trys.push([16, 23, 24, 25]);
2271
- _c = (e_2 = void 0, __values(arrayableToArray(tools.script))), _d = _c.next();
2272
- _o.label = 17;
2996
+ _u.label = 17;
2273
2997
  case 17:
2274
- if (!!_d.done) return [3 /*break*/, 22];
2275
- scriptTools = _d.value;
2276
- _o.label = 18;
2998
+ _u.trys.push([17, 24, 25, 26]);
2999
+ _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
3000
+ _u.label = 18;
2277
3001
  case 18:
2278
- _o.trys.push([18, 20, , 21]);
2279
- return [4 /*yield*/, scriptTools.execute({
2280
- scriptLanguage: currentTemplate.contentLanguage,
2281
- script: currentTemplate.content,
2282
- parameters: parametersToPass,
2283
- })];
3002
+ if (!!_j.done) return [3 /*break*/, 23];
3003
+ scriptTools = _j.value;
3004
+ _u.label = 19;
2284
3005
  case 19:
2285
- resultString = _o.sent();
2286
- return [3 /*break*/, 22];
3006
+ _u.trys.push([19, 21, , 22]);
3007
+ return [4 /*yield*/, scriptTools.execute(deepFreeze({
3008
+ scriptLanguage: currentTemplate.contentLanguage,
3009
+ script: preparedContent,
3010
+ parameters: parameters,
3011
+ }))];
2287
3012
  case 20:
2288
- error_2 = _o.sent();
3013
+ resultString = _u.sent();
3014
+ return [3 /*break*/, 23];
3015
+ case 21:
3016
+ error_2 = _u.sent();
2289
3017
  if (!(error_2 instanceof Error)) {
2290
3018
  throw error_2;
2291
3019
  }
3020
+ if (error_2 instanceof UnexpectedError) {
3021
+ throw error_2;
3022
+ }
2292
3023
  scriptPipelineExecutionErrors.push(error_2);
2293
- return [3 /*break*/, 21];
2294
- case 21:
2295
- _d = _c.next();
2296
- return [3 /*break*/, 17];
2297
- case 22: return [3 /*break*/, 25];
2298
- case 23:
2299
- e_2_1 = _o.sent();
2300
- e_2 = { error: e_2_1 };
2301
- return [3 /*break*/, 25];
3024
+ return [3 /*break*/, 22];
3025
+ case 22:
3026
+ _j = _h.next();
3027
+ return [3 /*break*/, 18];
3028
+ case 23: return [3 /*break*/, 26];
2302
3029
  case 24:
3030
+ e_4_1 = _u.sent();
3031
+ e_4 = { error: e_4_1 };
3032
+ return [3 /*break*/, 26];
3033
+ case 25:
2303
3034
  try {
2304
- if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
3035
+ if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
2305
3036
  }
2306
- finally { if (e_2) throw e_2.error; }
3037
+ finally { if (e_4) throw e_4.error; }
2307
3038
  return [7 /*endfinally*/];
2308
- case 25:
3039
+ case 26:
2309
3040
  if (resultString !== null) {
2310
- return [3 /*break*/, 29];
3041
+ return [3 /*break*/, 30];
2311
3042
  }
2312
3043
  if (scriptPipelineExecutionErrors.length === 1) {
2313
3044
  throw scriptPipelineExecutionErrors[0];
@@ -2317,101 +3048,104 @@
2317
3048
  .map(function (error) { return '- ' + error.message; })
2318
3049
  .join('\n\n')), "\n "); }));
2319
3050
  }
2320
- case 26:
3051
+ case 27:
2321
3052
  if (tools.userInterface === undefined) {
2322
3053
  throw new PipelineExecutionError('User interface tools are not available');
2323
3054
  }
2324
- return [4 /*yield*/, tools.userInterface.promptDialog({
3055
+ return [4 /*yield*/, tools.userInterface.promptDialog(deepFreeze({
2325
3056
  promptTitle: currentTemplate.title,
2326
- promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
2327
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
3057
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3058
+ defaultValue: replaceParameters(preparedContent, parameters),
2328
3059
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
2329
3060
  placeholder: undefined,
2330
3061
  priority: priority,
2331
- })];
2332
- case 27:
3062
+ }))];
3063
+ case 28:
2333
3064
  // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
2334
- resultString = _o.sent();
2335
- return [3 /*break*/, 29];
2336
- case 28: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2337
- case 29:
2338
- if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 46];
2339
- _o.label = 30;
3065
+ resultString = _u.sent();
3066
+ return [3 /*break*/, 30];
3067
+ case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2340
3068
  case 30:
2341
- _o.trys.push([30, 44, 45, 46]);
2342
- _e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
2343
- _o.label = 31;
3069
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
3070
+ _u.label = 31;
2344
3071
  case 31:
2345
- if (!!_f.done) return [3 /*break*/, 43];
2346
- functionName = _f.value;
3072
+ _u.trys.push([31, 45, 46, 47]);
3073
+ _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
3074
+ _u.label = 32;
3075
+ case 32:
3076
+ if (!!_l.done) return [3 /*break*/, 44];
3077
+ functionName = _l.value;
2347
3078
  // TODO: DRY [1]
2348
3079
  scriptPipelineExecutionErrors = [];
2349
3080
  postprocessingError = null;
2350
- _o.label = 32;
2351
- case 32:
2352
- _o.trys.push([32, 39, 40, 41]);
2353
- _g = (e_3 = void 0, __values(arrayableToArray(tools.script))), _h = _g.next();
2354
- _o.label = 33;
3081
+ _u.label = 33;
2355
3082
  case 33:
2356
- if (!!_h.done) return [3 /*break*/, 38];
2357
- scriptTools = _h.value;
2358
- _o.label = 34;
3083
+ _u.trys.push([33, 40, 41, 42]);
3084
+ _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
3085
+ _u.label = 34;
2359
3086
  case 34:
2360
- _o.trys.push([34, 36, , 37]);
3087
+ if (!!_o.done) return [3 /*break*/, 39];
3088
+ scriptTools = _o.value;
3089
+ _u.label = 35;
3090
+ case 35:
3091
+ _u.trys.push([35, 37, , 38]);
2361
3092
  return [4 /*yield*/, scriptTools.execute({
2362
3093
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2363
3094
  script: "".concat(functionName, "(resultString)"),
2364
3095
  parameters: {
2365
3096
  resultString: resultString || '',
2366
- // Note: No ...parametersToPass, because working with result only
3097
+ // Note: No ...parametersForTemplate, because working with result only
2367
3098
  },
2368
3099
  })];
2369
- case 35:
2370
- resultString = _o.sent();
2371
- postprocessingError = null;
2372
- return [3 /*break*/, 38];
2373
3100
  case 36:
2374
- error_3 = _o.sent();
3101
+ resultString = _u.sent();
3102
+ postprocessingError = null;
3103
+ return [3 /*break*/, 39];
3104
+ case 37:
3105
+ error_3 = _u.sent();
2375
3106
  if (!(error_3 instanceof Error)) {
2376
3107
  throw error_3;
2377
3108
  }
3109
+ if (error_3 instanceof UnexpectedError) {
3110
+ throw error_3;
3111
+ }
2378
3112
  postprocessingError = error_3;
2379
3113
  scriptPipelineExecutionErrors.push(error_3);
2380
- return [3 /*break*/, 37];
2381
- case 37:
2382
- _h = _g.next();
2383
- return [3 /*break*/, 33];
2384
- case 38: return [3 /*break*/, 41];
2385
- case 39:
2386
- e_3_1 = _o.sent();
2387
- e_3 = { error: e_3_1 };
2388
- return [3 /*break*/, 41];
3114
+ return [3 /*break*/, 38];
3115
+ case 38:
3116
+ _o = _m.next();
3117
+ return [3 /*break*/, 34];
3118
+ case 39: return [3 /*break*/, 42];
2389
3119
  case 40:
3120
+ e_5_1 = _u.sent();
3121
+ e_5 = { error: e_5_1 };
3122
+ return [3 /*break*/, 42];
3123
+ case 41:
2390
3124
  try {
2391
- if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
3125
+ if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
2392
3126
  }
2393
- finally { if (e_3) throw e_3.error; }
3127
+ finally { if (e_5) throw e_5.error; }
2394
3128
  return [7 /*endfinally*/];
2395
- case 41:
3129
+ case 42:
2396
3130
  if (postprocessingError) {
2397
3131
  throw postprocessingError;
2398
3132
  }
2399
- _o.label = 42;
2400
- case 42:
2401
- _f = _e.next();
2402
- return [3 /*break*/, 31];
2403
- case 43: return [3 /*break*/, 46];
2404
- case 44:
2405
- e_4_1 = _o.sent();
2406
- e_4 = { error: e_4_1 };
2407
- return [3 /*break*/, 46];
3133
+ _u.label = 43;
3134
+ case 43:
3135
+ _l = _k.next();
3136
+ return [3 /*break*/, 32];
3137
+ case 44: return [3 /*break*/, 47];
2408
3138
  case 45:
3139
+ e_6_1 = _u.sent();
3140
+ e_6 = { error: e_6_1 };
3141
+ return [3 /*break*/, 47];
3142
+ case 46:
2409
3143
  try {
2410
- if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
3144
+ if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
2411
3145
  }
2412
- finally { if (e_4) throw e_4.error; }
3146
+ finally { if (e_6) throw e_6.error; }
2413
3147
  return [7 /*endfinally*/];
2414
- case 46:
3148
+ case 47:
2415
3149
  // TODO: [💝] Unite object for expecting amount and format
2416
3150
  if (currentTemplate.expectFormat) {
2417
3151
  if (currentTemplate.expectFormat === 'JSON') {
@@ -2424,15 +3158,18 @@
2424
3158
  if (currentTemplate.expectations) {
2425
3159
  checkExpectations(currentTemplate.expectations, resultString || '');
2426
3160
  }
2427
- return [3 /*break*/, 51];
2428
- case 47:
2429
- error_4 = _o.sent();
3161
+ return [3 /*break*/, 52];
3162
+ case 48:
3163
+ error_4 = _u.sent();
2430
3164
  if (!(error_4 instanceof ExpectError)) {
2431
3165
  throw error_4;
2432
3166
  }
3167
+ if (error_4 instanceof UnexpectedError) {
3168
+ throw error_4;
3169
+ }
2433
3170
  expectError = error_4;
2434
- return [3 /*break*/, 49];
2435
- case 48:
3171
+ return [3 /*break*/, 50];
3172
+ case 49:
2436
3173
  if (!isJokerAttempt &&
2437
3174
  currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
2438
3175
  prompt
@@ -2441,28 +3178,21 @@
2441
3178
  ) {
2442
3179
  // TODO: [🧠] Maybe put other blockTypes into report
2443
3180
  executionReport.promptExecutions.push({
2444
- prompt: {
2445
- title: currentTemplate.title /* <- Note: If title in pipeline contains emojis, pass it innto report */,
2446
- content: prompt.content,
2447
- modelRequirements: prompt.modelRequirements,
2448
- expectations: prompt.expectations,
2449
- expectFormat: prompt.expectFormat,
2450
- // <- Note: Do want to pass ONLY wanted information to the report
2451
- },
3181
+ prompt: __assign({}, prompt),
2452
3182
  result: result || undefined,
2453
3183
  error: expectError || undefined,
2454
3184
  });
2455
3185
  }
2456
3186
  return [7 /*endfinally*/];
2457
- case 49:
3187
+ case 50:
2458
3188
  if (expectError !== null && attempt === maxAttempts - 1) {
2459
3189
  throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
2460
3190
  }
2461
- _o.label = 50;
2462
- case 50:
2463
- attempt++;
2464
- return [3 /*break*/, 3];
3191
+ _u.label = 51;
2465
3192
  case 51:
3193
+ attempt++;
3194
+ return [3 /*break*/, 4];
3195
+ case 52:
2466
3196
  if (resultString === null) {
2467
3197
  throw new UnexpectedError('Something went wrong and prompt result is null');
2468
3198
  }
@@ -2478,30 +3208,136 @@
2478
3208
  // <- [3]
2479
3209
  });
2480
3210
  }
2481
- parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _m));
3211
+ parametersToPass = Object.freeze(__assign(__assign({}, parametersToPass), (_t = {}, _t[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because pipeline checks logic consistency during construction */, _t)));
2482
3212
  return [2 /*return*/];
2483
3213
  }
2484
3214
  });
2485
3215
  });
2486
3216
  }
2487
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
2488
- var e_1, _c;
2489
- return __generator(this, function (_d) {
2490
- switch (_d.label) {
3217
+ function filterJustOutputParameters() {
3218
+ var e_9, _a;
3219
+ var outputParameters = {};
3220
+ try {
3221
+ // Note: Filter ONLY output parameters
3222
+ for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
3223
+ var isOutput = _a.isOutput;
3224
+ return isOutput;
3225
+ })), _c = _b.next(); !_c.done; _c = _b.next()) {
3226
+ var parameter = _c.value;
3227
+ if (parametersToPass[parameter.name] === undefined) {
3228
+ // [4]
3229
+ warnings.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution")));
3230
+ continue;
3231
+ }
3232
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3233
+ }
3234
+ }
3235
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
3236
+ finally {
3237
+ try {
3238
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3239
+ }
3240
+ finally { if (e_9) throw e_9.error; }
3241
+ }
3242
+ return outputParameters;
3243
+ }
3244
+ var errors, warnings, executionReport, _a, _b, parameter, _loop_1, _c, _d, parameterName, state_1, parametersToPass, resovedParameterNames_1, unresovedTemplates_1, resolving_1, loopLimit, _loop_2, error_1, usage_1, outputParameters_1, usage, outputParameters;
3245
+ var e_1, _e, e_2, _f;
3246
+ return __generator(this, function (_g) {
3247
+ switch (_g.label) {
2491
3248
  case 0:
2492
- parametersToPass = inputParameters;
3249
+ if (!(preparedPipeline === undefined)) return [3 /*break*/, 2];
3250
+ return [4 /*yield*/, preparePipeline(pipeline, {
3251
+ llmTools: llmTools,
3252
+ isVerbose: isVerbose,
3253
+ maxParallelCount: maxParallelCount,
3254
+ })];
3255
+ case 1:
3256
+ preparedPipeline = _g.sent();
3257
+ _g.label = 2;
3258
+ case 2:
3259
+ errors = [];
3260
+ warnings = [];
2493
3261
  executionReport = {
2494
- pipelineUrl: pipeline.pipelineUrl,
2495
- title: pipeline.title,
3262
+ pipelineUrl: preparedPipeline.pipelineUrl,
3263
+ title: preparedPipeline.title,
2496
3264
  promptbookUsedVersion: PROMPTBOOK_VERSION,
2497
- promptbookRequestedVersion: pipeline.promptbookVersion,
2498
- description: pipeline.description,
3265
+ promptbookRequestedVersion: preparedPipeline.promptbookVersion,
3266
+ description: preparedPipeline.description,
2499
3267
  promptExecutions: [],
2500
3268
  };
2501
- _d.label = 1;
2502
- case 1:
2503
- _d.trys.push([1, 6, , 7]);
2504
- resovedParameters_1 = pipeline.parameters
3269
+ try {
3270
+ // Note: Check that all input input parameters are defined
3271
+ for (_a = __values(preparedPipeline.parameters.filter(function (_a) {
3272
+ var isInput = _a.isInput;
3273
+ return isInput;
3274
+ })), _b = _a.next(); !_b.done; _b = _a.next()) {
3275
+ parameter = _b.value;
3276
+ if (inputParameters[parameter.name] === undefined) {
3277
+ return [2 /*return*/, deepFreezeWithSameType({
3278
+ isSuccessful: false,
3279
+ errors: __spreadArray([
3280
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an input parameter"))
3281
+ ], __read(errors), false),
3282
+ warnings: [],
3283
+ executionReport: executionReport,
3284
+ outputParameters: {},
3285
+ usage: ZERO_USAGE,
3286
+ preparedPipeline: preparedPipeline,
3287
+ })];
3288
+ }
3289
+ }
3290
+ }
3291
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3292
+ finally {
3293
+ try {
3294
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
3295
+ }
3296
+ finally { if (e_1) throw e_1.error; }
3297
+ }
3298
+ _loop_1 = function (parameterName) {
3299
+ var parameter = preparedPipeline.parameters.find(function (_a) {
3300
+ var name = _a.name;
3301
+ return name === parameterName;
3302
+ });
3303
+ if (parameter === undefined) {
3304
+ warnings.push(new PipelineExecutionError("Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.")));
3305
+ }
3306
+ else if (parameter.isInput === false) {
3307
+ return { value: deepFreezeWithSameType({
3308
+ isSuccessful: false,
3309
+ errors: __spreadArray([
3310
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input"))
3311
+ ], __read(errors), false),
3312
+ warnings: warnings,
3313
+ executionReport: executionReport,
3314
+ outputParameters: {},
3315
+ usage: ZERO_USAGE,
3316
+ preparedPipeline: preparedPipeline,
3317
+ }) };
3318
+ }
3319
+ };
3320
+ try {
3321
+ // Note: Check that no extra input parameters are passed
3322
+ for (_c = __values(Object.keys(inputParameters)), _d = _c.next(); !_d.done; _d = _c.next()) {
3323
+ parameterName = _d.value;
3324
+ state_1 = _loop_1(parameterName);
3325
+ if (typeof state_1 === "object")
3326
+ return [2 /*return*/, state_1.value];
3327
+ }
3328
+ }
3329
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
3330
+ finally {
3331
+ try {
3332
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
3333
+ }
3334
+ finally { if (e_2) throw e_2.error; }
3335
+ }
3336
+ parametersToPass = inputParameters;
3337
+ _g.label = 3;
3338
+ case 3:
3339
+ _g.trys.push([3, 8, , 9]);
3340
+ resovedParameterNames_1 = preparedPipeline.parameters
2505
3341
  .filter(function (_a) {
2506
3342
  var isInput = _a.isInput;
2507
3343
  return isInput;
@@ -2510,187 +3346,192 @@
2510
3346
  var name = _a.name;
2511
3347
  return name;
2512
3348
  });
2513
- unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
3349
+ unresovedTemplates_1 = __spreadArray([], __read(preparedPipeline.promptTemplates), false);
2514
3350
  resolving_1 = [];
2515
3351
  loopLimit = LOOP_LIMIT;
2516
- _loop_1 = function () {
3352
+ _loop_2 = function () {
2517
3353
  var currentTemplate, work_1;
2518
- return __generator(this, function (_e) {
2519
- switch (_e.label) {
3354
+ return __generator(this, function (_h) {
3355
+ switch (_h.label) {
2520
3356
  case 0:
2521
3357
  if (loopLimit-- < 0) {
3358
+ // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
2522
3359
  throw new UnexpectedError('Loop limit reached during resolving parameters pipeline execution');
2523
3360
  }
2524
- currentTemplate = unresovedTemplates.find(function (template) {
2525
- return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
3361
+ currentTemplate = unresovedTemplates_1.find(function (template) {
3362
+ return template.dependentParameterNames.every(function (name) {
3363
+ return __spreadArray(__spreadArray([], __read(resovedParameterNames_1), false), __read(RESERVED_PARAMETER_NAMES), false).includes(name);
3364
+ });
2526
3365
  });
2527
3366
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
2528
- throw new UnexpectedError(spaceTrim.spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePipeline\n "));
3367
+ throw new UnexpectedError(
3368
+ // TODO: [🐎] DRY
3369
+ spaceTrim.spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3370
+ .map(function (_a) {
3371
+ var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
3372
+ return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
3373
+ .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
3374
+ .join(' and '));
3375
+ })
3376
+ .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameterNames_1.map(function (name) { return "- Parameter {".concat(name, "}"); }).join('\n')), "\n\n Note: This should be catched in `validatePipeline`\n "); }));
2529
3377
  case 1:
2530
3378
  if (!!currentTemplate) return [3 /*break*/, 3];
2531
3379
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
2532
3380
  case 2:
2533
- /* [5] */ _e.sent();
3381
+ /* [5] */ _h.sent();
2534
3382
  return [3 /*break*/, 4];
2535
3383
  case 3:
2536
- unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
3384
+ unresovedTemplates_1 = unresovedTemplates_1.filter(function (template) { return template !== currentTemplate; });
2537
3385
  work_1 = executeSingleTemplate(currentTemplate)
2538
3386
  .then(function () {
2539
- resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
3387
+ resovedParameterNames_1 = __spreadArray(__spreadArray([], __read(resovedParameterNames_1), false), [currentTemplate.resultingParameterName], false);
2540
3388
  })
2541
3389
  .then(function () {
2542
3390
  resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
2543
3391
  });
2544
3392
  resolving_1.push(work_1);
2545
- _e.label = 4;
3393
+ _h.label = 4;
2546
3394
  case 4: return [2 /*return*/];
2547
3395
  }
2548
3396
  });
2549
3397
  };
2550
- _d.label = 2;
2551
- case 2:
2552
- if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
2553
- return [5 /*yield**/, _loop_1()];
2554
- case 3:
2555
- _d.sent();
2556
- return [3 /*break*/, 2];
2557
- case 4: return [4 /*yield*/, Promise.all(resolving_1)];
3398
+ _g.label = 4;
3399
+ case 4:
3400
+ if (!(unresovedTemplates_1.length > 0)) return [3 /*break*/, 6];
3401
+ return [5 /*yield**/, _loop_2()];
2558
3402
  case 5:
2559
- _d.sent();
2560
- return [3 /*break*/, 7];
2561
- case 6:
2562
- error_1 = _d.sent();
3403
+ _g.sent();
3404
+ return [3 /*break*/, 4];
3405
+ case 6: return [4 /*yield*/, Promise.all(resolving_1)];
3406
+ case 7:
3407
+ _g.sent();
3408
+ return [3 /*break*/, 9];
3409
+ case 8:
3410
+ error_1 = _g.sent();
2563
3411
  if (!(error_1 instanceof Error)) {
2564
3412
  throw error_1;
2565
3413
  }
2566
3414
  usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2567
3415
  var result = _a.result;
2568
- return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3416
+ return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2569
3417
  })), false));
2570
- return [2 /*return*/, {
3418
+ outputParameters_1 = filterJustOutputParameters();
3419
+ return [2 /*return*/, deepFreezeWithSameType({
2571
3420
  isSuccessful: false,
2572
- errors: [error_1],
3421
+ errors: __spreadArray([error_1], __read(errors), false),
3422
+ warnings: warnings,
2573
3423
  usage: usage_1,
2574
3424
  executionReport: executionReport,
2575
- outputParameters: parametersToPass,
2576
- }];
2577
- case 7:
2578
- try {
2579
- // Note: Filter ONLY output parameters
2580
- for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
2581
- parameter = _b.value;
2582
- if (parameter.isOutput) {
2583
- continue;
2584
- }
2585
- delete parametersToPass[parameter.name];
2586
- }
2587
- }
2588
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2589
- finally {
2590
- try {
2591
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2592
- }
2593
- finally { if (e_1) throw e_1.error; }
2594
- }
3425
+ outputParameters: outputParameters_1,
3426
+ preparedPipeline: preparedPipeline,
3427
+ })];
3428
+ case 9:
2595
3429
  usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2596
3430
  var result = _a.result;
2597
- return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3431
+ return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2598
3432
  })), false));
2599
- return [2 /*return*/, {
3433
+ outputParameters = filterJustOutputParameters();
3434
+ return [2 /*return*/, deepFreezeWithSameType({
2600
3435
  isSuccessful: true,
2601
- errors: [],
3436
+ errors: errors,
3437
+ warnings: warnings,
2602
3438
  usage: usage,
2603
3439
  executionReport: executionReport,
2604
- outputParameters: parametersToPass,
2605
- }];
3440
+ outputParameters: outputParameters,
3441
+ preparedPipeline: preparedPipeline,
3442
+ })];
2606
3443
  }
2607
3444
  });
2608
3445
  }); };
2609
3446
  return pipelineExecutor;
2610
3447
  }
2611
3448
  /**
3449
+ * TODO: Use isVerbose here (not only pass to `preparePipeline`)
3450
+ * TODO: [🧠] Use here `countTotalUsage` and put preparation and prepared pipiline to report
3451
+ * TODO: [🪂] Use maxParallelCount here (not only pass to `preparePipeline`)
3452
+ * TODO: [♈] Probbably move expectations from templates to parameters
2612
3453
  * TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
2613
3454
  * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
2614
3455
  * Note: CreatePipelineExecutorOptions are just connected to PipelineExecutor so do not extract to types folder
2615
3456
  * TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
3457
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3458
+ * TODO: [🧠][💷] `assertsExecutionSuccessful` should be the method of `PipelineExecutor` result BUT maybe NOT to preserve pure JSON object
2616
3459
  */
2617
3460
 
2618
- function prepareKnowledgeFromMarkdown(options) {
3461
+ /**
3462
+ * @@@
3463
+ */
3464
+ function prepareKnowledgeFromMarkdown(knowledgeContent /* <- TODO: [🖖] (?maybe not) Always the file */, options) {
2619
3465
  return __awaiter(this, void 0, void 0, function () {
2620
- var content, llmTools, _a, isVerbose, collection, prepareKnowledgeFromMarkdownExecutor, _b, prepareTitleExecutor, _c, prepareKeywordsExecutor, _d, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
2621
- var _e, _f, _g;
3466
+ var llmTools, _a, maxParallelCount, _b, isVerbose, collection, prepareKnowledgeFromMarkdownExecutor, _c, prepareTitleExecutor, _d, prepareKeywordsExecutor, _e, result, outputParameters, knowledgePiecesRaw, knowledgeTextPieces, knowledge;
3467
+ var _f, _g, _h;
2622
3468
  var _this = this;
2623
- return __generator(this, function (_h) {
2624
- switch (_h.label) {
3469
+ return __generator(this, function (_j) {
3470
+ switch (_j.label) {
2625
3471
  case 0:
2626
- content = options.content, llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
3472
+ llmTools = options.llmTools, _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a, _b = options.isVerbose, isVerbose = _b === void 0 ? false : _b;
3473
+ TODO_USE(maxParallelCount); // <- [🪂]
2627
3474
  collection = createCollectionFromJson.apply(void 0, __spreadArray([], __read(PipelineCollection), false));
2628
- _b = createPipelineExecutor;
2629
- _e = {};
2630
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md')];
2631
- case 1:
2632
- prepareKnowledgeFromMarkdownExecutor = _b.apply(void 0, [(_e.pipeline = _h.sent(),
2633
- _e.tools = {
2634
- llm: llmTools,
2635
- },
2636
- _e)]);
2637
3475
  _c = createPipelineExecutor;
2638
3476
  _f = {};
2639
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md')];
2640
- case 2:
2641
- prepareTitleExecutor = _c.apply(void 0, [(_f.pipeline = _h.sent(),
3477
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md')];
3478
+ case 1:
3479
+ prepareKnowledgeFromMarkdownExecutor = _c.apply(void 0, [(_f.pipeline = _j.sent(),
2642
3480
  _f.tools = {
2643
3481
  llm: llmTools,
2644
3482
  },
2645
3483
  _f)]);
2646
3484
  _d = createPipelineExecutor;
2647
3485
  _g = {};
2648
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md')];
2649
- case 3:
2650
- prepareKeywordsExecutor = _d.apply(void 0, [(_g.pipeline = _h.sent(),
3486
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md')];
3487
+ case 2:
3488
+ prepareTitleExecutor = _d.apply(void 0, [(_g.pipeline = _j.sent(),
2651
3489
  _g.tools = {
2652
3490
  llm: llmTools,
2653
3491
  },
2654
3492
  _g)]);
2655
- return [4 /*yield*/, prepareKnowledgeFromMarkdownExecutor({ content: content })];
3493
+ _e = createPipelineExecutor;
3494
+ _h = {};
3495
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md')];
3496
+ case 3:
3497
+ prepareKeywordsExecutor = _e.apply(void 0, [(_h.pipeline = _j.sent(),
3498
+ _h.tools = {
3499
+ llm: llmTools,
3500
+ },
3501
+ _h)]);
3502
+ return [4 /*yield*/, prepareKnowledgeFromMarkdownExecutor({ knowledgeContent: knowledgeContent })];
2656
3503
  case 4:
2657
- result = _h.sent();
2658
- // TODO: [0] !!! Aggeregate usage
3504
+ result = _j.sent();
2659
3505
  assertsExecutionSuccessful(result);
2660
3506
  outputParameters = result.outputParameters;
2661
- knowledgeRaw = outputParameters.knowledge;
2662
- knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
3507
+ knowledgePiecesRaw = outputParameters.knowledgePieces;
3508
+ knowledgeTextPieces = (knowledgePiecesRaw || '').split('\n---\n');
3509
+ // <- TODO: !!!!! Smarter split and filter out empty pieces
2663
3510
  if (isVerbose) {
2664
3511
  console.info('knowledgeTextPieces:', knowledgeTextPieces);
2665
3512
  }
2666
3513
  return [4 /*yield*/, Promise.all(
2667
- // TODO: !!! Do not send all at once but in chunks
3514
+ // TODO: [🪂] !! Do not send all at once but in chunks
2668
3515
  knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
2669
- var name, title, content, keywords, index, sources, titleResult, _a, titleRaw, keywordsResult, _b, keywordsRaw, embeddingResult, error_1;
3516
+ var name, title, knowledgePieceContent, keywords, index, titleResult, _a, titleRaw, keywordsResult, _b, keywordsRaw, embeddingResult, error_1;
2670
3517
  return __generator(this, function (_c) {
2671
3518
  switch (_c.label) {
2672
3519
  case 0:
2673
3520
  name = "piece-".concat(i);
2674
3521
  title = spaceTrim__default["default"](knowledgeTextPiece.substring(0, 100));
2675
- content = spaceTrim__default["default"](knowledgeTextPiece);
3522
+ knowledgePieceContent = spaceTrim__default["default"](knowledgeTextPiece);
2676
3523
  keywords = [];
2677
3524
  index = [];
2678
- sources = [
2679
- {
2680
- title: 'Markdown document' /* <- TODO: !!! Unhardcode */,
2681
- href: '#' /* <- TODO: !!! Unhardcode */,
2682
- },
2683
- ];
2684
3525
  _c.label = 1;
2685
3526
  case 1:
2686
3527
  _c.trys.push([1, 7, , 8]);
2687
- return [4 /*yield*/, prepareTitleExecutor({ content: content })];
3528
+ return [4 /*yield*/, prepareTitleExecutor({ knowledgePieceContent: knowledgePieceContent })];
2688
3529
  case 2:
2689
3530
  titleResult = _c.sent();
2690
3531
  _a = titleResult.outputParameters.title, titleRaw = _a === void 0 ? 'Untitled' : _a;
2691
3532
  title = spaceTrim__default["default"](titleRaw) /* <- TODO: Maybe do in pipeline */;
2692
3533
  name = titleToName(title);
2693
- return [4 /*yield*/, prepareKeywordsExecutor({ content: content })];
3534
+ return [4 /*yield*/, prepareKeywordsExecutor({ knowledgePieceContent: knowledgePieceContent })];
2694
3535
  case 3:
2695
3536
  keywordsResult = _c.sent();
2696
3537
  _b = keywordsResult.outputParameters.keywords, keywordsRaw = _b === void 0 ? '' : _b;
@@ -2708,14 +3549,13 @@
2708
3549
  case 4: return [4 /*yield*/, llmTools.callEmbeddingModel({
2709
3550
  title: "Embedding for ".concat(title) /* <- Note: No impact on embedding result itself, just for logging */,
2710
3551
  parameters: {},
2711
- content: content,
3552
+ content: knowledgePieceContent,
2712
3553
  modelRequirements: {
2713
3554
  modelVariant: 'EMBEDDING',
2714
3555
  },
2715
3556
  })];
2716
3557
  case 5:
2717
3558
  embeddingResult = _c.sent();
2718
- // TODO: [0] !!! Aggeregate usage embeddingResult.usage
2719
3559
  index.push({
2720
3560
  modelName: embeddingResult.modelName,
2721
3561
  position: embeddingResult.content,
@@ -2730,24 +3570,554 @@
2730
3570
  case 8: return [2 /*return*/, {
2731
3571
  name: name,
2732
3572
  title: title,
2733
- content: content,
3573
+ content: knowledgePieceContent,
2734
3574
  keywords: keywords,
2735
3575
  index: index,
2736
- sources: sources,
3576
+ // <- TODO: [☀] sources,
2737
3577
  }];
2738
3578
  }
2739
3579
  });
2740
3580
  }); }))];
2741
3581
  case 5:
2742
- knowledge = _h.sent();
3582
+ knowledge = _j.sent();
2743
3583
  return [2 /*return*/, knowledge];
2744
3584
  }
2745
3585
  });
2746
- });
2747
- }
3586
+ });
3587
+ }
3588
+ /**
3589
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/markdown`
3590
+ * TODO: [🪂] Do it in parallel 11:11
3591
+ * Note: No need to aggregate usage here, it is done by intercepting the llmTools
3592
+ */
3593
+
3594
+ /**
3595
+ * Prepares the knowle
3596
+ *
3597
+ * @see https://github.com/webgptorg/promptbook/discussions/41
3598
+ */
3599
+ function prepareKnowledgePieces(knowledgeSources, options) {
3600
+ return __awaiter(this, void 0, void 0, function () {
3601
+ var _a, maxParallelCount, knowledgePrepared;
3602
+ var _this = this;
3603
+ return __generator(this, function (_b) {
3604
+ switch (_b.label) {
3605
+ case 0:
3606
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a;
3607
+ knowledgePrepared = [];
3608
+ return [4 /*yield*/, forEachAsync(knowledgeSources, { maxParallelCount: maxParallelCount }, function (knowledgeSource) { return __awaiter(_this, void 0, void 0, function () {
3609
+ var partialPieces, pieces;
3610
+ return __generator(this, function (_a) {
3611
+ switch (_a.label) {
3612
+ case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!! Unhardcode markdown, detect which type it is
3613
+ options)];
3614
+ case 1:
3615
+ partialPieces = _a.sent();
3616
+ pieces = partialPieces.map(function (partialPiece) { return (__assign(__assign({}, partialPiece), { sources: [
3617
+ {
3618
+ name: knowledgeSource.name,
3619
+ // line, column <- TODO: [☀]
3620
+ // <- TODO: [❎]
3621
+ },
3622
+ ] })); });
3623
+ knowledgePrepared.push.apply(knowledgePrepared, __spreadArray([], __read(pieces), false));
3624
+ return [2 /*return*/];
3625
+ }
3626
+ });
3627
+ }); })];
3628
+ case 1:
3629
+ _b.sent();
3630
+ return [2 /*return*/, knowledgePrepared];
3631
+ }
3632
+ });
3633
+ });
3634
+ }
3635
+ /*
3636
+ TODO: [🧊] This is how it can look in future
3637
+ > type PrepareKnowledgeKnowledge = {
3638
+ > /**
3639
+ > * Unprepared knowledge
3640
+ > * /
3641
+ > readonly knowledgeSources: Array<KnowledgeSourceJson>;
3642
+ > };
3643
+ >
3644
+ > export async function prepareKnowledgePieces(
3645
+ > knowledge: PrepareKnowledgeKnowledge,
3646
+ > options: PrepareOptions,
3647
+ > ):
3648
+ */
3649
+ /**
3650
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/core`
3651
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3652
+ * Put `knowledgePieces` into `PrepareKnowledgeOptions`
3653
+ * TODO: [🪂] More than max things can run in parallel by acident [1,[2a,2b,_],[3a,3b,_]]
3654
+ * TODO: [🧠][❎] Do here propper M:N mapping
3655
+ * [x] One source can make multiple pieces
3656
+ * [ ] One piece can have multiple sources
3657
+ */
3658
+
3659
+ /**
3660
+ * Intercepts LLM tools and counts total usage of the tools
3661
+ *
3662
+ * @param llmTools LLM tools to be intercepted with usage counting
3663
+ * @returns LLM tools with same functionality with added total cost counting
3664
+ */
3665
+ function countTotalUsage(llmTools) {
3666
+ var _this = this;
3667
+ var totalUsage = ZERO_USAGE;
3668
+ var proxyTools = {
3669
+ get title() {
3670
+ // TODO: [🧠] Maybe put here some suffix
3671
+ return llmTools.title;
3672
+ },
3673
+ get description() {
3674
+ // TODO: [🧠] Maybe put here some suffix
3675
+ return llmTools.description;
3676
+ },
3677
+ listModels: function () {
3678
+ return /* not await */ llmTools.listModels();
3679
+ },
3680
+ get totalUsage() {
3681
+ return totalUsage;
3682
+ },
3683
+ };
3684
+ if (llmTools.callChatModel !== undefined) {
3685
+ proxyTools.callChatModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3686
+ var promptResult;
3687
+ return __generator(this, function (_a) {
3688
+ switch (_a.label) {
3689
+ case 0: return [4 /*yield*/, llmTools.callChatModel(prompt)];
3690
+ case 1:
3691
+ promptResult = _a.sent();
3692
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3693
+ return [2 /*return*/, promptResult];
3694
+ }
3695
+ });
3696
+ }); };
3697
+ }
3698
+ if (llmTools.callCompletionModel !== undefined) {
3699
+ proxyTools.callCompletionModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3700
+ var promptResult;
3701
+ return __generator(this, function (_a) {
3702
+ switch (_a.label) {
3703
+ case 0: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
3704
+ case 1:
3705
+ promptResult = _a.sent();
3706
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3707
+ return [2 /*return*/, promptResult];
3708
+ }
3709
+ });
3710
+ }); };
3711
+ }
3712
+ if (llmTools.callEmbeddingModel !== undefined) {
3713
+ proxyTools.callEmbeddingModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3714
+ var promptResult;
3715
+ return __generator(this, function (_a) {
3716
+ switch (_a.label) {
3717
+ case 0: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
3718
+ case 1:
3719
+ promptResult = _a.sent();
3720
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3721
+ return [2 /*return*/, promptResult];
3722
+ }
3723
+ });
3724
+ }); };
3725
+ }
3726
+ // <- Note: [🤖]
3727
+ return proxyTools;
3728
+ }
3729
+ /**
3730
+ * TODO: [🔼] !!! Export via `@promptbookcore/`
3731
+ * TODO: [🧠][💸] Maybe make some common abstraction `interceptLlmTools` and use here (or use javascript Proxy?)
3732
+ * TODO: [🧠] Is there some meaningfull way how to test this util
3733
+ * TODO: [🧠][🌯] Maybe a way how to hide ability to `get totalUsage`
3734
+ * > const [llmToolsWithUsage,getUsage] = countTotalUsage(llmTools);
3735
+ * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
3736
+ */
3737
+
3738
+ /**
3739
+ * Prepares the persona for the pipeline
3740
+ *
3741
+ * @see https://github.com/webgptorg/promptbook/discussions/22
3742
+ */
3743
+ function preparePersona(personaDescription, options) {
3744
+ return __awaiter(this, void 0, void 0, function () {
3745
+ var llmTools, _a, isVerbose, collection, preparePersonaExecutor, _b, availableModels, availableModelNames, result, outputParameters, modelRequirementsRaw, modelRequirements, modelName, systemMessage, temperature;
3746
+ var _c;
3747
+ return __generator(this, function (_d) {
3748
+ switch (_d.label) {
3749
+ case 0:
3750
+ llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
3751
+ collection = createCollectionFromJson.apply(void 0, __spreadArray([], __read(PipelineCollection), false));
3752
+ _b = createPipelineExecutor;
3753
+ _c = {};
3754
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-persona.ptbk.md')];
3755
+ case 1:
3756
+ preparePersonaExecutor = _b.apply(void 0, [(_c.pipeline = _d.sent(),
3757
+ _c.tools = {
3758
+ llm: llmTools,
3759
+ },
3760
+ _c)]);
3761
+ return [4 /*yield*/, llmTools.listModels()];
3762
+ case 2:
3763
+ availableModels = _d.sent();
3764
+ availableModelNames = availableModels
3765
+ .filter(function (_a) {
3766
+ var modelVariant = _a.modelVariant;
3767
+ return modelVariant === 'CHAT';
3768
+ })
3769
+ .map(function (_a) {
3770
+ var modelName = _a.modelName;
3771
+ return modelName;
3772
+ })
3773
+ .join(',');
3774
+ return [4 /*yield*/, preparePersonaExecutor({ availableModelNames: availableModelNames, personaDescription: personaDescription })];
3775
+ case 3:
3776
+ result = _d.sent();
3777
+ assertsExecutionSuccessful(result);
3778
+ outputParameters = result.outputParameters;
3779
+ modelRequirementsRaw = outputParameters.modelRequirements;
3780
+ modelRequirements = JSON.parse(modelRequirementsRaw);
3781
+ if (isVerbose) {
3782
+ console.info("PERSONA ".concat(personaDescription), modelRequirements);
3783
+ }
3784
+ modelName = modelRequirements.modelName, systemMessage = modelRequirements.systemMessage, temperature = modelRequirements.temperature;
3785
+ return [2 /*return*/, {
3786
+ modelVariant: 'CHAT',
3787
+ modelName: modelName,
3788
+ systemMessage: systemMessage,
3789
+ temperature: temperature,
3790
+ }];
3791
+ }
3792
+ });
3793
+ });
3794
+ }
3795
+ /**
3796
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3797
+ * TODO: [🏢] !! Check validity of `modelName` in pipeline
3798
+ * TODO: [🏢] !! Check validity of `systemMessage` in pipeline
3799
+ * TODO: [🏢] !! Check validity of `temperature` in pipeline
3800
+ */
3801
+
3802
+ /**
3803
+ * @@@
3804
+ */
3805
+ function prepareTemplates(pipeline, options) {
3806
+ return __awaiter(this, void 0, void 0, function () {
3807
+ var _a, maxParallelCount, promptTemplates, parameters, knowledgePiecesCount, promptTemplatesPrepared;
3808
+ var _this = this;
3809
+ return __generator(this, function (_b) {
3810
+ switch (_b.label) {
3811
+ case 0:
3812
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a;
3813
+ promptTemplates = pipeline.promptTemplates, parameters = pipeline.parameters, knowledgePiecesCount = pipeline.knowledgePiecesCount;
3814
+ // TODO: !!!!! Apply samples to each template (if missing and is for the template defined)
3815
+ TODO_USE(parameters);
3816
+ promptTemplatesPrepared = new Array(promptTemplates.length);
3817
+ return [4 /*yield*/, forEachAsync(promptTemplates, { maxParallelCount: maxParallelCount /* <- TODO: [🪂] When there are subtasks, this maximul limit can be broken */ }, function (template, index) { return __awaiter(_this, void 0, void 0, function () {
3818
+ var dependentParameterNames, preparedContent, preparedTemplate;
3819
+ return __generator(this, function (_a) {
3820
+ dependentParameterNames = template.dependentParameterNames;
3821
+ preparedContent = undefined;
3822
+ if (knowledgePiecesCount > 0 && !dependentParameterNames.includes('knowledge')) {
3823
+ preparedContent = spaceTrim.spaceTrim("\n {content}\n\n ## Knowledge\n\n {knowledge}\n ");
3824
+ // <- TODO: [🧠][🧻] Cutomize shape/language/formatting of the addition to the prompt
3825
+ dependentParameterNames = __spreadArray(__spreadArray([], __read(dependentParameterNames), false), [
3826
+ 'knowledge',
3827
+ ], false);
3828
+ }
3829
+ preparedTemplate = __assign(__assign({}, template), { dependentParameterNames: dependentParameterNames, preparedContent: preparedContent });
3830
+ promptTemplatesPrepared[index] = preparedTemplate;
3831
+ return [2 /*return*/];
3832
+ });
3833
+ }); })];
3834
+ case 1:
3835
+ _b.sent();
3836
+ return [2 /*return*/, { promptTemplatesPrepared: promptTemplatesPrepared }];
3837
+ }
3838
+ });
3839
+ });
3840
+ }
3841
+ /**
3842
+ * TODO: [🧠] Add context to each template (if missing)
3843
+ * TODO: [🧠] What is better name `prepareTemplate` or `prepareTemplateAndParameters`
3844
+ * TODO: [♨] !!! Prepare index the samples and maybe templates
3845
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3846
+ * TODO: Write tests for `preparePipeline`
3847
+ * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
3848
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3849
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3850
+ * TODO: [🧠][🥜]
3851
+ */
3852
+
3853
+ /**
3854
+ * Prepare pipeline from string (markdown) format to JSON format
3855
+ *
3856
+ * Note: This function does not validate logic of the pipeline
3857
+ * Note: This function acts as part of compilation process
3858
+ */
3859
+ function preparePipeline(pipeline, options) {
3860
+ return __awaiter(this, void 0, void 0, function () {
3861
+ var llmTools, _a, maxParallelCount, _b, isVerbose, parameters, promptTemplates,
3862
+ /*
3863
+ <- TODO: [🧠][0] `promptbookVersion` */
3864
+ knowledgeSources /*
3865
+ <- TODO: [🧊] `knowledgePieces` */, personas /*
3866
+ <- TODO: [🧊] `preparations` */, llmToolsWithUsage, currentPreparation, preparations, preparedPersonas, knowledgeSourcesPrepared, partialknowledgePiecesPrepared, knowledgePiecesPrepared, promptTemplatesPrepared /* TODO: parameters: parametersPrepared*/;
3867
+ var _this = this;
3868
+ return __generator(this, function (_c) {
3869
+ switch (_c.label) {
3870
+ case 0:
3871
+ llmTools = options.llmTools, _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a, _b = options.isVerbose, isVerbose = _b === void 0 ? false : _b;
3872
+ parameters = pipeline.parameters, promptTemplates = pipeline.promptTemplates, knowledgeSources = pipeline.knowledgeSources, personas = pipeline.personas;
3873
+ llmToolsWithUsage = countTotalUsage(llmTools);
3874
+ currentPreparation = {
3875
+ id: 1,
3876
+ // TODO: [🍥]> date: $currentDate(),
3877
+ promptbookVersion: PROMPTBOOK_VERSION,
3878
+ modelUsage: ZERO_USAGE,
3879
+ };
3880
+ preparations = [
3881
+ // ...preparations
3882
+ // <- TODO: [🧊]
3883
+ currentPreparation,
3884
+ ];
3885
+ preparedPersonas = new Array(personas.length);
3886
+ return [4 /*yield*/, forEachAsync(personas, { maxParallelCount: maxParallelCount /* <- TODO: [🪂] When there are subtasks, this maximul limit can be broken */ }, function (persona, index) { return __awaiter(_this, void 0, void 0, function () {
3887
+ var modelRequirements, preparedPersona;
3888
+ return __generator(this, function (_a) {
3889
+ switch (_a.label) {
3890
+ case 0: return [4 /*yield*/, preparePersona(persona.description, {
3891
+ llmTools: llmToolsWithUsage,
3892
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3893
+ isVerbose: isVerbose,
3894
+ })];
3895
+ case 1:
3896
+ modelRequirements = _a.sent();
3897
+ preparedPersona = __assign(__assign({}, persona), { modelRequirements: modelRequirements, preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] });
3898
+ preparedPersonas[index] = preparedPersona;
3899
+ return [2 /*return*/];
3900
+ }
3901
+ });
3902
+ }); })];
3903
+ case 1:
3904
+ _c.sent();
3905
+ knowledgeSourcesPrepared = knowledgeSources.map(function (source) { return (__assign(__assign({}, source), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
3906
+ return [4 /*yield*/, prepareKnowledgePieces(knowledgeSources /* <- TODO: [🧊] {knowledgeSources, knowledgePieces} */, {
3907
+ llmTools: llmToolsWithUsage,
3908
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3909
+ isVerbose: isVerbose,
3910
+ })];
3911
+ case 2:
3912
+ partialknowledgePiecesPrepared = _c.sent();
3913
+ knowledgePiecesPrepared = partialknowledgePiecesPrepared.map(function (piece) { return (__assign(__assign({}, piece), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
3914
+ return [4 /*yield*/, prepareTemplates({
3915
+ parameters: parameters,
3916
+ promptTemplates: promptTemplates,
3917
+ knowledgePiecesCount: knowledgePiecesPrepared.length,
3918
+ }, {
3919
+ llmTools: llmToolsWithUsage,
3920
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3921
+ isVerbose: isVerbose,
3922
+ })];
3923
+ case 3:
3924
+ promptTemplatesPrepared = (_c.sent()).promptTemplatesPrepared;
3925
+ // ----- /Templates preparation -----
3926
+ // Note: Count total usage
3927
+ currentPreparation.modelUsage = llmToolsWithUsage.totalUsage;
3928
+ return [2 /*return*/, __assign(__assign({}, pipeline), { promptTemplates: promptTemplatesPrepared, knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations })];
3929
+ }
3930
+ });
3931
+ });
3932
+ }
3933
+ /**
3934
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3935
+ * TODO: Write tests for `preparePipeline`
3936
+ * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
3937
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3938
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3939
+ */
3940
+
3941
+ /**
3942
+ * Tests if given string is valid URL.
3943
+ *
3944
+ * Note: This does not check if the file exists only if the path is valid
3945
+ */
3946
+ function isValidFilePath(filePath) {
3947
+ if (typeof filePath !== 'string') {
3948
+ return false;
3949
+ }
3950
+ var filePathSlashes = filePath.split('\\').join('/');
3951
+ // Absolute Unix path: /hello.txt
3952
+ if (/^(\/)/i.test(filePathSlashes)) {
3953
+ return true;
3954
+ }
3955
+ // Absolute Windows path: /hello.txt
3956
+ if (/^([A-Z]{1,2}:\/?)\//i.test(filePathSlashes)) {
3957
+ return true;
3958
+ }
3959
+ // Relative path: ./hello.txt
3960
+ if (/^(\.\.?\/)+/i.test(filePathSlashes)) {
3961
+ return true;
3962
+ }
3963
+ return false;
3964
+ }
3965
+
3966
+ /**
3967
+ * Parses the knowledge command
3968
+ *
3969
+ * @see ./KNOWLEDGE-README.md for more details
3970
+ * @private within the commands folder
3971
+ */
3972
+ var knowledgeCommandParser = {
3973
+ /**
3974
+ * Name of the command
3975
+ */
3976
+ name: 'KNOWLEDGE',
3977
+ /**
3978
+ * BOILERPLATE command can be used in:
3979
+ */
3980
+ usagePlaces: ['PIPELINE_HEAD'],
3981
+ /**
3982
+ * Description of the KNOWLEDGE command
3983
+ */
3984
+ description: "Tells promptbook which external knowledge to use",
3985
+ /**
3986
+ * Link to discussion
3987
+ */
3988
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3989
+ /**
3990
+ * Example usages of the KNOWLEDGE command
3991
+ */
3992
+ examples: [
3993
+ 'KNOWLEDGE https://www.pavolhejny.com/',
3994
+ 'KNOWLEDGE ./hejny-cv.txt',
3995
+ 'KNOWLEDGE ./hejny-cv.md',
3996
+ 'KNOWLEDGE ./hejny-cv.pdf',
3997
+ 'KNOWLEDGE ./hejny-cv.docx',
3998
+ ],
3999
+ /**
4000
+ * Parses the KNOWLEDGE command
4001
+ */
4002
+ parse: function (input) {
4003
+ var args = input.args;
4004
+ var source = args[0];
4005
+ if (source === undefined) {
4006
+ throw new ParsingError("Source is not defined");
4007
+ }
4008
+ if (source.startsWith('http://')) {
4009
+ throw new ParsingError("Source is not secure");
4010
+ }
4011
+ if (!(isValidFilePath(source) || isValidUrl(source))) {
4012
+ throw new ParsingError("Source not valid");
4013
+ }
4014
+ if (source.startsWith('../') || source.startsWith('/') || /^[A-Z]:[\\/]+/i.test(source)) {
4015
+ throw new ParsingError("Source cannot be outside of the .ptbk.md folder");
4016
+ }
4017
+ return {
4018
+ type: 'KNOWLEDGE',
4019
+ source: source,
4020
+ };
4021
+ },
4022
+ /**
4023
+ * Note: Prototype of [🍧] (remove this comment after full implementation)
4024
+ */
4025
+ applyToPipelineJson: function (personaCommand, subjects) {
4026
+ var source = personaCommand.source;
4027
+ var pipelineJson = subjects.pipelineJson;
4028
+ var name = titleToName(source);
4029
+ pipelineJson.knowledgeSources.push({
4030
+ name: name,
4031
+ source: source,
4032
+ });
4033
+ },
4034
+ };
4035
+
2748
4036
  /**
2749
- * 11:11
4037
+ * Parses the persona command
4038
+ *
4039
+ * @see ./PERSONA-README.md for more details
4040
+ * @private within the commands folder
2750
4041
  */
4042
+ var personaCommandParser = {
4043
+ /**
4044
+ * Name of the command
4045
+ */
4046
+ name: 'PERSONA',
4047
+ /**
4048
+ * Aliases for the PERSONA command
4049
+ */
4050
+ aliasNames: ['PERSON'],
4051
+ /**
4052
+ * PERSONA command can be used in:
4053
+ */
4054
+ usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
4055
+ /**
4056
+ * Description of the PERSONA command
4057
+ */
4058
+ description: "Persona command is used to specify who the system is, it will be transformed into system message, top_t,...",
4059
+ /**
4060
+ * Link to discussion
4061
+ */
4062
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
4063
+ /**
4064
+ * Example usages of the PERSONA command
4065
+ */
4066
+ examples: ['PERSONA Jane, skilled copywriter', 'PERSONA Joe, male 28 years old, programmer'],
4067
+ /**
4068
+ * Parses the PERSONA command
4069
+ */
4070
+ parse: function (input) {
4071
+ var rawArgs = input.rawArgs;
4072
+ var _a = __read(rawArgs.split(/[,;:]/, 2), 2), personaNameRaw = _a[0], personaDescriptionRaw = _a[1];
4073
+ var personaName = (personaNameRaw || '').trim();
4074
+ if (personaName === '') {
4075
+ throw new ParsingError("You must set name for the persona");
4076
+ }
4077
+ var personaDescription = (personaDescriptionRaw || '').trim();
4078
+ if (personaDescription === '') {
4079
+ personaDescription = null;
4080
+ }
4081
+ return {
4082
+ type: 'PERSONA',
4083
+ personaName: personaName,
4084
+ personaDescription: personaDescription,
4085
+ };
4086
+ },
4087
+ /**
4088
+ * Note: Prototype of [🍧] (remove this comment after full implementation)
4089
+ */
4090
+ applyToPipelineJson: function (personaCommand, subjects) {
4091
+ var personaName = personaCommand.personaName, personaDescription = personaCommand.personaDescription;
4092
+ var pipelineJson = subjects.pipelineJson, templateJson = subjects.templateJson;
4093
+ if (templateJson !== null) {
4094
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
4095
+ throw new ParsingError("PERSONA command can be used only in PROMPT_TEMPLATE block");
4096
+ }
4097
+ templateJson.personaName = personaName;
4098
+ }
4099
+ var persona = pipelineJson.personas.find(function (persona) { return persona.name === personaName; });
4100
+ if (persona === undefined) {
4101
+ pipelineJson.personas.push({
4102
+ name: personaName,
4103
+ description: personaDescription || '',
4104
+ });
4105
+ return;
4106
+ }
4107
+ if (persona.description === personaDescription) {
4108
+ return;
4109
+ }
4110
+ if (personaDescription === null) {
4111
+ return;
4112
+ }
4113
+ if (persona.description === '') {
4114
+ persona.description = personaDescription;
4115
+ return;
4116
+ }
4117
+ console.warn(spaceTrim__default["default"]("\n\n Persona \"".concat(personaName, "\" is defined multiple times with different description:\n\n First definition:\n ").concat(persona.description, "\n\n Second definition:\n ").concat(personaDescription, "\n\n ")));
4118
+ persona.description += spaceTrim__default["default"]('\n\n' + personaDescription);
4119
+ },
4120
+ };
2751
4121
 
2752
4122
  /**
2753
4123
  * Removes Markdown formatting tags from a string.
@@ -2867,7 +4237,7 @@
2867
4237
  /**
2868
4238
  * Link to discussion
2869
4239
  */
2870
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
4240
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
2871
4241
  /**
2872
4242
  * Example usages of the BLOCK command
2873
4243
  */
@@ -2887,7 +4257,7 @@
2887
4257
  'Knowledge BLOCK',
2888
4258
  // 'Knowledge', // <- Note: [⛱] For execution blocks which are also separate commands shortcut does not work
2889
4259
  //---
2890
- /* TODO: !!!! Not implemented block types will be in examples in future -> */
4260
+ /* Note: Not implemented block types will be in examples in future -> */
2891
4261
  'Instrument BLOCK',
2892
4262
  // 'Instrument', // <- Note: [⛱]
2893
4263
  'Action BLOCK',
@@ -2895,6 +4265,7 @@
2895
4265
  //---
2896
4266
  /* <- TODO: [🧠] Maybe dynamic */
2897
4267
  ],
4268
+ // TODO: [♓️] order: -10 /* <- Note: Putting before other commands */
2898
4269
  /**
2899
4270
  * Parses the BLOCK command
2900
4271
  */
@@ -2903,19 +4274,19 @@
2903
4274
  normalized = normalized.split('EXAMPLE').join('SAMPLE');
2904
4275
  var blockTypes = BlockTypes.filter(function (blockType) { return normalized.includes(blockType); });
2905
4276
  if (blockTypes.length !== 1) {
2906
- // console.log('!!!', { blockType });
2907
4277
  throw new ParsingError(spaceTrim__default["default"](function (block) { return "\n Unknown block type in BLOCK command\n\n Supported block types are:\n ".concat(block(BlockTypes.join(', ')), "\n "); }));
2908
4278
  }
2909
- // TODO: !!!! Not supported yet
4279
+ var blockType = blockTypes[0];
2910
4280
  return {
2911
4281
  type: 'BLOCK',
2912
- blockType: blockTypes[0],
4282
+ blockType: blockType,
2913
4283
  };
2914
4284
  },
2915
4285
  };
2916
4286
 
2917
4287
  /**
2918
4288
  * Units of text measurement
4289
+ * @see https://github.com/webgptorg/promptbook/discussions/30
2919
4290
  */
2920
4291
  var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
2921
4292
  /**
@@ -3011,7 +4382,7 @@
3011
4382
  /**
3012
4383
  * Link to discussion
3013
4384
  */
3014
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
4385
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
3015
4386
  /**
3016
4387
  * Example usages of the EXPECT command
3017
4388
  */
@@ -3129,7 +4500,7 @@
3129
4500
  /**
3130
4501
  * Link to discussion
3131
4502
  */
3132
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
4503
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
3133
4504
  /**
3134
4505
  * Example usages of the JOKER command
3135
4506
  */
@@ -3152,94 +4523,11 @@
3152
4523
  };
3153
4524
 
3154
4525
  /**
3155
- * Tests if given string is valid URL.
3156
- *
3157
- * Note: This does not check if the file exists only if the path is valid
3158
- */
3159
- function isValidFilePath(filePath) {
3160
- if (typeof filePath !== 'string') {
3161
- return false;
3162
- }
3163
- var filePathSlashes = filePath.split('\\').join('/');
3164
- // Absolute Unix path: /hello.txt
3165
- if (/^(\/)/i.test(filePathSlashes)) {
3166
- return true;
3167
- }
3168
- // Absolute Windows path: /hello.txt
3169
- if (/^([A-Z]{1,2}:\/?)\//i.test(filePathSlashes)) {
3170
- return true;
3171
- }
3172
- // Relative path: ./hello.txt
3173
- if (/^(\.\.?\/)+/i.test(filePathSlashes)) {
3174
- return true;
3175
- }
3176
- return false;
3177
- }
3178
-
3179
- /**
3180
- * Parses the knowledge command
4526
+ * @@@
3181
4527
  *
3182
- * @see ./KNOWLEDGE-README.md for more details
3183
- * @private within the commands folder
4528
+ * @private for `ModelVariant` and `modelCommandParser`
3184
4529
  */
3185
- var knowledgeCommandParser = {
3186
- /**
3187
- * Name of the command
3188
- */
3189
- name: 'KNOWLEDGE',
3190
- /**
3191
- * BOILERPLATE command can be used in:
3192
- */
3193
- usagePlaces: ['PIPELINE_HEAD'],
3194
- /**
3195
- * Description of the KNOWLEDGE command
3196
- */
3197
- description: "Tells promptbook which external knowledge to use",
3198
- /**
3199
- * Link to discussion
3200
- */
3201
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3202
- /**
3203
- * Example usages of the KNOWLEDGE command
3204
- */
3205
- examples: [
3206
- 'KNOWLEDGE https://www.pavolhejny.com/',
3207
- 'KNOWLEDGE ./hejny-cv.txt',
3208
- 'KNOWLEDGE ./hejny-cv.md',
3209
- 'KNOWLEDGE ./hejny-cv.pdf',
3210
- 'KNOWLEDGE ./hejny-cv.docx',
3211
- ],
3212
- /**
3213
- * Parses the KNOWLEDGE command
3214
- */
3215
- parse: function (input) {
3216
- var args = input.args;
3217
- var source = args[0];
3218
- if (source === undefined) {
3219
- throw new ParsingError("Source is not defined");
3220
- }
3221
- if (source.startsWith('http://')) {
3222
- throw new ParsingError("Source is not secure");
3223
- }
3224
- if (!(isValidFilePath(source) || isValidUrl(source))) {
3225
- throw new ParsingError("Source not valid");
3226
- }
3227
- if (source.startsWith('../') || source.startsWith('/') || /^[A-Z]:[\\/]+/i.test(source)) {
3228
- throw new ParsingError("Source cannot be outside of the .ptbk.md folder");
3229
- }
3230
- return {
3231
- type: 'KNOWLEDGE',
3232
- source: source,
3233
- };
3234
- },
3235
- };
3236
-
3237
4530
  var MODEL_VARIANTS = ['COMPLETION', 'CHAT', 'EMBEDDING' /* <- TODO [🏳] */ /* <- [🤖] */];
3238
- /**
3239
- * TODO: Maybe figure out better word than "variant"
3240
- * TODO: Add here more requirement options like max context size, max tokens, etc.
3241
- * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
3242
- */
3243
4531
 
3244
4532
  /**
3245
4533
  * Parses the model command
@@ -3255,7 +4543,11 @@
3255
4543
  /**
3256
4544
  * BOILERPLATE command can be used in:
3257
4545
  */
3258
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
4546
+ usagePlaces: [
4547
+ 'PIPELINE_HEAD',
4548
+ // <- TODO: [🧠][❔] Should there be possibility to set MODEL for entire pipeline?
4549
+ 'PIPELINE_TEMPLATE',
4550
+ ],
3259
4551
  /**
3260
4552
  * Description of the MODEL command
3261
4553
  */
@@ -3263,7 +4555,7 @@
3263
4555
  /**
3264
4556
  * Link to discussion
3265
4557
  */
3266
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
4558
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
3267
4559
  /**
3268
4560
  * Example usages of the MODEL command
3269
4561
  */
@@ -3343,11 +4635,11 @@
3343
4635
  /**
3344
4636
  * Link to discussion
3345
4637
  */
3346
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
4638
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
3347
4639
  /**
3348
4640
  * Example usages of the PARAMETER command
3349
4641
  */
3350
- examples: ['PARAMETER {title} Title of the book', 'OUTPUT PARAMETER {content} Content of the book'],
4642
+ examples: ['PARAMETER {title} Title of the book', 'OUTPUT PARAMETER {websiteContent} Content of the book'],
3351
4643
  /**
3352
4644
  * Parses the PARAMETER command
3353
4645
  */
@@ -3377,59 +4669,6 @@
3377
4669
  },
3378
4670
  };
3379
4671
 
3380
- /**
3381
- * Parses the persona command
3382
- *
3383
- * @see ./PERSONA-README.md for more details
3384
- * @private within the commands folder
3385
- */
3386
- var personaCommandParser = {
3387
- /**
3388
- * Name of the command
3389
- */
3390
- name: 'PERSONA',
3391
- /**
3392
- * Aliases for the PERSONA command
3393
- */
3394
- aliasNames: ['PERSON'],
3395
- /**
3396
- * PERSONA command can be used in:
3397
- */
3398
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
3399
- /**
3400
- * Description of the PERSONA command
3401
- */
3402
- description: "Persona command is used to specify who the system is, it will be transformed into system message, top_t,...",
3403
- /**
3404
- * Link to discussion
3405
- */
3406
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
3407
- /**
3408
- * Example usages of the PERSONA command
3409
- */
3410
- examples: ['PERSONA Jane, skilled copywriter', 'PERSONA Joe, male 28 years old, programmer'],
3411
- /**
3412
- * Parses the PERSONA command
3413
- */
3414
- parse: function (input) {
3415
- var rawArgs = input.rawArgs;
3416
- var _a = __read(rawArgs.split(/[,;:]/, 2), 2), personaNameRaw = _a[0], personaDescriptionRaw = _a[1];
3417
- var personaName = (personaNameRaw || '').trim();
3418
- if (personaName === '') {
3419
- throw new ParsingError("You must set name for the persona");
3420
- }
3421
- var personaDescription = (personaDescriptionRaw || '').trim();
3422
- if (personaDescription === '') {
3423
- personaDescription = null;
3424
- }
3425
- return {
3426
- type: 'PERSONA',
3427
- personaName: personaName,
3428
- personaDescription: personaDescription,
3429
- };
3430
- },
3431
- };
3432
-
3433
4672
  function isValidJavascriptName(javascriptName) {
3434
4673
  if (typeof javascriptName !== 'string') {
3435
4674
  return false;
@@ -3460,7 +4699,7 @@
3460
4699
  /**
3461
4700
  * Link to discussion
3462
4701
  */
3463
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
4702
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
3464
4703
  /**
3465
4704
  * Example usages of the POSTPROCESS command
3466
4705
  */
@@ -3499,11 +4738,8 @@
3499
4738
  /**
3500
4739
  * Name of the command
3501
4740
  */
3502
- name: 'VERSION',
3503
- /*
3504
- Note: [📇] No need to put here "PROMPTBOOK" alias here
3505
- aliasNames: ['PROMPTBOOK_VERSION'],
3506
- */
4741
+ name: 'PROMPTBOOK_VERSION',
4742
+ aliasNames: ['PTBK_VERSION', 'PTBK_V', 'PTBKV'],
3507
4743
  /**
3508
4744
  * BOILERPLATE command can be used in:
3509
4745
  */
@@ -3515,11 +4751,11 @@
3515
4751
  /**
3516
4752
  * Link to discussion
3517
4753
  */
3518
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
4754
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
3519
4755
  /**
3520
4756
  * Example usages of the PROMPTBOOK_VERSION command
3521
4757
  */
3522
- examples: ["PROMPTBOOK VERSION ".concat(PROMPTBOOK_VERSION), "VERSION ".concat(PROMPTBOOK_VERSION)],
4758
+ examples: ["PROMPTBOOK VERSION ".concat(PROMPTBOOK_VERSION), "PTBKV ".concat(PROMPTBOOK_VERSION)],
3523
4759
  /**
3524
4760
  * Parses the PROMPTBOOK_VERSION command
3525
4761
  */
@@ -3553,8 +4789,9 @@
3553
4789
  * Name of the command
3554
4790
  */
3555
4791
  name: 'URL',
4792
+ aliasNames: ['PIPELINE_URL'],
3556
4793
  /*
3557
- Note: [🛵] No need for alias name because it is already preprocessed
4794
+ Note: [🛵] No need for this alias name because it is already preprocessed
3558
4795
  aliasNames: ['HTTPS'],
3559
4796
  */
3560
4797
  /**
@@ -3568,11 +4805,12 @@
3568
4805
  /**
3569
4806
  * Link to discussion
3570
4807
  */
3571
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
4808
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
3572
4809
  /**
3573
4810
  * Example usages of the URL command
3574
4811
  */
3575
4812
  examples: [
4813
+ 'PIPELINE URL https://promptbook.studio/library/write-cv.ptbk.md',
3576
4814
  'URL https://promptbook.studio/library/write-cv.ptbk.md',
3577
4815
  'https://promptbook.studio/library/write-cv.ptbk.md',
3578
4816
  ],
@@ -3616,22 +4854,6 @@
3616
4854
  },
3617
4855
  };
3618
4856
 
3619
- /**
3620
- * Returns the same value that is passed as argument.
3621
- * No side effects.
3622
- *
3623
- * Note: It can be usefull for leveling indentation
3624
- *
3625
- * @param value any values
3626
- * @returns the same values
3627
- */
3628
- function just(value) {
3629
- if (value === undefined) {
3630
- return undefined;
3631
- }
3632
- return value;
3633
- }
3634
-
3635
4857
  /**
3636
4858
  * Parses the action command
3637
4859
  *
@@ -3654,7 +4876,7 @@
3654
4876
  /**
3655
4877
  * Link to discussion
3656
4878
  */
3657
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
4879
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
3658
4880
  /**
3659
4881
  * Example usages of the ACTION command
3660
4882
  */
@@ -3663,7 +4885,8 @@
3663
4885
  * Parses the ACTION command
3664
4886
  */
3665
4887
  parse: function (input) {
3666
- input.args;
4888
+ var args = input.args;
4889
+ TODO_USE(args);
3667
4890
  return {
3668
4891
  type: 'ACTION',
3669
4892
  };
@@ -3692,7 +4915,7 @@
3692
4915
  /**
3693
4916
  * Link to discussion
3694
4917
  */
3695
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
4918
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
3696
4919
  /**
3697
4920
  * Example usages of the INSTRUMENT command
3698
4921
  */
@@ -3701,7 +4924,8 @@
3701
4924
  * Parses the INSTRUMENT command
3702
4925
  */
3703
4926
  parse: function (input) {
3704
- input.args;
4927
+ var args = input.args;
4928
+ TODO_USE(args);
3705
4929
  return {
3706
4930
  type: 'INSTRUMENT',
3707
4931
  };
@@ -3734,7 +4958,7 @@
3734
4958
  /**
3735
4959
  * Link to discussion
3736
4960
  */
3737
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
4961
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
3738
4962
  /**
3739
4963
  * Example usages of the BOILERPLATE command
3740
4964
  */
@@ -3759,6 +4983,7 @@
3759
4983
  };
3760
4984
  /**
3761
4985
  * TODO: [💐] Implement BOILERPLATE command into `pipelineStringToJsonSync` function
4986
+ * Note: [⚪] This should never be in any released package
3762
4987
  */
3763
4988
 
3764
4989
  /**
@@ -3777,7 +5002,7 @@
3777
5002
  actionCommandParser,
3778
5003
  instrumentCommandParser,
3779
5004
  personaCommandParser,
3780
- boilerplateCommandParser, // <- TODO: !!!! Only in development, remove in production
5005
+ boilerplateCommandParser, // <- TODO: !! Only in development, remove in production
3781
5006
  ];
3782
5007
 
3783
5008
  /**
@@ -3824,10 +5049,6 @@
3824
5049
  .split(' ')
3825
5050
  .map(function (part) { return part.trim(); })
3826
5051
  .filter(function (item) { return item !== ''; })
3827
- // Note: [📇]:
3828
- .filter(function (item) { return !/^PTBK$/i.test(item); })
3829
- .filter(function (item) { return !/^PIPELINE$/i.test(item); })
3830
- .filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
3831
5052
  .map(removeMarkdownFormatting)
3832
5053
  .map(function (item) { return item.trim(); });
3833
5054
  if (items.length === 0 || items[0] === '') {
@@ -3863,18 +5084,18 @@
3863
5084
  }));
3864
5085
  }
3865
5086
  /**
3866
- * !!!
5087
+ * @@@
3867
5088
  */
3868
5089
  function getSupportedCommandsMessage() {
3869
5090
  return COMMANDS.flatMap(function (_a) {
3870
- var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, discussionUrl = _a.discussionUrl;
5091
+ var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, documentationUrl = _a.documentationUrl;
3871
5092
  return __spreadArray([
3872
- "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(discussionUrl, ")")
5093
+ "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(documentationUrl, ")")
3873
5094
  ], __read((aliasNames || []).map(function (aliasName) { return " - **".concat(aliasName, "** Alias for **").concat(name, "**"); })), false);
3874
5095
  }).join('\n');
3875
5096
  }
3876
5097
  /**
3877
- * !!!
5098
+ * @@@
3878
5099
  */
3879
5100
  function parseCommandVariant(input) {
3880
5101
  var e_1, _a;
@@ -3883,7 +5104,6 @@
3883
5104
  var _loop_1 = function (commandParser) {
3884
5105
  var name_1 = commandParser.name, aliasNames = commandParser.aliasNames, deprecatedNames = commandParser.deprecatedNames, parse = commandParser.parse;
3885
5106
  var names = __spreadArray(__spreadArray([name_1], __read((aliasNames || [])), false), __read((deprecatedNames || [])), false);
3886
- // console.log('!!!', { commandName, names });
3887
5107
  if (names.includes(commandName)) {
3888
5108
  try {
3889
5109
  return { value: parse({ usagePlace: usagePlace, raw: raw, rawArgs: rawArgs, normalized: normalized, args: args }) };
@@ -3993,25 +5213,42 @@
3993
5213
  var e_1, _a;
3994
5214
  var codeBlocks = [];
3995
5215
  var lines = markdown.split('\n');
5216
+ // Note: [0] Ensure that the last block notated by gt > will be closed
5217
+ lines.push('');
3996
5218
  var currentCodeBlock = null;
3997
5219
  try {
3998
5220
  for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
3999
5221
  var line = lines_1_1.value;
5222
+ if (line.startsWith('> ') || line === '>') {
5223
+ if (currentCodeBlock === null) {
5224
+ currentCodeBlock = { blockNotation: '>', language: null, content: '' };
5225
+ } /* not else */
5226
+ if (currentCodeBlock.blockNotation === '>') {
5227
+ if (currentCodeBlock.content !== '') {
5228
+ currentCodeBlock.content += '\n';
5229
+ }
5230
+ currentCodeBlock.content += line.slice(2);
5231
+ }
5232
+ }
5233
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
5234
+ codeBlocks.push(currentCodeBlock);
5235
+ currentCodeBlock = null;
5236
+ }
5237
+ /* not else */
4000
5238
  if (line.startsWith('```')) {
4001
5239
  var language = line.slice(3).trim() || null;
4002
5240
  if (currentCodeBlock === null) {
4003
- currentCodeBlock = { language: language, content: '' };
5241
+ currentCodeBlock = { blockNotation: '```', language: language, content: '' };
4004
5242
  }
4005
5243
  else {
4006
5244
  if (language !== null) {
4007
- // [🌻]
4008
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
5245
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
4009
5246
  }
4010
5247
  codeBlocks.push(currentCodeBlock);
4011
5248
  currentCodeBlock = null;
4012
5249
  }
4013
5250
  }
4014
- else if (currentCodeBlock !== null) {
5251
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
4015
5252
  if (currentCodeBlock.content !== '') {
4016
5253
  currentCodeBlock.content += '\n';
4017
5254
  }
@@ -4027,11 +5264,13 @@
4027
5264
  finally { if (e_1) throw e_1.error; }
4028
5265
  }
4029
5266
  if (currentCodeBlock !== null) {
4030
- // [🌻]
4031
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
5267
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
4032
5268
  }
4033
5269
  return codeBlocks;
4034
5270
  }
5271
+ /**
5272
+ * TODO: Maybe name for `blockNotation` instead of '```' and '>'
5273
+ */
4035
5274
 
4036
5275
  /**
4037
5276
  * Extracts exactly ONE code block from markdown.
@@ -4049,13 +5288,12 @@
4049
5288
  function extractOneBlockFromMarkdown(markdown) {
4050
5289
  var codeBlocks = extractAllBlocksFromMarkdown(markdown);
4051
5290
  if (codeBlocks.length !== 1) {
4052
- // TODO: Report more specific place where the error happened
4053
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
5291
+ throw new ParsingError(spaceTrim__default["default"](function (block) { return "\n There should be exactly 1 code block, found ".concat(codeBlocks.length, " code blocks\n\n ").concat(block(codeBlocks.map(function (block, i) { return "Block ".concat(i + 1, ":\n").concat(block.content); }).join('\n\n\n')), "\n "); }));
4054
5292
  }
4055
5293
  return codeBlocks[0];
4056
5294
  }
4057
5295
  /***
4058
- * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
5296
+ * TODO: [🍓][🌻] Decide of this is internal util, external util OR validator/postprocessor
4059
5297
  */
4060
5298
 
4061
5299
  /**
@@ -4065,13 +5303,13 @@
4065
5303
  var _a, _b;
4066
5304
  var lines = value.split('\n');
4067
5305
  if (!lines[0].startsWith('#')) {
4068
- throw new Error('Markdown section must start with heading');
5306
+ throw new ParsingError('Markdown section must start with heading');
4069
5307
  }
4070
5308
  var title = lines[0].replace(/^#+\s*/, '');
4071
5309
  var level = (_b = (_a = lines[0].match(/^#+/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
4072
5310
  var content = spaceTrim__default["default"](lines.slice(1).join('\n'));
4073
5311
  if (level < 1 || level > 6) {
4074
- throw new Error('Markdown section must have heading level between 1 and 6');
5312
+ throw new ParsingError('Markdown section must have heading level between 1 and 6');
4075
5313
  }
4076
5314
  return { title: title, level: level, content: content };
4077
5315
  }
@@ -4208,203 +5446,14 @@
4208
5446
  }
4209
5447
 
4210
5448
  /**
4211
- * Create difference set of two sets.
4212
- *
4213
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4214
- */
4215
- function difference(a, b, isEqual) {
4216
- var e_1, _a;
4217
- if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
4218
- var diff = new Set();
4219
- var _loop_1 = function (itemA) {
4220
- if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
4221
- diff.add(itemA);
4222
- }
4223
- };
4224
- try {
4225
- for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
4226
- var itemA = _c.value;
4227
- _loop_1(itemA);
4228
- }
4229
- }
4230
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4231
- finally {
4232
- try {
4233
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
4234
- }
4235
- finally { if (e_1) throw e_1.error; }
4236
- }
4237
- return diff;
4238
- }
4239
-
4240
- /**
4241
- * Creates a new set with all elements that are present in either set
4242
- *
4243
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4244
- */
4245
- function union() {
4246
- var e_1, _a, e_2, _b;
4247
- var sets = [];
4248
- for (var _i = 0; _i < arguments.length; _i++) {
4249
- sets[_i] = arguments[_i];
4250
- }
4251
- var union = new Set();
4252
- try {
4253
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
4254
- var set = sets_1_1.value;
4255
- try {
4256
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
4257
- var item = _d.value;
4258
- union.add(item);
4259
- }
4260
- }
4261
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4262
- finally {
4263
- try {
4264
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
4265
- }
4266
- finally { if (e_2) throw e_2.error; }
4267
- }
4268
- }
4269
- }
4270
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4271
- finally {
4272
- try {
4273
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
4274
- }
4275
- finally { if (e_1) throw e_1.error; }
4276
- }
4277
- return union;
4278
- }
4279
-
4280
- /**
4281
- * Parses the template and returns the list of all parameter names
4282
- *
4283
- * @param template the template with parameters in {curly} braces
4284
- * @returns the list of parameter names
4285
- */
4286
- function extractParameters(template) {
4287
- var e_1, _a;
4288
- var matches = template.matchAll(/{\w+}/g);
4289
- var parameterNames = new Set();
4290
- try {
4291
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
4292
- var match = matches_1_1.value;
4293
- var parameterName = match[0].slice(1, -1);
4294
- parameterNames.add(parameterName);
4295
- }
4296
- }
4297
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4298
- finally {
4299
- try {
4300
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
4301
- }
4302
- finally { if (e_1) throw e_1.error; }
4303
- }
4304
- return parameterNames;
4305
- }
4306
-
4307
- /**
4308
- * Parses the given script and returns the list of all used variables that are not defined in the script
4309
- *
4310
- * @param script from which to extract the variables
4311
- * @returns the list of variable names
4312
- * @throws {ParsingError} if the script is invalid
4313
- */
4314
- function extractVariables(script) {
4315
- var variables = new Set();
4316
- script = "(()=>{".concat(script, "})()");
4317
- try {
4318
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
4319
- try {
4320
- eval(script);
4321
- }
4322
- catch (error) {
4323
- if (!(error instanceof ReferenceError)) {
4324
- throw error;
4325
- }
4326
- var undefinedName = error.message.split(' ')[0];
4327
- /*
4328
- Note: Parsing the error
4329
- [ReferenceError: thing is not defined]
4330
- */
4331
- if (!undefinedName) {
4332
- throw error;
4333
- }
4334
- if (script.includes(undefinedName + '(')) {
4335
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
4336
- }
4337
- else {
4338
- variables.add(undefinedName);
4339
- script = "const ".concat(undefinedName, " = '';") + script;
4340
- }
4341
- }
4342
- }
4343
- catch (error) {
4344
- if (!(error instanceof Error)) {
4345
- throw error;
4346
- }
4347
- throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
4348
- }
4349
- return variables;
4350
- }
4351
- /**
4352
- * TODO: [🔣] Support for multiple languages - python, java,...
4353
- */
4354
-
4355
- /**
4356
- * Parses the prompt template and returns the set of all used parameters
4357
- *
4358
- * @param promptTemplate the template with used parameters
4359
- * @returns the set of parameter names
4360
- * @throws {ParsingError} if the script is invalid
4361
- */
4362
- function extractParametersFromPromptTemplate(promptTemplate) {
4363
- var e_1, _a, e_2, _b;
4364
- var parameterNames = new Set();
4365
- try {
4366
- for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
4367
- var parameterName = _d.value;
4368
- parameterNames.add(parameterName);
4369
- }
4370
- }
4371
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4372
- finally {
4373
- try {
4374
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
4375
- }
4376
- finally { if (e_1) throw e_1.error; }
4377
- }
4378
- if (promptTemplate.blockType === 'SCRIPT') {
4379
- try {
4380
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
4381
- var parameterName = _f.value;
4382
- parameterNames.add(parameterName);
4383
- }
4384
- }
4385
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4386
- finally {
4387
- try {
4388
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
4389
- }
4390
- finally { if (e_2) throw e_2.error; }
4391
- }
4392
- }
4393
- return parameterNames;
4394
- }
4395
- /**
4396
- * TODO: [🔣] If script require contentLanguage
4397
- */
4398
-
4399
- /**
4400
- * Compile promptbook from string (markdown) format to JSON format synchronously
5449
+ * Compile pipeline from string (markdown) format to JSON format synchronously
4401
5450
  *
4402
- * Note: There are two similar functions:
5451
+ * Note: There are 3 similar functions:
4403
5452
  * - `pipelineStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
4404
5453
  * - `pipelineStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
5454
+ * - `preparePipeline` - just one step in the compilation process
4405
5455
  *
4406
5456
  * @param pipelineString {Promptbook} in string markdown format (.ptbk.md)
4407
- * @param options - Options and tools for the compilation
4408
5457
  * @returns {Promptbook} compiled in JSON format (.ptbk.json)
4409
5458
  * @throws {ParsingError} if the promptbook string is not valid
4410
5459
  *
@@ -4420,7 +5469,10 @@
4420
5469
  description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
4421
5470
  parameters: [],
4422
5471
  promptTemplates: [],
4423
- knowledge: [],
5472
+ knowledgeSources: [],
5473
+ knowledgePieces: [],
5474
+ personas: [],
5475
+ preparations: [],
4424
5476
  };
4425
5477
  // =============================================================
4426
5478
  // Note: 1️⃣ Parsing of the markdown into object
@@ -4450,7 +5502,7 @@
4450
5502
  existingParameter.description &&
4451
5503
  existingParameter.description !== parameterDescription &&
4452
5504
  parameterDescription) {
4453
- throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
5505
+ throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description:\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
4454
5506
  }
4455
5507
  if (existingParameter) {
4456
5508
  if (parameterDescription) {
@@ -4469,11 +5521,12 @@
4469
5521
  // =============================================================
4470
5522
  // Note: 3️⃣ Process pipeline head
4471
5523
  pipelineJson.title = pipelineHead.title;
4472
- // TODO: [1] DRY description
5524
+ // TODO: [🎾][1] DRY description
4473
5525
  var description = pipelineHead.content;
4474
- // Note: Remove codeblocks - TODO: Maybe put this into util (exported from `@promptbool/utils`)
5526
+ // Note: Remove codeblocks - TODO: [🎾] Make util removeAllBlocksFromMarkdown (exported from `@promptbool/utils`)
4475
5527
  description = description.split(/^```.*^```/gms).join('');
4476
- //Note: Remove lists and return statement - TODO: Maybe put this into util (exported from `@promptbool/utils`)
5528
+ description = description.split(/^>.*$/gm).join('');
5529
+ //Note: Remove lists and return statement - TODO: [🎾] Make util (exported from `@promptbool/utils`)
4477
5530
  description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4478
5531
  description = spaceTrim.spaceTrim(description);
4479
5532
  if (description === '') {
@@ -4501,7 +5554,7 @@
4501
5554
  pipelineJson.pipelineUrl = command.pipelineUrl.href;
4502
5555
  break;
4503
5556
  case 'KNOWLEDGE':
4504
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5557
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4505
5558
  break;
4506
5559
  case 'ACTION':
4507
5560
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
@@ -4510,7 +5563,8 @@
4510
5563
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4511
5564
  break;
4512
5565
  case 'PERSONA':
4513
- console.error(new NotYetImplementedError('Personas are not implemented yet'));
5566
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
5567
+ // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4514
5568
  break;
4515
5569
  case 'BOILERPLATE':
4516
5570
  throw new ParsingError('BOILERPLATE command is only for testing purposes and should not be used in the .ptbk.md file'); // <- TODO: [🚞]
@@ -4533,29 +5587,83 @@
4533
5587
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
4534
5588
  var templateModelRequirements = __assign({}, defaultModelRequirements);
4535
5589
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
4536
- var dependentParameterNames = new Set();
4537
- var blockType = 'PROMPT_TEMPLATE';
4538
- var jokers = [];
4539
- var postprocessing = [];
4540
- var expectAmount = {};
4541
- var expectFormat = undefined;
4542
- var isBlockTypeChanged = false;
5590
+ var lastLine = section.content.split('\n').pop();
5591
+ var resultingParameterNameMatch = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
5592
+ var resultingParameterName = null;
5593
+ if (resultingParameterNameMatch &&
5594
+ resultingParameterNameMatch.groups !== undefined &&
5595
+ resultingParameterNameMatch.groups.resultingParamName !== undefined) {
5596
+ resultingParameterName = resultingParameterNameMatch.groups.resultingParamName;
5597
+ }
5598
+ var expectResultingParameterName = function () {
5599
+ if (resultingParameterName !== null) {
5600
+ return resultingParameterName;
5601
+ }
5602
+ throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Template section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
5603
+ // TODO: Show code of invalid sections each time + DRY
5604
+ section.content
5605
+ .split('\n')
5606
+ .map(function (line) { return " | ".concat(line); } /* <- TODO: [🚞] */)
5607
+ .join('\n')), "\n "); }));
5608
+ };
5609
+ var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
5610
+ // TODO: [🎾][1] DRY description
5611
+ var description_1 = section.content;
5612
+ // Note: Remove codeblocks - TODO: [🎾]
5613
+ description_1 = description_1.split(/^```.*^```/gms).join('');
5614
+ description_1 = description_1.split(/^>.*$/gm).join('');
5615
+ //Note: Remove lists and return statement - TODO: [🎾]
5616
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5617
+ description_1 = spaceTrim.spaceTrim(description_1);
5618
+ if (description_1 === '') {
5619
+ description_1 = undefined;
5620
+ }
5621
+ var templateJson = {
5622
+ blockType: 'PROMPT_TEMPLATE',
5623
+ name: titleToName(section.title),
5624
+ title: section.title,
5625
+ description: description_1,
5626
+ modelRequirements: templateModelRequirements,
5627
+ content: content,
5628
+ };
5629
+ /**
5630
+ * This is nessesary because block type can be
5631
+ * - Set zero times, so anticipate 'PROMPT_TEMPLATE'
5632
+ * - Set one time
5633
+ * - Set more times - throw error
5634
+ *
5635
+ * Note: [2]
5636
+ */
5637
+ var isBlockTypeSet = false;
4543
5638
  try {
4544
5639
  for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
4545
5640
  var listItem = listItems_2_1.value;
4546
5641
  var command = parseCommand(listItem, 'PIPELINE_TEMPLATE');
5642
+ // TODO [🍧][♓️] List commands and before apply order them
4547
5643
  switch (command.type) {
4548
5644
  // TODO: [🍧] Use here applyToPipelineJson and remove switch statement
4549
5645
  case 'BLOCK':
4550
- if (isBlockTypeChanged) {
5646
+ if (isBlockTypeSet) {
4551
5647
  throw new ParsingError('Block type is already defined in the prompt template. It can be defined only once.');
4552
5648
  }
4553
5649
  if (command.blockType === 'SAMPLE') {
4554
- console.error(new NotYetImplementedError('Block type SAMPLE is not implemented yet'));
5650
+ expectResultingParameterName();
5651
+ var parameter = pipelineJson.parameters.find(function (param) { return param.name === resultingParameterName; });
5652
+ if (parameter === undefined) {
5653
+ throw new UnexpectedError("Can not find parameter {".concat(resultingParameterName, "} to assign sample value"));
5654
+ }
5655
+ parameter.sampleValues = parameter.sampleValues || [];
5656
+ parameter.sampleValues.push(content);
4555
5657
  return "continue-templates";
4556
5658
  }
4557
5659
  if (command.blockType === 'KNOWLEDGE') {
4558
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5660
+ knowledgeCommandParser.applyToPipelineJson({
5661
+ type: 'KNOWLEDGE',
5662
+ source: content, // <- TODO: [🐝] !!! Work with KNOWLEDGE which not referring to the source file or website, but its content itself
5663
+ }, {
5664
+ pipelineJson: pipelineJson,
5665
+ templateJson: templateJson,
5666
+ });
4559
5667
  return "continue-templates";
4560
5668
  }
4561
5669
  if (command.blockType === 'ACTION') {
@@ -4566,35 +5674,37 @@
4566
5674
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4567
5675
  return "continue-templates";
4568
5676
  }
4569
- blockType = command.blockType;
4570
- isBlockTypeChanged = true;
5677
+ expectResultingParameterName();
5678
+ templateJson.blockType = command.blockType;
5679
+ isBlockTypeSet = true; //<- Note: [2]
4571
5680
  break;
4572
5681
  case 'EXPECT_AMOUNT':
4573
5682
  // eslint-disable-next-line no-case-declarations
4574
5683
  var unit = command.unit.toLowerCase();
4575
- expectAmount[unit] = expectAmount[unit] || {};
5684
+ templateJson.expectations = templateJson.expectations || {};
5685
+ templateJson.expectations[unit] = templateJson.expectations[unit] || {};
4576
5686
  if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
4577
- if (expectAmount[unit].min !== undefined) {
4578
- throw new ParsingError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5687
+ if (templateJson.expectations[unit].min !== undefined) {
5688
+ throw new ParsingError("Already defined minumum ".concat(templateJson.expectations[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4579
5689
  }
4580
- expectAmount[unit].min = command.amount;
5690
+ templateJson.expectations[unit].min = command.amount;
4581
5691
  } /* not else */
4582
5692
  if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
4583
- if (expectAmount[unit].max !== undefined) {
4584
- throw new ParsingError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5693
+ if (templateJson.expectations[unit].max !== undefined) {
5694
+ throw new ParsingError("Already defined maximum ".concat(templateJson.expectations[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4585
5695
  }
4586
- expectAmount[unit].max = command.amount;
5696
+ templateJson.expectations[unit].max = command.amount;
4587
5697
  }
4588
5698
  break;
4589
5699
  case 'EXPECT_FORMAT':
4590
- if (expectFormat !== undefined && command.format !== expectFormat) {
4591
- throw new ParsingError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
5700
+ if (templateJson.expectFormat !== undefined && command.format !== templateJson.expectFormat) {
5701
+ throw new ParsingError(spaceTrim.spaceTrim("\n Expect format is already defined to \"".concat(templateJson.expectFormat, "\".\n Now you try to redefine it by \"").concat(command.format, "\".\n ")));
4592
5702
  }
4593
- expectFormat = command.format;
5703
+ templateJson.expectFormat = command.format;
4594
5704
  break;
4595
5705
  case 'JOKER':
4596
- jokers.push(command.parameterName);
4597
- dependentParameterNames.add(command.parameterName);
5706
+ templateJson.jokerParameterNames = templateJson.jokerParameterNames || [];
5707
+ templateJson.jokerParameterNames.push(command.parameterName);
4598
5708
  break;
4599
5709
  case 'MODEL':
4600
5710
  templateModelRequirements[command.key] = command.value;
@@ -4604,19 +5714,24 @@
4604
5714
  defineParam(command);
4605
5715
  break;
4606
5716
  case 'POSTPROCESS':
4607
- postprocessing.push(command.functionName);
5717
+ templateJson.postprocessingFunctionNames = templateJson.postprocessingFunctionNames || [];
5718
+ templateJson.postprocessingFunctionNames.push(command.functionName);
4608
5719
  break;
4609
5720
  case 'KNOWLEDGE':
4610
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5721
+ // TODO: [👙] The knowledge is maybe relevant for just this template
5722
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
4611
5723
  break;
4612
5724
  case 'ACTION':
5725
+ // TODO: [👙] The action is maybe relevant for just this template
4613
5726
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
4614
5727
  break;
4615
5728
  case 'INSTRUMENT':
5729
+ // TODO: [👙] The instrument is maybe relevant for just this template
4616
5730
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4617
5731
  break;
4618
5732
  case 'PERSONA':
4619
- console.error(new NotYetImplementedError('Personas are not implemented yet'));
5733
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
5734
+ // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4620
5735
  break;
4621
5736
  case 'BOILERPLATE':
4622
5737
  console.error(new ParsingError('BOILERPLATE command is only for testing purposes and should not be used in the .ptbk.md file'));
@@ -4634,70 +5749,29 @@
4634
5749
  }
4635
5750
  finally { if (e_3) throw e_3.error; }
4636
5751
  }
4637
- var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
4638
- if (blockType === 'SCRIPT') {
5752
+ // TODO: [🍧] Should be done in BLOCK command
5753
+ if (templateJson.blockType === 'SCRIPT') {
4639
5754
  if (!language) {
4640
5755
  throw new ParsingError('You must specify the language of the script in the prompt template');
4641
5756
  }
4642
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5757
+ if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
4643
5758
  throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
4644
5759
  }
5760
+ templateJson.contentLanguage = language;
4645
5761
  }
4646
- var lastLine = section.content.split('\n').pop();
4647
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
4648
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
4649
- throw new ParsingError(spaceTrim.spaceTrim(function (block) { return "\n Each section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
4650
- // TODO: Show code of invalid sections each time + DRY
4651
- section.content
4652
- .split('\n')
4653
- .map(function (line) { return " | ".concat(line); } /* <- TODO: [🚞] */)
4654
- .join('\n')), "\n "); }));
4655
- }
4656
- var resultingParameterName = match.groups.resultingParamName;
4657
- // TODO: [1] DRY description
4658
- var description_1 = section.content;
4659
- // Note: Remove codeblocks
4660
- description_1 = description_1.split(/^```.*^```/gms).join('');
4661
- //Note: Remove lists and return statement
4662
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4663
- description_1 = spaceTrim.spaceTrim(description_1);
4664
- if (description_1 === '') {
4665
- description_1 = undefined;
4666
- }
4667
- if (Object.keys(jokers).length === 0) {
4668
- jokers = undefined;
4669
- }
4670
- if (Object.keys(expectAmount).length === 0) {
4671
- expectAmount = undefined;
4672
- }
4673
- if (Object.keys(postprocessing).length === 0) {
4674
- postprocessing = undefined;
4675
- }
4676
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, blockType: blockType, content: content })));
5762
+ // TODO: [🍧][❔] Should be done in BLOCK command
4677
5763
  if (templateModelRequirements.modelVariant === undefined) {
4678
5764
  templateModelRequirements.modelVariant = 'CHAT';
4679
5765
  }
4680
- dependentParameterNames = difference(dependentParameterNames, new Set(RESERVED_PARAMETER_NAMES));
4681
- var template = {
4682
- name: titleToName(section.title),
4683
- title: section.title,
4684
- description: description_1,
4685
- dependentParameterNames: Array.from(dependentParameterNames),
4686
- blockType: blockType,
4687
- jokers: jokers,
4688
- postprocessing: postprocessing,
4689
- expectations: expectAmount,
4690
- expectFormat: expectFormat,
4691
- modelRequirements: templateModelRequirements,
4692
- contentLanguage: blockType === 'SCRIPT' ? language : undefined,
4693
- content: content,
4694
- resultingParameterName: resultingParameterName,
4695
- };
4696
- if (blockType !== 'PROMPT_TEMPLATE') {
4697
- delete template.modelRequirements;
5766
+ templateJson.dependentParameterNames = Array.from(extractParameterNamesFromPromptTemplate(templateJson));
5767
+ // TODO: [🍧][❔] Remove this condition - modelRequirements should be put here via BLOCK command not removed when PROMPT_TEMPLATE
5768
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
5769
+ delete templateJson.modelRequirements;
4698
5770
  }
5771
+ // TODO: [🍧] Make this better - for example each command parser can call and apply this
5772
+ templateJson.resultingParameterName = expectResultingParameterName( /* <- Note: This is once more redundant */);
4699
5773
  // TODO: [🍧] What actually about preparation and pushing the block into `promptTemplates`
4700
- pipelineJson.promptTemplates.push(template /* <- !!! */);
5774
+ pipelineJson.promptTemplates.push(templateJson);
4701
5775
  };
4702
5776
  try {
4703
5777
  // =============================================================
@@ -4764,14 +5838,18 @@
4764
5838
  * TODO: Use spaceTrim more effectively
4765
5839
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
4766
5840
  * TODO: [🥞] Not optimal parsing because `splitMarkdownIntoSections` is executed twice with same string, once through `flattenMarkdown` and second directly here
5841
+ * TODO: [♈] Probbably move expectations from templates to parameters
5842
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
5843
+ * TODO: [🍙] Make some standart order of json properties
4767
5844
  */
4768
5845
 
4769
5846
  /**
4770
- * Compile promptbook from string (markdown) format to JSON format
5847
+ * Compile pipeline from string (markdown) format to JSON format
4771
5848
  *
4772
- * Note: There are two similar functions:
5849
+ * Note: There are 3 similar functions:
4773
5850
  * - `pipelineStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
4774
5851
  * - `pipelineStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
5852
+ * - `preparePipeline` - just one step in the compilation process
4775
5853
  *
4776
5854
  * @param pipelineString {Promptbook} in string markdown format (.ptbk.md)
4777
5855
  * @param options - Options and tools for the compilation
@@ -4782,22 +5860,18 @@
4782
5860
  * Note: This function acts as compilation process
4783
5861
  */
4784
5862
  function pipelineStringToJson(pipelineString, options) {
4785
- if (options === void 0) { options = {}; }
5863
+ if (options === void 0) { options = { llmTools: null }; }
4786
5864
  return __awaiter(this, void 0, void 0, function () {
4787
- var llmTools, pipelineJson, knowledge;
5865
+ var llmTools, pipelineJson;
4788
5866
  return __generator(this, function (_a) {
4789
5867
  switch (_a.label) {
4790
5868
  case 0:
4791
5869
  llmTools = options.llmTools;
4792
5870
  pipelineJson = pipelineStringToJsonSync(pipelineString);
4793
- if (!llmTools) return [3 /*break*/, 2];
4794
- return [4 /*yield*/, prepareKnowledgeFromMarkdown({
4795
- content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
4796
- llmTools: llmTools,
4797
- })];
5871
+ if (!(llmTools !== null)) return [3 /*break*/, 2];
5872
+ return [4 /*yield*/, preparePipeline(pipelineJson, { llmTools: llmTools })];
4798
5873
  case 1:
4799
- knowledge = _a.sent();
4800
- pipelineJson = __assign(__assign({}, pipelineJson), { knowledge: __spreadArray(__spreadArray([], __read((pipelineJson.knowledge || [])), false), __read(knowledge), false) });
5874
+ pipelineJson = _a.sent();
4801
5875
  _a.label = 2;
4802
5876
  case 2: return [2 /*return*/, pipelineJson];
4803
5877
  }
@@ -4806,12 +5880,13 @@
4806
5880
  }
4807
5881
  /**
4808
5882
  * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
5883
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
4809
5884
  */
4810
5885
 
4811
5886
  /**
4812
5887
  * Add or modify an auto-generated section in a markdown file
4813
5888
  *
4814
- * @private within the package
5889
+ * @private within the repository
4815
5890
  */
4816
5891
  function addAutoGeneratedSection(content, options) {
4817
5892
  var sectionName = options.sectionName, sectionContent = options.sectionContent;
@@ -4823,7 +5898,10 @@
4823
5898
  }
4824
5899
  var placeForSection = removeContentComments(content).match(/^##.*$/im);
4825
5900
  if (!placeForSection) {
4826
- throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
5901
+ throw new ParsingError(
5902
+ // <- [🧠] Maybe something better than `ParsingError`
5903
+ "No place where to put the section <!--".concat(sectionName, "-->"));
5904
+ // <- [🚞]
4827
5905
  }
4828
5906
  var _a = __read(placeForSection, 1), heading = _a[0];
4829
5907
  return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
@@ -4980,6 +6058,28 @@
4980
6058
  * TODO: [🕌] When more than 2 functionalities, split into separate functions
4981
6059
  */
4982
6060
 
6061
+ /**
6062
+ * Stringify the PipelineJson with proper formatting
6063
+ *
6064
+ * Note: [0] It can be used for more JSON types like whole collection of pipelines, single knowledge piece, etc.
6065
+ * Note: In contrast to JSON.stringify, this function ensures that **embedding index** is on single line
6066
+ */
6067
+ function stringifyPipelineJson(pipeline) {
6068
+ var pipelineJsonStringified = JSON.stringify(pipeline, null, 4);
6069
+ for (var i = 0; i < LOOP_LIMIT; i++) {
6070
+ pipelineJsonStringified = pipelineJsonStringified.replace(/(-?0\.\d+),[\n\s]+(-?0\.\d+)/gms, "$1".concat(REPLACING_NONCE, "$2"));
6071
+ }
6072
+ pipelineJsonStringified = pipelineJsonStringified.split(REPLACING_NONCE).join(', ');
6073
+ pipelineJsonStringified += '\n';
6074
+ return pipelineJsonStringified;
6075
+ }
6076
+ /**
6077
+ * TODO: [🐝] Not Working propperly @see https://promptbook.studio/samples/mixed-knowledge.ptbk.md
6078
+ * TODO: [🧠][0] Maybe rename to `stringifyPipelineJson`, `stringifyIndexedJson`,...
6079
+ * TODO: [🧠] Maybe more elegant solution than replacing via regex
6080
+ * TODO: [🍙] Make some standart order of json properties
6081
+ */
6082
+
4983
6083
  /**
4984
6084
  * This error indicates that the pipeline collection cannot be propperly loaded
4985
6085
  */
@@ -4994,6 +6094,20 @@
4994
6094
  return CollectionError;
4995
6095
  }(Error));
4996
6096
 
6097
+ /**
6098
+ * This error type indicates that the version of the pipeline is not matching the expected version
6099
+ */
6100
+ var VersionMismatchError = /** @class */ (function (_super) {
6101
+ __extends(VersionMismatchError, _super);
6102
+ function VersionMismatchError(message, expectedVersion) {
6103
+ var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n This error indicates that there is error with compatibility\n\n Expected version:\n ").concat(expectedVersion, "\n\n Current version:\n ").concat(PROMPTBOOK_VERSION, "\n\n "); })) || this;
6104
+ _this.name = 'UnexpectedError';
6105
+ Object.setPrototypeOf(_this, VersionMismatchError.prototype);
6106
+ return _this;
6107
+ }
6108
+ return VersionMismatchError;
6109
+ }(Error));
6110
+
4997
6111
  /**
4998
6112
  * Pretty print an embedding vector for logging
4999
6113
  */
@@ -5020,6 +6134,29 @@
5020
6134
  return uncertainNumber;
5021
6135
  }
5022
6136
 
6137
+ /**
6138
+ * Function `usageToHuman` will take usage and convert it to human readable report
6139
+ */
6140
+ function usageToHuman(usage) {
6141
+ var report = 'Usage:';
6142
+ var uncertainNumberToHuman = function (_a) {
6143
+ var value = _a.value, isUncertain = _a.isUncertain;
6144
+ return "".concat(isUncertain ? 'approximately ' : '').concat(Math.round(value * 100) / 100);
6145
+ };
6146
+ report += '\n' + "- Cost ".concat(uncertainNumberToHuman(usage.price), " USD");
6147
+ report += '\n' + "- Saved ".concat(uncertainNumberToHuman(usageToWorktime(usage)), " hours of human time");
6148
+ return spaceTrim__default["default"](report);
6149
+ }
6150
+ /**
6151
+ * TODO: Use "$1" not "1 USD"
6152
+ * TODO: Use markdown formatting like "Cost approximately **$1**"
6153
+ * TODO: Report in minutes, seconds, days NOT 0.1 hours
6154
+ * TODO: [🧠] Maybe make from `uncertainNumberToHuman` separate exported utility
6155
+ * TODO: When negligible usage, report "Negligible" or just don't report it
6156
+ * TODO: [🧠] Maybe use "~" instead of "approximately"
6157
+ * TODO: [🏛] Maybe make some markdown builder
6158
+ */
6159
+
5023
6160
  /**
5024
6161
  * Delagates the user interaction to a async callback function
5025
6162
  * You need to provide your own implementation of this callback function and its bind to UI.
@@ -5081,6 +6218,9 @@
5081
6218
  };
5082
6219
  return SimplePromptInterfaceTools;
5083
6220
  }());
6221
+ /**
6222
+ * Note: [🔵] This code should never be published outside of `@promptbook/browser`
6223
+ */
5084
6224
 
5085
6225
  /**
5086
6226
  * Default options for generating an execution report string
@@ -5093,7 +6233,7 @@
5093
6233
  /**
5094
6234
  * Format either small or big number
5095
6235
  *
5096
- * @private within the package
6236
+ * @private within the repository
5097
6237
  */
5098
6238
  function formatNumber(value) {
5099
6239
  if (value === 0) {
@@ -5113,7 +6253,7 @@
5113
6253
  /**
5114
6254
  * Create a markdown table from a 2D array of strings
5115
6255
  *
5116
- * @private within the package
6256
+ * @private within the repository
5117
6257
  */
5118
6258
  function createMarkdownTable(table) {
5119
6259
  var columnWidths = table.reduce(function (widths, row) {
@@ -5141,7 +6281,7 @@
5141
6281
  /**
5142
6282
  * Function createMarkdownChart will draw a chart in markdown from ⬛+🟦 tiles
5143
6283
  *
5144
- * @private within the package
6284
+ * @private within the repository
5145
6285
  */
5146
6286
  function createMarkdownChart(options) {
5147
6287
  var e_1, _a;
@@ -5194,7 +6334,7 @@
5194
6334
  /**
5195
6335
  * Count the duration of working time
5196
6336
  *
5197
- * @private within the package
6337
+ * @private within the repository
5198
6338
  */
5199
6339
  function countWorkingDuration(items) {
5200
6340
  var e_1, _a;
@@ -5351,10 +6491,13 @@
5351
6491
  if (just(true)) {
5352
6492
  executionReportString +=
5353
6493
  '\n\n\n\n' +
5354
- spaceTrim.spaceTrim(function (block) { return "\n\n ### Prompt\n\n ```\n ".concat(block(escapeMarkdownBlock(promptExecution.prompt.content)), "\n ```\n\n "); });
6494
+ spaceTrim.spaceTrim(function (block) {
6495
+ var _a;
6496
+ return "\n\n ### Prompt\n\n ```\n ".concat(block(escapeMarkdownBlock(((_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.rawPromptContent) || promptExecution.prompt.content)), "\n ```\n\n ");
6497
+ });
5355
6498
  }
5356
6499
  if (promptExecution.result && promptExecution.result.content) {
5357
- executionReportString += '\n\n\n\n' + '### Result';
6500
+ executionReportString += '\n\n\n\n' + '### Result' + '\n\n';
5358
6501
  if (promptExecution.result === undefined) {
5359
6502
  executionReportString += '*No result*';
5360
6503
  }
@@ -5406,6 +6549,7 @@
5406
6549
  exports.ReferenceError = ReferenceError$1;
5407
6550
  exports.SimplePromptInterfaceTools = SimplePromptInterfaceTools;
5408
6551
  exports.UnexpectedError = UnexpectedError;
6552
+ exports.VersionMismatchError = VersionMismatchError;
5409
6553
  exports.addUsage = addUsage;
5410
6554
  exports.assertsExecutionSuccessful = assertsExecutionSuccessful;
5411
6555
  exports.checkExpectations = checkExpectations;
@@ -5423,7 +6567,11 @@
5423
6567
  exports.pipelineStringToJson = pipelineStringToJson;
5424
6568
  exports.pipelineStringToJsonSync = pipelineStringToJsonSync;
5425
6569
  exports.prepareKnowledgeFromMarkdown = prepareKnowledgeFromMarkdown;
6570
+ exports.preparePipeline = preparePipeline;
5426
6571
  exports.prettifyPipelineString = prettifyPipelineString;
6572
+ exports.stringifyPipelineJson = stringifyPipelineJson;
6573
+ exports.unpreparePipeline = unpreparePipeline;
6574
+ exports.usageToHuman = usageToHuman;
5427
6575
  exports.usageToWorktime = usageToWorktime;
5428
6576
  exports.validatePipeline = validatePipeline;
5429
6577