@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/esm/index.es.js CHANGED
@@ -251,7 +251,7 @@ function pipelineJsonToString(pipelineJson) {
251
251
  /* Note: Not using:> name, */
252
252
  title_1 = promptTemplate.title, description_1 = promptTemplate.description,
253
253
  /* Note: dependentParameterNames, */
254
- jokers = promptTemplate.jokers, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
254
+ jokers = promptTemplate.jokerParameterNames, blockType = promptTemplate.blockType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessingFunctionNames, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
255
255
  pipelineString += '\n\n';
256
256
  pipelineString += "## ".concat(title_1);
257
257
  if (description_1) {
@@ -385,23 +385,120 @@ function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
385
385
  return parameterString;
386
386
  }
387
387
  /**
388
- * TODO: !!!!! Implement new features and commands into `promptTemplateParameterJsonToString`
388
+ * TODO: !!!! Implement new features and commands into `promptTemplateParameterJsonToString`
389
389
  * TODO: [🧠] Is there a way to auto-detect missing features in pipelineJsonToString
390
- * TODO: Escape all
390
+ * TODO: [🏛] Maybe make some markdown builder
391
+ * TODO: [🏛] Escape all
391
392
  */
392
393
 
394
+ /**
395
+ * @@@
396
+ *
397
+ * @returns The same object as the input, but deeply frozen
398
+ *
399
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
400
+ */
401
+ function deepFreeze(objectValue) {
402
+ var e_1, _a;
403
+ var propertyNames = Object.getOwnPropertyNames(objectValue);
404
+ try {
405
+ for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
406
+ var propertyName = propertyNames_1_1.value;
407
+ var value = objectValue[propertyName];
408
+ if (value && typeof value === 'object') {
409
+ deepFreeze(value);
410
+ }
411
+ }
412
+ }
413
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
414
+ finally {
415
+ try {
416
+ if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
417
+ }
418
+ finally { if (e_1) throw e_1.error; }
419
+ }
420
+ return Object.freeze(objectValue);
421
+ }
422
+ /**
423
+ * @@@
424
+ * @@@
425
+ *
426
+ * @returns The same object as the input, but deeply frozen
427
+ *
428
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
429
+ */
430
+ function deepFreezeWithSameType(objectValue) {
431
+ return deepFreeze(objectValue);
432
+ }
433
+ /**
434
+ * TODO: [🔼] Export from `@promptbook/utils`
435
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
436
+ */
437
+
438
+ /**
439
+ * Returns the same value that is passed as argument.
440
+ * No side effects.
441
+ *
442
+ * Note: It can be usefull for:
443
+ *
444
+ * 1) Leveling indentation
445
+ * 2) Putting always-true or always-false conditions without getting eslint errors
446
+ *
447
+ * @param value any values
448
+ * @returns the same values
449
+ * @private within the repository
450
+ */
451
+ function just(value) {
452
+ if (value === undefined) {
453
+ return undefined;
454
+ }
455
+ return value;
456
+ }
457
+
393
458
  /**
394
459
  * The maximum number of iterations for a loops
395
460
  */
396
461
  var LOOP_LIMIT = 1000;
462
+ /**
463
+ * The maximum number of (LLM) tasks running in parallel
464
+ */
465
+ var MAX_PARALLEL_COUNT = 5;
466
+ /**
467
+ * The maximum number of attempts to execute LLM task before giving up
468
+ */
469
+ var MAX_EXECUTION_ATTEMPTS = 3;
470
+ /**
471
+ * Nonce which is used for replacing things in strings
472
+ */
473
+ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
397
474
  /**
398
475
  * The names of the parameters that are reserved for special purposes
399
476
  */
400
- var RESERVED_PARAMETER_NAMES = [
477
+ var RESERVED_PARAMETER_NAMES = deepFreeze([
478
+ 'content',
401
479
  'context',
480
+ 'knowledge',
481
+ 'samples',
482
+ 'modelName',
483
+ 'currentDate',
402
484
  // <- TODO: Add more like 'date', 'modelName',...
403
485
  // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
404
- ];
486
+ ]);
487
+ /**
488
+ * @@@
489
+ */
490
+ var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
491
+ /**
492
+ * @@@
493
+ */
494
+ var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
495
+ /*
496
+ TODO: !!! Just testing false-negative detection of [🟡][🟢][🔵][⚪] leak
497
+ */
498
+ // [🟡][🟢][🔵][⚪]
499
+ /**
500
+ * TODO: [🔼] Export all to core
501
+ */
405
502
 
406
503
  /**
407
504
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -479,7 +576,7 @@ function isValidPromptbookVersion(version) {
479
576
  if ( /* version === '1.0.0' || */version === '2.0.0' || version === '3.0.0') {
480
577
  return false;
481
578
  }
482
- // <- TODO: !!!! Check isValidPromptbookVersion against PROMPTBOOK_VERSIONS
579
+ // <- TODO: !!! Check isValidPromptbookVersion against PROMPTBOOK_VERSIONS
483
580
  return true;
484
581
  }
485
582
 
@@ -604,56 +701,67 @@ function isValidPipelineUrl(url) {
604
701
  */
605
702
  function validatePipeline(pipeline) {
606
703
  // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
607
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
704
+ var e_1, _a, e_2, _b, e_3, _c;
705
+ var pipelineIdentification = (function () {
706
+ // Note: This is a 😐 implementation of [🚞]
707
+ var _ = [];
708
+ if (pipeline.sourceFile !== undefined) {
709
+ _.push("File: ".concat(pipeline.sourceFile));
710
+ }
711
+ if (pipeline.pipelineUrl !== undefined) {
712
+ _.push("Url: ".concat(pipeline.pipelineUrl));
713
+ }
714
+ return _.join('\n');
715
+ })();
608
716
  if (pipeline.pipelineUrl !== undefined && !isValidPipelineUrl(pipeline.pipelineUrl)) {
609
717
  // <- Note: [🚲]
610
- throw new PipelineLogicError("Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\""));
718
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
611
719
  }
612
720
  if (!isValidPromptbookVersion(pipeline.promptbookVersion)) {
613
721
  // <- Note: [🚲]
614
- throw new PipelineLogicError("Invalid promptbook Version \"".concat(pipeline.pipelineUrl, "\""));
722
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Invalid Promptbook Version \"".concat(pipeline.promptbookVersion, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
615
723
  }
616
724
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
617
725
  if (!Array.isArray(pipeline.parameters)) {
618
726
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
619
- throw new ParsingError(spaceTrim$1("\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 ")));
727
+ throw new ParsingError(spaceTrim$1(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 "); }));
620
728
  }
621
729
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
622
730
  if (!Array.isArray(pipeline.promptTemplates)) {
623
731
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
624
- throw new ParsingError(spaceTrim$1("\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 ")));
732
+ throw new ParsingError(spaceTrim$1(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 "); }));
625
733
  }
626
734
  var _loop_1 = function (parameter) {
627
735
  if (parameter.isInput && parameter.isOutput) {
628
- throw new PipelineLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
736
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n\n Parameter {".concat(parameter.name, "} can not be both input and output\n\n ").concat(block(pipelineIdentification), "\n "); }));
629
737
  }
630
738
  // Note: Testing that parameter is either intermediate or output BUT not created and unused
631
739
  if (!parameter.isInput &&
632
740
  !parameter.isOutput &&
633
741
  !pipeline.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
634
- throw new PipelineLogicError(spaceTrim$1("\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 ")));
742
+ throw new PipelineLogicError(spaceTrim$1(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 "); }));
635
743
  }
636
744
  // Note: Testing that parameter is either input or result of some template
637
745
  if (!parameter.isInput &&
638
746
  !pipeline.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
639
- throw new PipelineLogicError(spaceTrim$1("\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 ")));
747
+ throw new PipelineLogicError(spaceTrim$1(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 "); }));
640
748
  }
641
749
  };
642
750
  try {
643
751
  // Note: Check each parameter individually
644
- for (var _e = __values(pipeline.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
645
- var parameter = _f.value;
752
+ for (var _d = __values(pipeline.parameters), _e = _d.next(); !_e.done; _e = _d.next()) {
753
+ var parameter = _e.value;
646
754
  _loop_1(parameter);
647
755
  }
648
756
  }
649
757
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
650
758
  finally {
651
759
  try {
652
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
760
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
653
761
  }
654
762
  finally { if (e_1) throw e_1.error; }
655
763
  }
656
- // Note: Check each template individually
764
+ // Note: All input parameters are defined - so that they can be used as result of some template
657
765
  var definedParameters = new Set(pipeline.parameters.filter(function (_a) {
658
766
  var isInput = _a.isInput;
659
767
  return isInput;
@@ -661,66 +769,80 @@ function validatePipeline(pipeline) {
661
769
  var name = _a.name;
662
770
  return name;
663
771
  }));
664
- try {
665
- for (var _g = __values(pipeline.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
666
- var template = _h.value;
667
- if (definedParameters.has(template.resultingParameterName)) {
668
- throw new PipelineLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
772
+ var _loop_2 = function (template) {
773
+ var e_4, _h, e_5, _j;
774
+ if (definedParameters.has(template.resultingParameterName)) {
775
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter {".concat(template.resultingParameterName, "} is defined multiple times\n\n ").concat(block(pipelineIdentification), "\n "); }));
776
+ }
777
+ if (RESERVED_PARAMETER_NAMES.includes(template.resultingParameterName)) {
778
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter name {".concat(template.resultingParameterName, "} is reserved, please use different name\n\n ").concat(block(pipelineIdentification), "\n "); }));
779
+ }
780
+ definedParameters.add(template.resultingParameterName);
781
+ if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
782
+ throw new PipelineLogicError(spaceTrim$1(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 "); }));
783
+ }
784
+ if (template.jokerParameterNames && template.jokerParameterNames.length > 0) {
785
+ if (!template.expectFormat &&
786
+ !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
787
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
669
788
  }
670
- definedParameters.add(template.resultingParameterName);
671
- if (template.blockType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
672
- throw new PipelineLogicError(spaceTrim$1("\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 ")));
673
- }
674
- if (template.jokers && template.jokers.length > 0) {
675
- if (!template.expectFormat &&
676
- !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
677
- throw new PipelineLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
789
+ var _loop_4 = function (joker) {
790
+ if (!template.dependentParameterNames.includes(joker)) {
791
+ throw new PipelineLogicError(spaceTrim$1(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 "); }));
792
+ }
793
+ };
794
+ try {
795
+ for (var _k = (e_4 = void 0, __values(template.jokerParameterNames)), _l = _k.next(); !_l.done; _l = _k.next()) {
796
+ var joker = _l.value;
797
+ _loop_4(joker);
678
798
  }
799
+ }
800
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
801
+ finally {
679
802
  try {
680
- for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
681
- var joker = _k.value;
682
- if (!template.dependentParameterNames.includes(joker)) {
683
- throw new PipelineLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
684
- }
685
- }
803
+ if (_l && !_l.done && (_h = _k.return)) _h.call(_k);
686
804
  }
687
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
688
- finally {
689
- try {
690
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
691
- }
692
- finally { if (e_3) throw e_3.error; }
805
+ finally { if (e_4) throw e_4.error; }
806
+ }
807
+ }
808
+ if (template.expectations) {
809
+ var _loop_5 = function (unit, min, max) {
810
+ if (min !== undefined && max !== undefined && min > max) {
811
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")\n\n ").concat(block(pipelineIdentification), "\n "); }));
812
+ }
813
+ if (min !== undefined && min < 0) {
814
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Min expectation of ".concat(unit, " must be zero or positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
815
+ }
816
+ if (max !== undefined && max <= 0) {
817
+ throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Max expectation of ".concat(unit, " must be positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
818
+ }
819
+ };
820
+ try {
821
+ for (var _m = (e_5 = void 0, __values(Object.entries(template.expectations))), _o = _m.next(); !_o.done; _o = _m.next()) {
822
+ var _p = __read(_o.value, 2), unit = _p[0], _q = _p[1], min = _q.min, max = _q.max;
823
+ _loop_5(unit, min, max);
693
824
  }
694
825
  }
695
- if (template.expectations) {
826
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
827
+ finally {
696
828
  try {
697
- for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
698
- var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
699
- if (min !== undefined && max !== undefined && min > max) {
700
- throw new PipelineLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
701
- }
702
- if (min !== undefined && min < 0) {
703
- throw new PipelineLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
704
- }
705
- if (max !== undefined && max <= 0) {
706
- throw new PipelineLogicError("Max expectation of ".concat(unit, " must be positive"));
707
- }
708
- }
709
- }
710
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
711
- finally {
712
- try {
713
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
714
- }
715
- finally { if (e_4) throw e_4.error; }
829
+ if (_o && !_o.done && (_j = _m.return)) _j.call(_m);
716
830
  }
831
+ finally { if (e_5) throw e_5.error; }
717
832
  }
718
833
  }
834
+ };
835
+ try {
836
+ // Note: Checking each template individually
837
+ for (var _f = __values(pipeline.promptTemplates), _g = _f.next(); !_g.done; _g = _f.next()) {
838
+ var template = _g.value;
839
+ _loop_2(template);
840
+ }
719
841
  }
720
842
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
721
843
  finally {
722
844
  try {
723
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
845
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
724
846
  }
725
847
  finally { if (e_2) throw e_2.error; }
726
848
  }
@@ -734,24 +856,42 @@ function validatePipeline(pipeline) {
734
856
  var name = _a.name;
735
857
  return name;
736
858
  });
859
+ try {
860
+ // Note: All reserved parameters are resolved
861
+ 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()) {
862
+ var reservedParameterName = RESERVED_PARAMETER_NAMES_1_1.value;
863
+ resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), [reservedParameterName], false);
864
+ }
865
+ }
866
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
867
+ finally {
868
+ try {
869
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_c = RESERVED_PARAMETER_NAMES_1.return)) _c.call(RESERVED_PARAMETER_NAMES_1);
870
+ }
871
+ finally { if (e_3) throw e_3.error; }
872
+ }
737
873
  var unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
874
+ // <- TODO: [🧠][🥜]
738
875
  var loopLimit = LOOP_LIMIT;
739
- var _loop_2 = function () {
876
+ var _loop_3 = function () {
740
877
  if (loopLimit-- < 0) {
741
- throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePipeline`');
878
+ // Note: Really UnexpectedError not LimitReachedError - this should not happen and be caught below
879
+ throw new UnexpectedError(spaceTrim$1(function (block) { return "\n Loop limit reached during detection of circular dependencies in `validatePipeline`\n\n ".concat(block(pipelineIdentification), "\n "); }));
742
880
  }
743
881
  var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
744
882
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
745
883
  });
746
884
  if (currentlyResovedTemplates.length === 0) {
747
- throw new PipelineLogicError(spaceTrim$1(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
885
+ throw new PipelineLogicError(
886
+ // TODO: [🐎] DRY
887
+ spaceTrim$1(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
748
888
  .map(function (_a) {
749
889
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
750
- return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
890
+ return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
751
891
  .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
752
- .join(', '));
892
+ .join(' and '));
753
893
  })
754
- .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
894
+ .join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- Parameter {".concat(name, "}"); }).join('\n')), "\n\n ").concat(block(pipelineIdentification), "\n "); }));
755
895
  }
756
896
  resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
757
897
  var resultingParameterName = _a.resultingParameterName;
@@ -760,7 +900,7 @@ function validatePipeline(pipeline) {
760
900
  unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
761
901
  };
762
902
  while (unresovedTemplates.length > 0) {
763
- _loop_2();
903
+ _loop_3();
764
904
  }
765
905
  return pipeline;
766
906
  }
@@ -773,7 +913,16 @@ function validatePipeline(pipeline) {
773
913
  * > * It checks:
774
914
  * > * - it has a valid structure
775
915
  * > * - ...
776
- * > ex port function validatePipeline(promptbook: unknown): asserts promptbook is PipelineJson {
916
+ * > ex port function validatePipeline(promptbook: really_unknown): asserts promptbook is PipelineJson {
917
+ */
918
+ /**
919
+ * TODO: [🐣] !!!! Validate that all samples match expectations
920
+ * TODO: [🐣][🐝] !!!! Validate that knowledge is valid (non-void)
921
+ * TODO: [🐣] !!!! Validate that persona can be used only with CHAT variant
922
+ * TODO: [🐣] !!!! Validate that parameter with reserved name not used RESERVED_PARAMETER_NAMES
923
+ * TODO: [🐣] !!!! Validate that reserved parameter is not used as joker
924
+ * TODO: [🧠] Validation not only logic itself but imports around - files and websites and rerefenced pipelines exists
925
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
777
926
  */
778
927
 
779
928
  /**
@@ -805,79 +954,137 @@ var ReferenceError$1 = /** @class */ (function (_super) {
805
954
  }(Error));
806
955
 
807
956
  /**
808
- * Library of promptbooks that groups together promptbooks for an application.
809
- * This implementation is a very thin wrapper around the Array / Map of promptbooks.
957
+ * Parses the template and returns the list of all parameter names
958
+ *
959
+ * @param template the template with parameters in {curly} braces
960
+ * @returns the list of parameter names
961
+ */
962
+ function extractParameterNames(template) {
963
+ var e_1, _a;
964
+ var matches = template.matchAll(/{\w+}/g);
965
+ var parameterNames = new Set();
966
+ try {
967
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
968
+ var match = matches_1_1.value;
969
+ var parameterName = match[0].slice(1, -1);
970
+ parameterNames.add(parameterName);
971
+ }
972
+ }
973
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
974
+ finally {
975
+ try {
976
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
977
+ }
978
+ finally { if (e_1) throw e_1.error; }
979
+ }
980
+ return parameterNames;
981
+ }
982
+
983
+ /**
984
+ * Unprepare just strips the preparation data of the pipeline
985
+ */
986
+ function unpreparePipeline(pipeline) {
987
+ var personas = pipeline.personas, knowledgeSources = pipeline.knowledgeSources, promptTemplates = pipeline.promptTemplates;
988
+ personas = personas.map(function (persona) { return (__assign(__assign({}, persona), { modelRequirements: undefined, preparationIds: undefined })); });
989
+ knowledgeSources = knowledgeSources.map(function (knowledgeSource) { return (__assign(__assign({}, knowledgeSource), { preparationIds: undefined })); });
990
+ promptTemplates = promptTemplates.map(function (promptTemplate) {
991
+ var dependentParameterNames = promptTemplate.dependentParameterNames;
992
+ var parameterNames = extractParameterNames(promptTemplate.preparedContent || '');
993
+ dependentParameterNames = dependentParameterNames.filter(function (dependentParameterName) { return !parameterNames.has(dependentParameterName); });
994
+ var promptTemplateUnprepared = __assign(__assign({}, promptTemplate), { dependentParameterNames: dependentParameterNames });
995
+ delete promptTemplateUnprepared.preparedContent;
996
+ return promptTemplateUnprepared;
997
+ });
998
+ return __assign(__assign({}, pipeline), { promptTemplates: promptTemplates, knowledgeSources: knowledgeSources, knowledgePieces: [], personas: personas, preparations: [] });
999
+ }
1000
+ /**
1001
+ * TODO: [🔼] !!! Export via `@promptbook/core`
1002
+ * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
1003
+ * TODO: Write tests for `preparePipeline`
1004
+ * TODO: [🍙] Make some standart order of json properties
1005
+ */
1006
+
1007
+ /**
1008
+ * Library of pipelines that groups together pipelines for an application.
1009
+ * This implementation is a very thin wrapper around the Array / Map of pipelines.
810
1010
  *
811
1011
  * @private use `createCollectionFromJson` instead
812
- * @see https://github.com/webgptorg/promptbook#promptbook-collection
1012
+ * @see https://github.com/webgptorg/pipeline#pipeline-collection
813
1013
  */
814
1014
  var SimplePipelineCollection = /** @class */ (function () {
815
1015
  /**
816
- * Constructs a pipeline collection from promptbooks
1016
+ * Constructs a pipeline collection from pipelines
817
1017
  *
818
- * @param promptbooks !!!
1018
+ * @param pipelines @@@
819
1019
  *
820
1020
  * @private Use instead `createCollectionFromJson`
821
- * Note: During the construction logic of all promptbooks are validated
1021
+ * Note: During the construction logic of all pipelines are validated
822
1022
  * Note: It is not recommended to use this constructor directly, use `createCollectionFromJson` *(or other variant)* instead
823
1023
  */
824
1024
  function SimplePipelineCollection() {
825
1025
  var e_1, _a;
826
- var promptbooks = [];
1026
+ var pipelines = [];
827
1027
  for (var _i = 0; _i < arguments.length; _i++) {
828
- promptbooks[_i] = arguments[_i];
1028
+ pipelines[_i] = arguments[_i];
829
1029
  }
830
1030
  this.collection = new Map();
831
1031
  try {
832
- for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
833
- var promptbook = promptbooks_1_1.value;
834
- if (promptbook.pipelineUrl === undefined) {
835
- throw new ReferenceError$1(spaceTrim$1("\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 ")));
1032
+ for (var pipelines_1 = __values(pipelines), pipelines_1_1 = pipelines_1.next(); !pipelines_1_1.done; pipelines_1_1 = pipelines_1.next()) {
1033
+ var pipeline = pipelines_1_1.value;
1034
+ // TODO: [👠] DRY
1035
+ if (pipeline.pipelineUrl === undefined) {
1036
+ throw new ReferenceError$1(spaceTrim$1("\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 ")));
836
1037
  }
837
- validatePipeline(promptbook);
1038
+ // Note: [🐨]
1039
+ validatePipeline(pipeline);
1040
+ // TODO: [🦄] DRY
838
1041
  // Note: [🦄]
839
- if (this.collection.has(promptbook.pipelineUrl) &&
840
- pipelineJsonToString(promptbook) !== pipelineJsonToString(this.collection.get(promptbook.pipelineUrl))) {
841
- var existing = this.collection.get(promptbook.pipelineUrl);
842
- throw new ReferenceError$1(spaceTrim$1("\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 ")));
1042
+ if (
1043
+ // TODO: [🐽]
1044
+ this.collection.has(pipeline.pipelineUrl) &&
1045
+ pipelineJsonToString(unpreparePipeline(pipeline)) !==
1046
+ pipelineJsonToString(unpreparePipeline(this.collection.get(pipeline.pipelineUrl)))) {
1047
+ var existing = this.collection.get(pipeline.pipelineUrl);
1048
+ throw new ReferenceError$1(spaceTrim$1("\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 ")));
843
1049
  }
844
- this.collection.set(promptbook.pipelineUrl, promptbook);
1050
+ // Note: [🧠] Overwrite existing pipeline with the same URL
1051
+ this.collection.set(pipeline.pipelineUrl, pipeline);
845
1052
  }
846
1053
  }
847
1054
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
848
1055
  finally {
849
1056
  try {
850
- if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
1057
+ if (pipelines_1_1 && !pipelines_1_1.done && (_a = pipelines_1.return)) _a.call(pipelines_1);
851
1058
  }
852
1059
  finally { if (e_1) throw e_1.error; }
853
1060
  }
854
1061
  }
855
1062
  /**
856
- * Gets all promptbooks in the collection
1063
+ * Gets all pipelines in the collection
857
1064
  */
858
1065
  SimplePipelineCollection.prototype.listPipelines = function () {
859
1066
  return Array.from(this.collection.keys());
860
1067
  };
861
1068
  /**
862
- * Gets promptbook by its URL
1069
+ * Gets pipeline by its URL
863
1070
  *
864
1071
  * Note: This is not a direct fetching from the URL, but a lookup in the collection
865
1072
  */
866
1073
  SimplePipelineCollection.prototype.getPipelineByUrl = function (url) {
867
1074
  var _this = this;
868
- var promptbook = this.collection.get(url);
869
- if (!promptbook) {
1075
+ var pipeline = this.collection.get(url);
1076
+ if (!pipeline) {
870
1077
  if (this.listPipelines().length === 0) {
871
- throw new NotFoundError(spaceTrim$1("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
1078
+ throw new NotFoundError(spaceTrim$1("\n Pipeline with url \"".concat(url, "\" not found\n\n No pipelines available\n ")));
872
1079
  }
873
- throw new NotFoundError(spaceTrim$1(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPipelines()
1080
+ throw new NotFoundError(spaceTrim$1(function (block) { return "\n Pipeline with url \"".concat(url, "\" not found\n\n Available pipelines:\n ").concat(block(_this.listPipelines()
874
1081
  .map(function (pipelineUrl) { return "- ".concat(pipelineUrl); })
875
1082
  .join('\n')), "\n\n "); }));
876
1083
  }
877
- return promptbook;
1084
+ return pipeline;
878
1085
  };
879
1086
  /**
880
- * Checks whether given prompt was defined in any promptbook in the collection
1087
+ * Checks whether given prompt was defined in any pipeline in the collection
881
1088
  */
882
1089
  SimplePipelineCollection.prototype.isResponsibleForPrompt = function (prompt) {
883
1090
  return true;
@@ -1119,7 +1326,199 @@ var BlockTypes = [
1119
1326
  // <- [🩻]
1120
1327
  ];
1121
1328
 
1122
- 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"}];
1329
+ /**
1330
+ * @@@
1331
+ */
1332
+ function deepClone(objectValue) {
1333
+ return JSON.parse(JSON.stringify(objectValue));
1334
+ /*
1335
+ TODO: [🧠] Is there a better implementation?
1336
+ > const propertyNames = Object.getOwnPropertyNames(objectValue);
1337
+ > for (const propertyName of propertyNames) {
1338
+ > const value = (objectValue as really_any)[propertyName];
1339
+ > if (value && typeof value === 'object') {
1340
+ > deepClone(value);
1341
+ > }
1342
+ > }
1343
+ > return Object.assign({}, objectValue);
1344
+ */
1345
+ }
1346
+ /**
1347
+ * TODO: [🔼] Export from `@promptbook/utils`
1348
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
1349
+ */
1350
+
1351
+ /**
1352
+ * @@@
1353
+ *
1354
+ * TODO: [🔼] Export with addUsage
1355
+ */
1356
+ var ZERO_USAGE = deepFreeze({
1357
+ price: { value: 0 },
1358
+ input: {
1359
+ tokensCount: { value: 0 },
1360
+ charactersCount: { value: 0 },
1361
+ wordsCount: { value: 0 },
1362
+ sentencesCount: { value: 0 },
1363
+ linesCount: { value: 0 },
1364
+ paragraphsCount: { value: 0 },
1365
+ pagesCount: { value: 0 },
1366
+ },
1367
+ output: {
1368
+ tokensCount: { value: 0 },
1369
+ charactersCount: { value: 0 },
1370
+ wordsCount: { value: 0 },
1371
+ sentencesCount: { value: 0 },
1372
+ linesCount: { value: 0 },
1373
+ paragraphsCount: { value: 0 },
1374
+ pagesCount: { value: 0 },
1375
+ },
1376
+ });
1377
+ /**
1378
+ * Function `addUsage` will add multiple usages into one
1379
+ *
1380
+ * Note: If you provide 0 values, it returns ZERO_USAGE
1381
+ */
1382
+ function addUsage() {
1383
+ var usageItems = [];
1384
+ for (var _i = 0; _i < arguments.length; _i++) {
1385
+ usageItems[_i] = arguments[_i];
1386
+ }
1387
+ return usageItems.reduce(function (acc, item) {
1388
+ var e_1, _a, e_2, _b;
1389
+ var _c;
1390
+ acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
1391
+ try {
1392
+ for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
1393
+ var key = _e.value;
1394
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1395
+ //@ts-ignore
1396
+ if (item.input[key]) {
1397
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1398
+ //@ts-ignore
1399
+ acc.input[key].value += item.input[key].value || 0;
1400
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1401
+ //@ts-ignore
1402
+ if (item.input[key].isUncertain) {
1403
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1404
+ //@ts-ignore
1405
+ acc.input[key].isUncertain = true;
1406
+ }
1407
+ }
1408
+ }
1409
+ }
1410
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1411
+ finally {
1412
+ try {
1413
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1414
+ }
1415
+ finally { if (e_1) throw e_1.error; }
1416
+ }
1417
+ try {
1418
+ for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
1419
+ var key = _g.value;
1420
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1421
+ //@ts-ignore
1422
+ if (item.output[key]) {
1423
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1424
+ //@ts-ignore
1425
+ acc.output[key].value += item.output[key].value || 0;
1426
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1427
+ //@ts-ignore
1428
+ if (item.output[key].isUncertain) {
1429
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1430
+ //@ts-ignore
1431
+ acc.output[key].isUncertain = true;
1432
+ }
1433
+ }
1434
+ }
1435
+ }
1436
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1437
+ finally {
1438
+ try {
1439
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1440
+ }
1441
+ finally { if (e_2) throw e_2.error; }
1442
+ }
1443
+ return acc;
1444
+ }, deepClone(ZERO_USAGE));
1445
+ }
1446
+
1447
+ /**
1448
+ * Async version of Array.forEach
1449
+ *
1450
+ * @param array - Array to iterate over
1451
+ * @param options - Options for the function
1452
+ * @param callbackfunction - Function to call for each item
1453
+ */
1454
+ function forEachAsync(array, options, callbackfunction) {
1455
+ return __awaiter(this, void 0, void 0, function () {
1456
+ var _a, maxParallelCount, index, runningTasks, tasks, _loop_1, _b, _c, item, e_1_1;
1457
+ var e_1, _d;
1458
+ return __generator(this, function (_e) {
1459
+ switch (_e.label) {
1460
+ case 0:
1461
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? Infinity : _a;
1462
+ index = 0;
1463
+ runningTasks = [];
1464
+ tasks = [];
1465
+ _loop_1 = function (item) {
1466
+ var currentIndex, task;
1467
+ return __generator(this, function (_f) {
1468
+ switch (_f.label) {
1469
+ case 0:
1470
+ currentIndex = index++;
1471
+ task = callbackfunction(item, currentIndex, array);
1472
+ tasks.push(task);
1473
+ runningTasks.push(task);
1474
+ /* not await */ Promise.resolve(task).then(function () {
1475
+ runningTasks = runningTasks.filter(function (t) { return t !== task; });
1476
+ });
1477
+ if (!(maxParallelCount < runningTasks.length)) return [3 /*break*/, 2];
1478
+ return [4 /*yield*/, Promise.race(runningTasks)];
1479
+ case 1:
1480
+ _f.sent();
1481
+ _f.label = 2;
1482
+ case 2: return [2 /*return*/];
1483
+ }
1484
+ });
1485
+ };
1486
+ _e.label = 1;
1487
+ case 1:
1488
+ _e.trys.push([1, 6, 7, 8]);
1489
+ _b = __values(array), _c = _b.next();
1490
+ _e.label = 2;
1491
+ case 2:
1492
+ if (!!_c.done) return [3 /*break*/, 5];
1493
+ item = _c.value;
1494
+ return [5 /*yield**/, _loop_1(item)];
1495
+ case 3:
1496
+ _e.sent();
1497
+ _e.label = 4;
1498
+ case 4:
1499
+ _c = _b.next();
1500
+ return [3 /*break*/, 2];
1501
+ case 5: return [3 /*break*/, 8];
1502
+ case 6:
1503
+ e_1_1 = _e.sent();
1504
+ e_1 = { error: e_1_1 };
1505
+ return [3 /*break*/, 8];
1506
+ case 7:
1507
+ try {
1508
+ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
1509
+ }
1510
+ finally { if (e_1) throw e_1.error; }
1511
+ return [7 /*endfinally*/];
1512
+ case 8: return [4 /*yield*/, Promise.all(tasks)];
1513
+ case 9:
1514
+ _e.sent();
1515
+ return [2 /*return*/];
1516
+ }
1517
+ });
1518
+ });
1519
+ }
1520
+
1521
+ 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"}];
1123
1522
 
1124
1523
  var defaultDiacriticsRemovalMap = [
1125
1524
  {
@@ -1446,6 +1845,14 @@ function removeEmojis(text) {
1446
1845
  * Function normalizes title to name which can be used as identifier
1447
1846
  */
1448
1847
  function titleToName(value) {
1848
+ if (value.startsWith('http://') || value.startsWith('https://')) {
1849
+ // TODO: Maybe check against some list unallowed characters
1850
+ return value;
1851
+ }
1852
+ if (value.startsWith('./') || value.startsWith('../')) {
1853
+ // TODO: Maybe check against some list unallowed characters
1854
+ return value;
1855
+ }
1449
1856
  value = removeEmojis(value);
1450
1857
  value = normalizeToKebabCase(value);
1451
1858
  // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
@@ -1484,7 +1891,11 @@ function assertsExecutionSuccessful(executionResult) {
1484
1891
  throw errors[0];
1485
1892
  }
1486
1893
  else {
1487
- throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
1894
+ throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors
1895
+ .map(function (error, index) {
1896
+ return spaceTrim$1(function (block) { return "\n Error ".concat(index + 1, ":\n ").concat(block(error.stack || error.message), "\n "); });
1897
+ })
1898
+ .join('\n')), "\n "); }));
1488
1899
  }
1489
1900
  }
1490
1901
  /**
@@ -1492,26 +1903,134 @@ function assertsExecutionSuccessful(executionResult) {
1492
1903
  */
1493
1904
 
1494
1905
  /**
1495
- * This error occurs when some expectation is not met in the execution of the pipeline
1906
+ * Parses the given script and returns the list of all used variables that are not defined in the script
1496
1907
  *
1497
- * @private Always catched and rethrown as `PipelineExecutionError`
1498
- * Note: This is a kindof subtype of PipelineExecutionError
1908
+ * @param script from which to extract the variables
1909
+ * @returns the list of variable names
1910
+ * @throws {ParsingError} if the script is invalid
1499
1911
  */
1500
- var ExpectError = /** @class */ (function (_super) {
1501
- __extends(ExpectError, _super);
1502
- function ExpectError(message) {
1503
- var _this = _super.call(this, message) || this;
1504
- _this.name = 'ExpectError';
1505
- Object.setPrototypeOf(_this, ExpectError.prototype);
1506
- return _this;
1507
- }
1508
- return ExpectError;
1509
- }(Error));
1912
+ function extractVariables(script) {
1913
+ var variables = new Set();
1914
+ script = "(()=>{".concat(script, "})()");
1915
+ try {
1916
+ for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
1917
+ try {
1918
+ eval(script);
1919
+ }
1920
+ catch (error) {
1921
+ if (!(error instanceof ReferenceError)) {
1922
+ throw error;
1923
+ }
1924
+ var undefinedName = error.message.split(' ')[0];
1925
+ /*
1926
+ Note: Parsing the error
1927
+ [ReferenceError: thing is not defined]
1928
+ */
1929
+ if (!undefinedName) {
1930
+ throw error;
1931
+ }
1932
+ if (script.includes(undefinedName + '(')) {
1933
+ script = "const ".concat(undefinedName, " = ()=>'';") + script;
1934
+ }
1935
+ else {
1936
+ variables.add(undefinedName);
1937
+ script = "const ".concat(undefinedName, " = '';") + script;
1938
+ }
1939
+ }
1940
+ }
1941
+ catch (error) {
1942
+ if (!(error instanceof Error)) {
1943
+ throw error;
1944
+ }
1945
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
1946
+ }
1947
+ return variables;
1948
+ }
1949
+ /**
1950
+ * TODO: [🔣] Support for multiple languages - python, java,...
1951
+ */
1952
+
1953
+ /**
1954
+ * Parses the prompt template and returns the set of all used parameters
1955
+ *
1956
+ * @param promptTemplate the template with used parameters
1957
+ * @returns the set of parameter names
1958
+ * @throws {ParsingError} if the script is invalid
1959
+ */
1960
+ function extractParameterNamesFromPromptTemplate(promptTemplate) {
1961
+ var e_1, _a, e_2, _b, e_3, _c;
1962
+ var title = promptTemplate.title, description = promptTemplate.description, blockType = promptTemplate.blockType, content = promptTemplate.content, preparedContent = promptTemplate.preparedContent, jokerParameterNames = promptTemplate.jokerParameterNames;
1963
+ var parameterNames = new Set();
1964
+ try {
1965
+ 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()) {
1966
+ var parameterName = _e.value;
1967
+ parameterNames.add(parameterName);
1968
+ }
1969
+ }
1970
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1971
+ finally {
1972
+ try {
1973
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1974
+ }
1975
+ finally { if (e_1) throw e_1.error; }
1976
+ }
1977
+ if (blockType === 'SCRIPT') {
1978
+ try {
1979
+ for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
1980
+ var parameterName = _g.value;
1981
+ parameterNames.add(parameterName);
1982
+ }
1983
+ }
1984
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1985
+ finally {
1986
+ try {
1987
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1988
+ }
1989
+ finally { if (e_2) throw e_2.error; }
1990
+ }
1991
+ }
1992
+ try {
1993
+ for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
1994
+ var jokerName = _j.value;
1995
+ parameterNames.add(jokerName);
1996
+ }
1997
+ }
1998
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1999
+ finally {
2000
+ try {
2001
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
2002
+ }
2003
+ finally { if (e_3) throw e_3.error; }
2004
+ }
2005
+ parameterNames.delete('content');
2006
+ // <- Note {websiteContent} is used in `preparedContent`
2007
+ return parameterNames;
2008
+ }
2009
+ /**
2010
+ * TODO: [🔣] If script require contentLanguage
2011
+ */
2012
+
2013
+ /**
2014
+ * This error occurs when some expectation is not met in the execution of the pipeline
2015
+ *
2016
+ * @private Always catched and rethrown as `PipelineExecutionError`
2017
+ * Note: This is a kindof subtype of PipelineExecutionError
2018
+ */
2019
+ var ExpectError = /** @class */ (function (_super) {
2020
+ __extends(ExpectError, _super);
2021
+ function ExpectError(message) {
2022
+ var _this = _super.call(this, message) || this;
2023
+ _this.name = 'ExpectError';
2024
+ Object.setPrototypeOf(_this, ExpectError.prototype);
2025
+ return _this;
2026
+ }
2027
+ return ExpectError;
2028
+ }(Error));
1510
2029
 
1511
2030
  /**
1512
2031
  * Function isValidJsonString will tell you if the string is valid JSON or not
1513
2032
  */
1514
- function isValidJsonString(value /* <-[👨‍⚖️] */) {
2033
+ function isValidJsonString(value /* <- [👨‍⚖️] */) {
1515
2034
  try {
1516
2035
  JSON.parse(value);
1517
2036
  return true;
@@ -1654,10 +2173,17 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
1654
2173
  throw errors[0];
1655
2174
  }
1656
2175
  else if (errors.length > 1) {
1657
- throw new PipelineExecutionError(spaceTrim(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 "); }));
2176
+ throw new PipelineExecutionError(
2177
+ // TODO: Tell which execution tools failed like
2178
+ // 1) OpenAI throw PipelineExecutionError: Parameter {knowledge} is not defined
2179
+ // 2) AnthropicClaude throw PipelineExecutionError: Parameter {knowledge} is not defined
2180
+ // 3) ...
2181
+ spaceTrim(function (block) { return "\n All execution tools failed:\n\n ".concat(block(errors
2182
+ .map(function (error, i) { return "".concat(i + 1, ") **").concat(error.name || 'Error', ":** ").concat(error.message); })
2183
+ .join('\n')), "\n\n "); }));
1658
2184
  }
1659
2185
  else {
1660
- throw new PipelineExecutionError(spaceTrim(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
2186
+ throw new PipelineExecutionError(spaceTrim(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
1661
2187
  .map(function (tools) { return "- ".concat(tools.title, " ").concat(tools.description || ''); })
1662
2188
  .join('\n')), "\n\n "); }));
1663
2189
  }
@@ -1667,7 +2193,7 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
1667
2193
  };
1668
2194
  /**
1669
2195
  * List all available models that can be used
1670
- * This liost is a combination of all available models from all execution tools
2196
+ * This lists is a combination of all available models from all execution tools
1671
2197
  */
1672
2198
  MultipleLlmExecutionTools.prototype.listModels = function () {
1673
2199
  return __awaiter(this, void 0, void 0, function () {
@@ -1713,6 +2239,8 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
1713
2239
  }());
1714
2240
  /**
1715
2241
  * TODO: [🧠][🎛] Aggregating multiple models - have result not only from one first aviable model BUT all of them
2242
+ * 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
2243
+ * Look how `countTotalUsage` (and `cacheLlmTools`) implements it
1716
2244
  */
1717
2245
 
1718
2246
  /**
@@ -1734,7 +2262,7 @@ function joinLlmExecutionTools() {
1734
2262
  llmExecutionTools[_i] = arguments[_i];
1735
2263
  }
1736
2264
  if (llmExecutionTools.length === 0) {
1737
- var warningMessage = spaceTrim("\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 ");
2265
+ var warningMessage = spaceTrim("\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 ");
1738
2266
  // TODO: [🟥] Detect browser / node and make it colorfull
1739
2267
  console.warn(warningMessage);
1740
2268
  /*
@@ -1760,6 +2288,39 @@ function joinLlmExecutionTools() {
1760
2288
  }
1761
2289
  return new (MultipleLlmExecutionTools.bind.apply(MultipleLlmExecutionTools, __spreadArray([void 0], __read(llmExecutionTools), false)))();
1762
2290
  }
2291
+ /**
2292
+ * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
2293
+ */
2294
+
2295
+ /**
2296
+ * Determine if the pipeline is fully prepared
2297
+ */
2298
+ function isPipelinePrepared(pipeline) {
2299
+ // Note: Ignoring `pipeline.preparations` @@@
2300
+ // Note: Ignoring `pipeline.knowledgePieces` @@@
2301
+ if (!pipeline.personas.every(function (persona) { return persona.modelRequirements !== undefined; })) {
2302
+ return false;
2303
+ }
2304
+ if (!pipeline.knowledgeSources.every(function (knowledgeSource) { return knowledgeSource.preparationIds !== undefined; })) {
2305
+ return false;
2306
+ }
2307
+ /*
2308
+ TODO: [🧠][🍫] `promptTemplates` can not be determined if they are fully prepared SO ignoring them
2309
+ > if (!pipeline.promptTemplates.every(({ preparedContent }) => preparedContent === undefined)) {
2310
+ > return false;
2311
+ > }
2312
+ */
2313
+ return true;
2314
+ }
2315
+ /**
2316
+ * TODO: [🐠] Maybe base this on `makeValidator`
2317
+ * TODO: [🔼] Export via core or utils
2318
+ * TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
2319
+ * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
2320
+ * - [🏍] ? Is context in each template
2321
+ * - [♨] Are samples prepared
2322
+ * - [♨] Are templates prepared
2323
+ */
1763
2324
 
1764
2325
  /**
1765
2326
  * Takes an item or an array of items and returns an array of items
@@ -1781,106 +2342,197 @@ function arrayableToArray(input) {
1781
2342
  }
1782
2343
 
1783
2344
  /**
1784
- * The version of the Promptbook library
2345
+ * Just marks a place of place where should be something implemented
2346
+ * No side effects.
2347
+ *
2348
+ * Note: It can be usefull suppressing eslint errors of unused variables
2349
+ *
2350
+ * @param value any values
2351
+ * @returns void
2352
+ * @private within the repository
1785
2353
  */
1786
- var PROMPTBOOK_VERSION = '0.61.0-8';
1787
- // TODO: !!!! List here all the versions and annotate + put into script
2354
+ function TODO_USE() {
2355
+ var value = [];
2356
+ for (var _i = 0; _i < arguments.length; _i++) {
2357
+ value[_i] = arguments[_i];
2358
+ }
2359
+ }
1788
2360
 
1789
2361
  /**
1790
- * Function `addUsage` will add multiple usages into one
1791
- *
1792
- * Note: If you provide 0 values, it returns void usage
2362
+ * This error type indicates that some limit was reached
1793
2363
  */
1794
- function addUsage() {
1795
- var usageItems = [];
1796
- for (var _i = 0; _i < arguments.length; _i++) {
1797
- usageItems[_i] = arguments[_i];
2364
+ var LimitReachedError = /** @class */ (function (_super) {
2365
+ __extends(LimitReachedError, _super);
2366
+ function LimitReachedError(message) {
2367
+ var _this = _super.call(this, message) || this;
2368
+ _this.name = 'LimitReachedError';
2369
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
2370
+ return _this;
1798
2371
  }
1799
- var initialStructure = {
1800
- price: { value: 0 },
1801
- input: {
1802
- tokensCount: { value: 0 },
1803
- charactersCount: { value: 0 },
1804
- wordsCount: { value: 0 },
1805
- sentencesCount: { value: 0 },
1806
- linesCount: { value: 0 },
1807
- paragraphsCount: { value: 0 },
1808
- pagesCount: { value: 0 },
1809
- },
1810
- output: {
1811
- tokensCount: { value: 0 },
1812
- charactersCount: { value: 0 },
1813
- wordsCount: { value: 0 },
1814
- sentencesCount: { value: 0 },
1815
- linesCount: { value: 0 },
1816
- paragraphsCount: { value: 0 },
1817
- pagesCount: { value: 0 },
1818
- },
1819
- };
1820
- return usageItems.reduce(function (acc, item) {
1821
- var e_1, _a, e_2, _b;
1822
- var _c;
1823
- acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
1824
- try {
1825
- for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
1826
- var key = _e.value;
1827
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1828
- //@ts-ignore
1829
- if (item.input[key]) {
1830
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1831
- //@ts-ignore
1832
- acc.input[key].value += item.input[key].value || 0;
1833
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1834
- //@ts-ignore
1835
- if (item.input[key].isUncertain) {
1836
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1837
- //@ts-ignore
1838
- acc.input[key].isUncertain = true;
1839
- }
1840
- }
2372
+ return LimitReachedError;
2373
+ }(Error));
2374
+
2375
+ /**
2376
+ * Replaces parameters in template with values from parameters object
2377
+ *
2378
+ * @param template the template with parameters in {curly} braces
2379
+ * @param parameters the object with parameters
2380
+ * @returns the template with replaced parameters
2381
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2382
+ */
2383
+ function replaceParameters(template, parameters) {
2384
+ var e_1, _a;
2385
+ try {
2386
+ for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
2387
+ var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
2388
+ if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
2389
+ throw new UnexpectedError("Parameter {".concat(parameterName, "} has missing value"));
1841
2390
  }
1842
- }
1843
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
1844
- finally {
1845
- try {
1846
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
2391
+ else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
2392
+ // TODO: [🍵]
2393
+ throw new UnexpectedError("Parameter {".concat(parameterName, "} is restricted to use"));
1847
2394
  }
1848
- finally { if (e_1) throw e_1.error; }
1849
2395
  }
2396
+ }
2397
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2398
+ finally {
1850
2399
  try {
1851
- for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
1852
- var key = _g.value;
1853
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1854
- //@ts-ignore
1855
- if (item.output[key]) {
1856
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1857
- //@ts-ignore
1858
- acc.output[key].value += item.output[key].value || 0;
1859
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1860
- //@ts-ignore
1861
- if (item.output[key].isUncertain) {
1862
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1863
- //@ts-ignore
1864
- acc.output[key].isUncertain = true;
1865
- }
1866
- }
1867
- }
2400
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1868
2401
  }
1869
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1870
- finally {
1871
- try {
1872
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1873
- }
1874
- finally { if (e_2) throw e_2.error; }
2402
+ finally { if (e_1) throw e_1.error; }
2403
+ }
2404
+ var replacedTemplate = template;
2405
+ var match;
2406
+ var loopLimit = LOOP_LIMIT;
2407
+ var _loop_1 = function () {
2408
+ if (loopLimit-- < 0) {
2409
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
1875
2410
  }
1876
- return acc;
1877
- }, initialStructure);
2411
+ var precol = match.groups.precol;
2412
+ var parameterName = match.groups.parameterName;
2413
+ if (parameterName === '') {
2414
+ return "continue";
2415
+ }
2416
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2417
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
2418
+ }
2419
+ if (parameters[parameterName] === undefined) {
2420
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2421
+ }
2422
+ var parameterValue = parameters[parameterName];
2423
+ if (parameterValue === undefined) {
2424
+ throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2425
+ }
2426
+ parameterValue = parameterValue.toString();
2427
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2428
+ parameterValue = parameterValue
2429
+ .split('\n')
2430
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2431
+ .join('\n');
2432
+ }
2433
+ replacedTemplate =
2434
+ replacedTemplate.substring(0, match.index + precol.length) +
2435
+ parameterValue +
2436
+ replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2437
+ };
2438
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2439
+ .exec(replacedTemplate))) {
2440
+ _loop_1();
2441
+ }
2442
+ // [💫] Check if there are parameters that are not closed properly
2443
+ if (/{\w+$/.test(replacedTemplate)) {
2444
+ throw new PipelineExecutionError('Parameter is not closed');
2445
+ }
2446
+ // [💫] Check if there are parameters that are not opened properly
2447
+ if (/^\w+}/.test(replacedTemplate)) {
2448
+ throw new PipelineExecutionError('Parameter is not opened');
2449
+ }
2450
+ return replacedTemplate;
1878
2451
  }
1879
2452
 
1880
2453
  /**
1881
- * Counts number of characters in the text
1882
- */
1883
- function countCharacters(text) {
2454
+ * Create difference set of two sets.
2455
+ *
2456
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
2457
+ */
2458
+ function difference(a, b, isEqual) {
2459
+ var e_1, _a;
2460
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
2461
+ var diff = new Set();
2462
+ var _loop_1 = function (itemA) {
2463
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
2464
+ diff.add(itemA);
2465
+ }
2466
+ };
2467
+ try {
2468
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
2469
+ var itemA = _c.value;
2470
+ _loop_1(itemA);
2471
+ }
2472
+ }
2473
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2474
+ finally {
2475
+ try {
2476
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2477
+ }
2478
+ finally { if (e_1) throw e_1.error; }
2479
+ }
2480
+ return diff;
2481
+ }
2482
+ /**
2483
+ * TODO: [🧠][💯] Maybe also implement symmetricDifference
2484
+ */
2485
+
2486
+ /**
2487
+ * Creates a new set with all elements that are present in either set
2488
+ *
2489
+ * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
2490
+ */
2491
+ function union() {
2492
+ var e_1, _a, e_2, _b;
2493
+ var sets = [];
2494
+ for (var _i = 0; _i < arguments.length; _i++) {
2495
+ sets[_i] = arguments[_i];
2496
+ }
2497
+ var union = new Set();
2498
+ try {
2499
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
2500
+ var set = sets_1_1.value;
2501
+ try {
2502
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
2503
+ var item = _d.value;
2504
+ union.add(item);
2505
+ }
2506
+ }
2507
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2508
+ finally {
2509
+ try {
2510
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
2511
+ }
2512
+ finally { if (e_2) throw e_2.error; }
2513
+ }
2514
+ }
2515
+ }
2516
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2517
+ finally {
2518
+ try {
2519
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
2520
+ }
2521
+ finally { if (e_1) throw e_1.error; }
2522
+ }
2523
+ return union;
2524
+ }
2525
+
2526
+ /**
2527
+ * The version of the Promptbook library
2528
+ */
2529
+ var PROMPTBOOK_VERSION = '0.61.0';
2530
+ // TODO: !!!! List here all the versions and annotate + put into script
2531
+
2532
+ /**
2533
+ * Counts number of characters in the text
2534
+ */
2535
+ function countCharacters(text) {
1884
2536
  // Remove null characters
1885
2537
  text = text.replace(/\0/g, '');
1886
2538
  // Replace emojis (and also ZWJ sequence) with hyphens
@@ -2008,66 +2660,6 @@ function isPassingExpectations(expectations, value) {
2008
2660
  * TODO: [💝] Unite object for expecting amount and format
2009
2661
  */
2010
2662
 
2011
- /**
2012
- * Replaces parameters in template with values from parameters object
2013
- *
2014
- * @param template the template with parameters in {curly} braces
2015
- * @param parameters the object with parameters
2016
- * @returns the template with replaced parameters
2017
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
2018
- *
2019
- * @private within the createPipelineExecutor
2020
- */
2021
- function replaceParameters(template, parameters) {
2022
- var replacedTemplate = template;
2023
- var match;
2024
- var loopLimit = LOOP_LIMIT;
2025
- var _loop_1 = function () {
2026
- if (loopLimit-- < 0) {
2027
- throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
2028
- }
2029
- var precol = match.groups.precol;
2030
- var parameterName = match.groups.parameterName;
2031
- if (parameterName === '') {
2032
- return "continue";
2033
- }
2034
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
2035
- throw new PipelineExecutionError('Parameter is already opened or not closed');
2036
- }
2037
- if (parameters[parameterName] === undefined) {
2038
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2039
- }
2040
- var parameterValue = parameters[parameterName];
2041
- if (parameterValue === undefined) {
2042
- throw new PipelineExecutionError("Parameter {".concat(parameterName, "} is not defined"));
2043
- }
2044
- parameterValue = parameterValue.toString();
2045
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2046
- parameterValue = parameterValue
2047
- .split('\n')
2048
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
2049
- .join('\n');
2050
- }
2051
- replacedTemplate =
2052
- replacedTemplate.substring(0, match.index + precol.length) +
2053
- parameterValue +
2054
- replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
2055
- };
2056
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
2057
- .exec(replacedTemplate))) {
2058
- _loop_1();
2059
- }
2060
- // [💫] Check if there are parameters that are not closed properly
2061
- if (/{\w+$/.test(replacedTemplate)) {
2062
- throw new PipelineExecutionError('Parameter is not closed');
2063
- }
2064
- // [💫] Check if there are parameters that are not opened properly
2065
- if (/^\w+}/.test(replacedTemplate)) {
2066
- throw new PipelineExecutionError('Parameter is not opened');
2067
- }
2068
- return replacedTemplate;
2069
- }
2070
-
2071
2663
  /**
2072
2664
  * Creates executor function from pipeline and execution tools.
2073
2665
  *
@@ -2077,24 +2669,107 @@ function replaceParameters(template, parameters) {
2077
2669
  function createPipelineExecutor(options) {
2078
2670
  var _this = this;
2079
2671
  var pipeline = options.pipeline, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
2080
- var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
2081
- // TODO: !!!!! Implement new commands
2672
+ 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;
2082
2673
  validatePipeline(pipeline);
2083
- // TODO: !!!!! Do here materialization of still unmaterialized or dynamic knowledge
2084
- // TODO: !!!!! Do here personaToModelRequirements
2085
2674
  var llmTools = joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(arrayableToArray(tools.llm)), false));
2675
+ var preparedPipeline;
2676
+ if (isPipelinePrepared(pipeline)) {
2677
+ preparedPipeline = pipeline;
2678
+ }
2679
+ else if (isNotPreparedWarningSupressed !== true) {
2680
+ console.warn(spaceTrim$1("\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 ")));
2681
+ }
2086
2682
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2683
+ // TODO: !!! Extract to separate functions and files - ALL FUNCTIONS BELOW
2684
+ function getContextForTemplate(// <- TODO: [🧠][🥜]
2685
+ template) {
2686
+ return __awaiter(this, void 0, void 0, function () {
2687
+ return __generator(this, function (_a) {
2688
+ TODO_USE(template);
2689
+ return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [🏍] Implement */];
2690
+ });
2691
+ });
2692
+ }
2693
+ function getKnowledgeForTemplate(// <- TODO: [🧠][🥜]
2694
+ template) {
2695
+ return __awaiter(this, void 0, void 0, function () {
2696
+ return __generator(this, function (_a) {
2697
+ // TODO: [♨] Implement Better - use real index and keyword search from `template` and {samples}
2698
+ TODO_USE(template);
2699
+ return [2 /*return*/, preparedPipeline.knowledgePieces.map(function (_a) {
2700
+ var content = _a.content;
2701
+ return "- ".concat(content);
2702
+ }).join('\n')];
2703
+ });
2704
+ });
2705
+ }
2706
+ function getSamplesForTemplate(// <- TODO: [🧠][🥜]
2707
+ template) {
2708
+ return __awaiter(this, void 0, void 0, function () {
2709
+ return __generator(this, function (_a) {
2710
+ // TODO: [♨] Implement Better - use real index and keyword search
2711
+ TODO_USE(template);
2712
+ return [2 /*return*/, RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */];
2713
+ });
2714
+ });
2715
+ }
2716
+ function getReservedParametersForTemplate(template) {
2717
+ return __awaiter(this, void 0, void 0, function () {
2718
+ var context, knowledge, samples, currentDate, modelName, reservedParameters, RESERVED_PARAMETER_NAMES_1, RESERVED_PARAMETER_NAMES_1_1, parameterName;
2719
+ var e_3, _a;
2720
+ return __generator(this, function (_b) {
2721
+ switch (_b.label) {
2722
+ case 0: return [4 /*yield*/, getContextForTemplate(template)];
2723
+ case 1:
2724
+ context = _b.sent();
2725
+ return [4 /*yield*/, getKnowledgeForTemplate(template)];
2726
+ case 2:
2727
+ knowledge = _b.sent();
2728
+ return [4 /*yield*/, getSamplesForTemplate(template)];
2729
+ case 3:
2730
+ samples = _b.sent();
2731
+ currentDate = new Date().toISOString();
2732
+ modelName = RESERVED_PARAMETER_MISSING_VALUE;
2733
+ reservedParameters = {
2734
+ content: RESERVED_PARAMETER_RESTRICTED,
2735
+ context: context,
2736
+ knowledge: knowledge,
2737
+ samples: samples,
2738
+ currentDate: currentDate,
2739
+ modelName: modelName,
2740
+ };
2741
+ try {
2742
+ // Note: Doublecheck that ALL reserved parameters are defined:
2743
+ 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()) {
2744
+ parameterName = RESERVED_PARAMETER_NAMES_1_1.value;
2745
+ if (reservedParameters[parameterName] === undefined) {
2746
+ throw new UnexpectedError("Reserved parameter {".concat(parameterName, "} is not defined"));
2747
+ }
2748
+ }
2749
+ }
2750
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2751
+ finally {
2752
+ try {
2753
+ if (RESERVED_PARAMETER_NAMES_1_1 && !RESERVED_PARAMETER_NAMES_1_1.done && (_a = RESERVED_PARAMETER_NAMES_1.return)) _a.call(RESERVED_PARAMETER_NAMES_1);
2754
+ }
2755
+ finally { if (e_3) throw e_3.error; }
2756
+ }
2757
+ return [2 /*return*/, reservedParameters];
2758
+ }
2759
+ });
2760
+ });
2761
+ }
2087
2762
  function executeSingleTemplate(currentTemplate) {
2088
2763
  return __awaiter(this, void 0, void 0, function () {
2089
- 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;
2090
- var e_2, _j, e_4, _k, e_3, _l, _m;
2764
+ 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;
2765
+ var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2091
2766
  var _this = this;
2092
- return __generator(this, function (_o) {
2093
- switch (_o.label) {
2767
+ return __generator(this, function (_u) {
2768
+ switch (_u.label) {
2094
2769
  case 0:
2095
2770
  name = "pipeline-executor-frame-".concat(currentTemplate.name);
2096
2771
  title = currentTemplate.title;
2097
- priority = pipeline.promptTemplates.length - pipeline.promptTemplates.indexOf(currentTemplate);
2772
+ priority = preparedPipeline.promptTemplates.length - preparedPipeline.promptTemplates.indexOf(currentTemplate);
2098
2773
  if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
2099
2774
  return [4 /*yield*/, onProgress({
2100
2775
  name: name,
@@ -2107,61 +2782,111 @@ function createPipelineExecutor(options) {
2107
2782
  // <- [3]
2108
2783
  })];
2109
2784
  case 1:
2110
- _o.sent();
2111
- _o.label = 2;
2785
+ _u.sent();
2786
+ _u.label = 2;
2112
2787
  case 2:
2788
+ usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
2789
+ dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2790
+ if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2791
+ throw new UnexpectedError(spaceTrim$1("\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 ")));
2792
+ }
2793
+ _b = (_a = Object).freeze;
2794
+ _c = [{}];
2795
+ return [4 /*yield*/, getReservedParametersForTemplate(currentTemplate)];
2796
+ case 3:
2797
+ definedParameters = _b.apply(_a, [__assign.apply(void 0, [__assign.apply(void 0, _c.concat([(_u.sent())])), parametersToPass])]);
2798
+ definedParameterNames = new Set(Object.keys(definedParameters));
2799
+ parameters = {};
2800
+ try {
2801
+ // Note: [2] Check that all used parameters are defined and removing unused parameters for this template
2802
+ for (_d = __values(Array.from(union(definedParameterNames, usedParameterNames, dependentParameterNames))), _e = _d.next(); !_e.done; _e = _d.next()) {
2803
+ parameterName = _e.value;
2804
+ // Situation: Parameter is defined and used
2805
+ if (definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2806
+ parameters[parameterName] = definedParameters[parameterName];
2807
+ }
2808
+ // Situation: Parameter is defined but NOT used
2809
+ else if (definedParameterNames.has(parameterName) && !usedParameterNames.has(parameterName)) {
2810
+ // Do not pass this parameter to prompt
2811
+ }
2812
+ // Situation: Parameter is NOT defined BUT used
2813
+ else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2814
+ // Houston, we have a problem
2815
+ // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2816
+ throw new UnexpectedError(spaceTrim$1("\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 ")));
2817
+ }
2818
+ }
2819
+ }
2820
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2821
+ finally {
2822
+ try {
2823
+ if (_e && !_e.done && (_p = _d.return)) _p.call(_d);
2824
+ }
2825
+ finally { if (e_7) throw e_7.error; }
2826
+ }
2827
+ // Note: Now we can freeze `parameters` because we are sure that all and only used parameters are defined
2828
+ Object.freeze(parameters);
2113
2829
  result = null;
2114
2830
  resultString = null;
2115
2831
  expectError = null;
2116
2832
  maxAttempts = currentTemplate.blockType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
2117
- jokers = currentTemplate.jokers || [];
2118
- attempt = -jokers.length;
2119
- _o.label = 3;
2120
- case 3:
2121
- if (!(attempt < maxAttempts)) return [3 /*break*/, 51];
2833
+ jokerParameterNames = currentTemplate.jokerParameterNames || [];
2834
+ preparedContent = (currentTemplate.preparedContent || '{content}')
2835
+ .split('{content}')
2836
+ .join(currentTemplate.content);
2837
+ attempt = -jokerParameterNames.length;
2838
+ _u.label = 4;
2839
+ case 4:
2840
+ if (!(attempt < maxAttempts)) return [3 /*break*/, 52];
2122
2841
  isJokerAttempt = attempt < 0;
2123
- joker = jokers[jokers.length + attempt];
2124
- if (isJokerAttempt && !joker) {
2842
+ jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
2843
+ if (isJokerAttempt && !jokerParameterName) {
2125
2844
  throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
2126
2845
  }
2127
2846
  result = null;
2128
2847
  resultString = null;
2129
2848
  expectError = null;
2130
2849
  if (isJokerAttempt) {
2131
- if (typeof parametersToPass[joker] === 'undefined') {
2132
- throw new PipelineExecutionError("Joker parameter {".concat(joker, "} not defined"));
2850
+ if (parameters[jokerParameterName] === undefined) {
2851
+ throw new PipelineExecutionError("Joker parameter {".concat(jokerParameterName, "} not defined"));
2852
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
2853
+ }
2854
+ else {
2855
+ resultString = parameters[jokerParameterName];
2133
2856
  }
2134
- resultString = parametersToPass[joker];
2135
- }
2136
- _o.label = 4;
2137
- case 4:
2138
- _o.trys.push([4, 47, 48, 49]);
2139
- if (!!isJokerAttempt) return [3 /*break*/, 29];
2140
- _a = currentTemplate.blockType;
2141
- switch (_a) {
2142
- case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
2143
- case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
2144
- case 'SCRIPT': return [3 /*break*/, 15];
2145
- case 'PROMPT_DIALOG': return [3 /*break*/, 26];
2146
2857
  }
2147
- return [3 /*break*/, 28];
2858
+ _u.label = 5;
2148
2859
  case 5:
2149
- resultString = replaceParameters(currentTemplate.content, parametersToPass);
2860
+ _u.trys.push([5, 48, 49, 50]);
2861
+ if (!!isJokerAttempt) return [3 /*break*/, 30];
2862
+ _f = currentTemplate.blockType;
2863
+ switch (_f) {
2864
+ case 'SIMPLE_TEMPLATE': return [3 /*break*/, 6];
2865
+ case 'PROMPT_TEMPLATE': return [3 /*break*/, 7];
2866
+ case 'SCRIPT': return [3 /*break*/, 16];
2867
+ case 'PROMPT_DIALOG': return [3 /*break*/, 27];
2868
+ }
2150
2869
  return [3 /*break*/, 29];
2151
2870
  case 6:
2871
+ resultString = replaceParameters(preparedContent, parameters);
2872
+ return [3 /*break*/, 30];
2873
+ case 7:
2152
2874
  prompt = {
2153
2875
  title: currentTemplate.title,
2154
- pipelineUrl: "".concat(pipeline.pipelineUrl
2155
- ? pipeline.pipelineUrl
2876
+ pipelineUrl: "".concat(preparedPipeline.pipelineUrl
2877
+ ? preparedPipeline.pipelineUrl
2156
2878
  : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
2157
- parameters: parametersToPass,
2158
- content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2879
+ parameters: parameters,
2880
+ content: preparedContent,
2159
2881
  modelRequirements: currentTemplate.modelRequirements,
2160
- expectations: currentTemplate.expectations,
2882
+ expectations: __assign(__assign({}, (preparedPipeline.personas.find(function (_a) {
2883
+ var name = _a.name;
2884
+ return name === currentTemplate.personaName;
2885
+ }) || {})), currentTemplate.expectations),
2161
2886
  expectFormat: currentTemplate.expectFormat,
2162
- postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2163
- var errors, _a, _b, scriptTools, error_5, e_5_1;
2164
- var e_5, _c;
2887
+ postprocessing: (currentTemplate.postprocessingFunctionNames || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2888
+ var errors, _a, _b, scriptTools, error_5, e_8_1;
2889
+ var e_8, _c;
2165
2890
  return __generator(this, function (_d) {
2166
2891
  switch (_d.label) {
2167
2892
  case 0:
@@ -2182,7 +2907,7 @@ function createPipelineExecutor(options) {
2182
2907
  script: "".concat(functionName, "(result)"),
2183
2908
  parameters: {
2184
2909
  result: result || '',
2185
- // Note: No ...parametersToPass, because working with result only
2910
+ // Note: No ...parametersForTemplate, because working with result only
2186
2911
  },
2187
2912
  })];
2188
2913
  case 4: return [2 /*return*/, _d.sent()];
@@ -2191,6 +2916,9 @@ function createPipelineExecutor(options) {
2191
2916
  if (!(error_5 instanceof Error)) {
2192
2917
  throw error_5;
2193
2918
  }
2919
+ if (error_5 instanceof UnexpectedError) {
2920
+ throw error_5;
2921
+ }
2194
2922
  errors.push(error_5);
2195
2923
  return [3 /*break*/, 6];
2196
2924
  case 6:
@@ -2198,14 +2926,14 @@ function createPipelineExecutor(options) {
2198
2926
  return [3 /*break*/, 2];
2199
2927
  case 7: return [3 /*break*/, 10];
2200
2928
  case 8:
2201
- e_5_1 = _d.sent();
2202
- e_5 = { error: e_5_1 };
2929
+ e_8_1 = _d.sent();
2930
+ e_8 = { error: e_8_1 };
2203
2931
  return [3 /*break*/, 10];
2204
2932
  case 9:
2205
2933
  try {
2206
2934
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2207
2935
  }
2208
- finally { if (e_5) throw e_5.error; }
2936
+ finally { if (e_8) throw e_8.error; }
2209
2937
  return [7 /*endfinally*/];
2210
2938
  case 10:
2211
2939
  if (errors.length === 0) {
@@ -2221,35 +2949,35 @@ function createPipelineExecutor(options) {
2221
2949
  });
2222
2950
  }); }; }),
2223
2951
  };
2224
- _b = currentTemplate.modelRequirements.modelVariant;
2225
- switch (_b) {
2226
- case 'CHAT': return [3 /*break*/, 7];
2227
- case 'COMPLETION': return [3 /*break*/, 9];
2228
- case 'EMBEDDING': return [3 /*break*/, 11];
2952
+ _g = currentTemplate.modelRequirements.modelVariant;
2953
+ switch (_g) {
2954
+ case 'CHAT': return [3 /*break*/, 8];
2955
+ case 'COMPLETION': return [3 /*break*/, 10];
2956
+ case 'EMBEDDING': return [3 /*break*/, 12];
2229
2957
  }
2230
- return [3 /*break*/, 13];
2231
- case 7: return [4 /*yield*/, llmTools.callChatModel(prompt)];
2232
- case 8:
2233
- chatResult = _o.sent();
2958
+ return [3 /*break*/, 14];
2959
+ case 8: return [4 /*yield*/, llmTools.callChatModel(deepFreeze(prompt))];
2960
+ case 9:
2961
+ chatResult = _u.sent();
2234
2962
  // TODO: [🍬] Destroy chatThread
2235
2963
  result = chatResult;
2236
2964
  resultString = chatResult.content;
2237
- return [3 /*break*/, 14];
2238
- case 9: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
2239
- case 10:
2240
- completionResult = _o.sent();
2965
+ return [3 /*break*/, 15];
2966
+ case 10: return [4 /*yield*/, llmTools.callCompletionModel(deepFreeze(prompt))];
2967
+ case 11:
2968
+ completionResult = _u.sent();
2241
2969
  result = completionResult;
2242
2970
  resultString = completionResult.content;
2243
- return [3 /*break*/, 14];
2244
- case 11: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
2245
- case 12:
2246
- embeddingResult = _o.sent();
2971
+ return [3 /*break*/, 15];
2972
+ case 12: return [4 /*yield*/, llmTools.callEmbeddingModel(deepFreeze(prompt))];
2973
+ case 13:
2974
+ embeddingResult = _u.sent();
2247
2975
  result = embeddingResult;
2248
2976
  resultString = embeddingResult.content.join(',');
2249
- return [3 /*break*/, 14];
2250
- case 13: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2251
- case 14: return [3 /*break*/, 29];
2252
- case 15:
2977
+ return [3 /*break*/, 15];
2978
+ case 14: throw new PipelineExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
2979
+ case 15: return [3 /*break*/, 30];
2980
+ case 16:
2253
2981
  if (arrayableToArray(tools.script).length === 0) {
2254
2982
  throw new PipelineExecutionError('No script execution tools are available');
2255
2983
  }
@@ -2258,49 +2986,52 @@ function createPipelineExecutor(options) {
2258
2986
  }
2259
2987
  // TODO: DRY [1]
2260
2988
  scriptPipelineExecutionErrors = [];
2261
- _o.label = 16;
2262
- case 16:
2263
- _o.trys.push([16, 23, 24, 25]);
2264
- _c = (e_2 = void 0, __values(arrayableToArray(tools.script))), _d = _c.next();
2265
- _o.label = 17;
2989
+ _u.label = 17;
2266
2990
  case 17:
2267
- if (!!_d.done) return [3 /*break*/, 22];
2268
- scriptTools = _d.value;
2269
- _o.label = 18;
2991
+ _u.trys.push([17, 24, 25, 26]);
2992
+ _h = (e_4 = void 0, __values(arrayableToArray(tools.script))), _j = _h.next();
2993
+ _u.label = 18;
2270
2994
  case 18:
2271
- _o.trys.push([18, 20, , 21]);
2272
- return [4 /*yield*/, scriptTools.execute({
2273
- scriptLanguage: currentTemplate.contentLanguage,
2274
- script: currentTemplate.content,
2275
- parameters: parametersToPass,
2276
- })];
2995
+ if (!!_j.done) return [3 /*break*/, 23];
2996
+ scriptTools = _j.value;
2997
+ _u.label = 19;
2277
2998
  case 19:
2278
- resultString = _o.sent();
2279
- return [3 /*break*/, 22];
2999
+ _u.trys.push([19, 21, , 22]);
3000
+ return [4 /*yield*/, scriptTools.execute(deepFreeze({
3001
+ scriptLanguage: currentTemplate.contentLanguage,
3002
+ script: preparedContent,
3003
+ parameters: parameters,
3004
+ }))];
2280
3005
  case 20:
2281
- error_2 = _o.sent();
3006
+ resultString = _u.sent();
3007
+ return [3 /*break*/, 23];
3008
+ case 21:
3009
+ error_2 = _u.sent();
2282
3010
  if (!(error_2 instanceof Error)) {
2283
3011
  throw error_2;
2284
3012
  }
3013
+ if (error_2 instanceof UnexpectedError) {
3014
+ throw error_2;
3015
+ }
2285
3016
  scriptPipelineExecutionErrors.push(error_2);
2286
- return [3 /*break*/, 21];
2287
- case 21:
2288
- _d = _c.next();
2289
- return [3 /*break*/, 17];
2290
- case 22: return [3 /*break*/, 25];
2291
- case 23:
2292
- e_2_1 = _o.sent();
2293
- e_2 = { error: e_2_1 };
2294
- return [3 /*break*/, 25];
3017
+ return [3 /*break*/, 22];
3018
+ case 22:
3019
+ _j = _h.next();
3020
+ return [3 /*break*/, 18];
3021
+ case 23: return [3 /*break*/, 26];
2295
3022
  case 24:
3023
+ e_4_1 = _u.sent();
3024
+ e_4 = { error: e_4_1 };
3025
+ return [3 /*break*/, 26];
3026
+ case 25:
2296
3027
  try {
2297
- if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
3028
+ if (_j && !_j.done && (_q = _h.return)) _q.call(_h);
2298
3029
  }
2299
- finally { if (e_2) throw e_2.error; }
3030
+ finally { if (e_4) throw e_4.error; }
2300
3031
  return [7 /*endfinally*/];
2301
- case 25:
3032
+ case 26:
2302
3033
  if (resultString !== null) {
2303
- return [3 /*break*/, 29];
3034
+ return [3 /*break*/, 30];
2304
3035
  }
2305
3036
  if (scriptPipelineExecutionErrors.length === 1) {
2306
3037
  throw scriptPipelineExecutionErrors[0];
@@ -2310,101 +3041,104 @@ function createPipelineExecutor(options) {
2310
3041
  .map(function (error) { return '- ' + error.message; })
2311
3042
  .join('\n\n')), "\n "); }));
2312
3043
  }
2313
- case 26:
3044
+ case 27:
2314
3045
  if (tools.userInterface === undefined) {
2315
3046
  throw new PipelineExecutionError('User interface tools are not available');
2316
3047
  }
2317
- return [4 /*yield*/, tools.userInterface.promptDialog({
3048
+ return [4 /*yield*/, tools.userInterface.promptDialog(deepFreeze({
2318
3049
  promptTitle: currentTemplate.title,
2319
- promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
2320
- defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
3050
+ promptMessage: replaceParameters(currentTemplate.description || '', parameters),
3051
+ defaultValue: replaceParameters(preparedContent, parameters),
2321
3052
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
2322
3053
  placeholder: undefined,
2323
3054
  priority: priority,
2324
- })];
2325
- case 27:
3055
+ }))];
3056
+ case 28:
2326
3057
  // TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
2327
- resultString = _o.sent();
2328
- return [3 /*break*/, 29];
2329
- case 28: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2330
- case 29:
2331
- if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 46];
2332
- _o.label = 30;
3058
+ resultString = _u.sent();
3059
+ return [3 /*break*/, 30];
3060
+ case 29: throw new PipelineExecutionError("Unknown execution type \"".concat(currentTemplate.blockType, "\""));
2333
3061
  case 30:
2334
- _o.trys.push([30, 44, 45, 46]);
2335
- _e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
2336
- _o.label = 31;
3062
+ if (!(!isJokerAttempt && currentTemplate.postprocessingFunctionNames)) return [3 /*break*/, 47];
3063
+ _u.label = 31;
2337
3064
  case 31:
2338
- if (!!_f.done) return [3 /*break*/, 43];
2339
- functionName = _f.value;
3065
+ _u.trys.push([31, 45, 46, 47]);
3066
+ _k = (e_6 = void 0, __values(currentTemplate.postprocessingFunctionNames)), _l = _k.next();
3067
+ _u.label = 32;
3068
+ case 32:
3069
+ if (!!_l.done) return [3 /*break*/, 44];
3070
+ functionName = _l.value;
2340
3071
  // TODO: DRY [1]
2341
3072
  scriptPipelineExecutionErrors = [];
2342
3073
  postprocessingError = null;
2343
- _o.label = 32;
2344
- case 32:
2345
- _o.trys.push([32, 39, 40, 41]);
2346
- _g = (e_3 = void 0, __values(arrayableToArray(tools.script))), _h = _g.next();
2347
- _o.label = 33;
3074
+ _u.label = 33;
2348
3075
  case 33:
2349
- if (!!_h.done) return [3 /*break*/, 38];
2350
- scriptTools = _h.value;
2351
- _o.label = 34;
3076
+ _u.trys.push([33, 40, 41, 42]);
3077
+ _m = (e_5 = void 0, __values(arrayableToArray(tools.script))), _o = _m.next();
3078
+ _u.label = 34;
2352
3079
  case 34:
2353
- _o.trys.push([34, 36, , 37]);
3080
+ if (!!_o.done) return [3 /*break*/, 39];
3081
+ scriptTools = _o.value;
3082
+ _u.label = 35;
3083
+ case 35:
3084
+ _u.trys.push([35, 37, , 38]);
2354
3085
  return [4 /*yield*/, scriptTools.execute({
2355
3086
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2356
3087
  script: "".concat(functionName, "(resultString)"),
2357
3088
  parameters: {
2358
3089
  resultString: resultString || '',
2359
- // Note: No ...parametersToPass, because working with result only
3090
+ // Note: No ...parametersForTemplate, because working with result only
2360
3091
  },
2361
3092
  })];
2362
- case 35:
2363
- resultString = _o.sent();
2364
- postprocessingError = null;
2365
- return [3 /*break*/, 38];
2366
3093
  case 36:
2367
- error_3 = _o.sent();
3094
+ resultString = _u.sent();
3095
+ postprocessingError = null;
3096
+ return [3 /*break*/, 39];
3097
+ case 37:
3098
+ error_3 = _u.sent();
2368
3099
  if (!(error_3 instanceof Error)) {
2369
3100
  throw error_3;
2370
3101
  }
3102
+ if (error_3 instanceof UnexpectedError) {
3103
+ throw error_3;
3104
+ }
2371
3105
  postprocessingError = error_3;
2372
3106
  scriptPipelineExecutionErrors.push(error_3);
2373
- return [3 /*break*/, 37];
2374
- case 37:
2375
- _h = _g.next();
2376
- return [3 /*break*/, 33];
2377
- case 38: return [3 /*break*/, 41];
2378
- case 39:
2379
- e_3_1 = _o.sent();
2380
- e_3 = { error: e_3_1 };
2381
- return [3 /*break*/, 41];
3107
+ return [3 /*break*/, 38];
3108
+ case 38:
3109
+ _o = _m.next();
3110
+ return [3 /*break*/, 34];
3111
+ case 39: return [3 /*break*/, 42];
2382
3112
  case 40:
3113
+ e_5_1 = _u.sent();
3114
+ e_5 = { error: e_5_1 };
3115
+ return [3 /*break*/, 42];
3116
+ case 41:
2383
3117
  try {
2384
- if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
3118
+ if (_o && !_o.done && (_s = _m.return)) _s.call(_m);
2385
3119
  }
2386
- finally { if (e_3) throw e_3.error; }
3120
+ finally { if (e_5) throw e_5.error; }
2387
3121
  return [7 /*endfinally*/];
2388
- case 41:
3122
+ case 42:
2389
3123
  if (postprocessingError) {
2390
3124
  throw postprocessingError;
2391
3125
  }
2392
- _o.label = 42;
2393
- case 42:
2394
- _f = _e.next();
2395
- return [3 /*break*/, 31];
2396
- case 43: return [3 /*break*/, 46];
2397
- case 44:
2398
- e_4_1 = _o.sent();
2399
- e_4 = { error: e_4_1 };
2400
- return [3 /*break*/, 46];
3126
+ _u.label = 43;
3127
+ case 43:
3128
+ _l = _k.next();
3129
+ return [3 /*break*/, 32];
3130
+ case 44: return [3 /*break*/, 47];
2401
3131
  case 45:
3132
+ e_6_1 = _u.sent();
3133
+ e_6 = { error: e_6_1 };
3134
+ return [3 /*break*/, 47];
3135
+ case 46:
2402
3136
  try {
2403
- if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
3137
+ if (_l && !_l.done && (_r = _k.return)) _r.call(_k);
2404
3138
  }
2405
- finally { if (e_4) throw e_4.error; }
3139
+ finally { if (e_6) throw e_6.error; }
2406
3140
  return [7 /*endfinally*/];
2407
- case 46:
3141
+ case 47:
2408
3142
  // TODO: [💝] Unite object for expecting amount and format
2409
3143
  if (currentTemplate.expectFormat) {
2410
3144
  if (currentTemplate.expectFormat === 'JSON') {
@@ -2417,15 +3151,18 @@ function createPipelineExecutor(options) {
2417
3151
  if (currentTemplate.expectations) {
2418
3152
  checkExpectations(currentTemplate.expectations, resultString || '');
2419
3153
  }
2420
- return [3 /*break*/, 51];
2421
- case 47:
2422
- error_4 = _o.sent();
3154
+ return [3 /*break*/, 52];
3155
+ case 48:
3156
+ error_4 = _u.sent();
2423
3157
  if (!(error_4 instanceof ExpectError)) {
2424
3158
  throw error_4;
2425
3159
  }
3160
+ if (error_4 instanceof UnexpectedError) {
3161
+ throw error_4;
3162
+ }
2426
3163
  expectError = error_4;
2427
- return [3 /*break*/, 49];
2428
- case 48:
3164
+ return [3 /*break*/, 50];
3165
+ case 49:
2429
3166
  if (!isJokerAttempt &&
2430
3167
  currentTemplate.blockType === 'PROMPT_TEMPLATE' &&
2431
3168
  prompt
@@ -2434,28 +3171,21 @@ function createPipelineExecutor(options) {
2434
3171
  ) {
2435
3172
  // TODO: [🧠] Maybe put other blockTypes into report
2436
3173
  executionReport.promptExecutions.push({
2437
- prompt: {
2438
- title: currentTemplate.title /* <- Note: If title in pipeline contains emojis, pass it innto report */,
2439
- content: prompt.content,
2440
- modelRequirements: prompt.modelRequirements,
2441
- expectations: prompt.expectations,
2442
- expectFormat: prompt.expectFormat,
2443
- // <- Note: Do want to pass ONLY wanted information to the report
2444
- },
3174
+ prompt: __assign({}, prompt),
2445
3175
  result: result || undefined,
2446
3176
  error: expectError || undefined,
2447
3177
  });
2448
3178
  }
2449
3179
  return [7 /*endfinally*/];
2450
- case 49:
3180
+ case 50:
2451
3181
  if (expectError !== null && attempt === maxAttempts - 1) {
2452
3182
  throw new PipelineExecutionError(spaceTrim$1(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 "); }));
2453
3183
  }
2454
- _o.label = 50;
2455
- case 50:
2456
- attempt++;
2457
- return [3 /*break*/, 3];
3184
+ _u.label = 51;
2458
3185
  case 51:
3186
+ attempt++;
3187
+ return [3 /*break*/, 4];
3188
+ case 52:
2459
3189
  if (resultString === null) {
2460
3190
  throw new UnexpectedError('Something went wrong and prompt result is null');
2461
3191
  }
@@ -2471,30 +3201,136 @@ function createPipelineExecutor(options) {
2471
3201
  // <- [3]
2472
3202
  });
2473
3203
  }
2474
- 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));
3204
+ 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)));
2475
3205
  return [2 /*return*/];
2476
3206
  }
2477
3207
  });
2478
3208
  });
2479
3209
  }
2480
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
2481
- var e_1, _c;
2482
- return __generator(this, function (_d) {
2483
- switch (_d.label) {
3210
+ function filterJustOutputParameters() {
3211
+ var e_9, _a;
3212
+ var outputParameters = {};
3213
+ try {
3214
+ // Note: Filter ONLY output parameters
3215
+ for (var _b = __values(preparedPipeline.parameters.filter(function (_a) {
3216
+ var isOutput = _a.isOutput;
3217
+ return isOutput;
3218
+ })), _c = _b.next(); !_c.done; _c = _b.next()) {
3219
+ var parameter = _c.value;
3220
+ if (parametersToPass[parameter.name] === undefined) {
3221
+ // [4]
3222
+ warnings.push(new PipelineExecutionError("Parameter {".concat(parameter.name, "} should be an output parameter, but it was not generated during pipeline execution")));
3223
+ continue;
3224
+ }
3225
+ outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3226
+ }
3227
+ }
3228
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
3229
+ finally {
3230
+ try {
3231
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3232
+ }
3233
+ finally { if (e_9) throw e_9.error; }
3234
+ }
3235
+ return outputParameters;
3236
+ }
3237
+ 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;
3238
+ var e_1, _e, e_2, _f;
3239
+ return __generator(this, function (_g) {
3240
+ switch (_g.label) {
2484
3241
  case 0:
2485
- parametersToPass = inputParameters;
3242
+ if (!(preparedPipeline === undefined)) return [3 /*break*/, 2];
3243
+ return [4 /*yield*/, preparePipeline(pipeline, {
3244
+ llmTools: llmTools,
3245
+ isVerbose: isVerbose,
3246
+ maxParallelCount: maxParallelCount,
3247
+ })];
3248
+ case 1:
3249
+ preparedPipeline = _g.sent();
3250
+ _g.label = 2;
3251
+ case 2:
3252
+ errors = [];
3253
+ warnings = [];
2486
3254
  executionReport = {
2487
- pipelineUrl: pipeline.pipelineUrl,
2488
- title: pipeline.title,
3255
+ pipelineUrl: preparedPipeline.pipelineUrl,
3256
+ title: preparedPipeline.title,
2489
3257
  promptbookUsedVersion: PROMPTBOOK_VERSION,
2490
- promptbookRequestedVersion: pipeline.promptbookVersion,
2491
- description: pipeline.description,
3258
+ promptbookRequestedVersion: preparedPipeline.promptbookVersion,
3259
+ description: preparedPipeline.description,
2492
3260
  promptExecutions: [],
2493
3261
  };
2494
- _d.label = 1;
2495
- case 1:
2496
- _d.trys.push([1, 6, , 7]);
2497
- resovedParameters_1 = pipeline.parameters
3262
+ try {
3263
+ // Note: Check that all input input parameters are defined
3264
+ for (_a = __values(preparedPipeline.parameters.filter(function (_a) {
3265
+ var isInput = _a.isInput;
3266
+ return isInput;
3267
+ })), _b = _a.next(); !_b.done; _b = _a.next()) {
3268
+ parameter = _b.value;
3269
+ if (inputParameters[parameter.name] === undefined) {
3270
+ return [2 /*return*/, deepFreezeWithSameType({
3271
+ isSuccessful: false,
3272
+ errors: __spreadArray([
3273
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an input parameter"))
3274
+ ], __read(errors), false),
3275
+ warnings: [],
3276
+ executionReport: executionReport,
3277
+ outputParameters: {},
3278
+ usage: ZERO_USAGE,
3279
+ preparedPipeline: preparedPipeline,
3280
+ })];
3281
+ }
3282
+ }
3283
+ }
3284
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3285
+ finally {
3286
+ try {
3287
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
3288
+ }
3289
+ finally { if (e_1) throw e_1.error; }
3290
+ }
3291
+ _loop_1 = function (parameterName) {
3292
+ var parameter = preparedPipeline.parameters.find(function (_a) {
3293
+ var name = _a.name;
3294
+ return name === parameterName;
3295
+ });
3296
+ if (parameter === undefined) {
3297
+ warnings.push(new PipelineExecutionError("Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.")));
3298
+ }
3299
+ else if (parameter.isInput === false) {
3300
+ return { value: deepFreezeWithSameType({
3301
+ isSuccessful: false,
3302
+ errors: __spreadArray([
3303
+ new PipelineExecutionError("Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input"))
3304
+ ], __read(errors), false),
3305
+ warnings: warnings,
3306
+ executionReport: executionReport,
3307
+ outputParameters: {},
3308
+ usage: ZERO_USAGE,
3309
+ preparedPipeline: preparedPipeline,
3310
+ }) };
3311
+ }
3312
+ };
3313
+ try {
3314
+ // Note: Check that no extra input parameters are passed
3315
+ for (_c = __values(Object.keys(inputParameters)), _d = _c.next(); !_d.done; _d = _c.next()) {
3316
+ parameterName = _d.value;
3317
+ state_1 = _loop_1(parameterName);
3318
+ if (typeof state_1 === "object")
3319
+ return [2 /*return*/, state_1.value];
3320
+ }
3321
+ }
3322
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
3323
+ finally {
3324
+ try {
3325
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
3326
+ }
3327
+ finally { if (e_2) throw e_2.error; }
3328
+ }
3329
+ parametersToPass = inputParameters;
3330
+ _g.label = 3;
3331
+ case 3:
3332
+ _g.trys.push([3, 8, , 9]);
3333
+ resovedParameterNames_1 = preparedPipeline.parameters
2498
3334
  .filter(function (_a) {
2499
3335
  var isInput = _a.isInput;
2500
3336
  return isInput;
@@ -2503,187 +3339,192 @@ function createPipelineExecutor(options) {
2503
3339
  var name = _a.name;
2504
3340
  return name;
2505
3341
  });
2506
- unresovedTemplates = __spreadArray([], __read(pipeline.promptTemplates), false);
3342
+ unresovedTemplates_1 = __spreadArray([], __read(preparedPipeline.promptTemplates), false);
2507
3343
  resolving_1 = [];
2508
3344
  loopLimit = LOOP_LIMIT;
2509
- _loop_1 = function () {
3345
+ _loop_2 = function () {
2510
3346
  var currentTemplate, work_1;
2511
- return __generator(this, function (_e) {
2512
- switch (_e.label) {
3347
+ return __generator(this, function (_h) {
3348
+ switch (_h.label) {
2513
3349
  case 0:
2514
3350
  if (loopLimit-- < 0) {
3351
+ // Note: Really UnexpectedError not LimitReachedError - this should be catched during validatePipeline
2515
3352
  throw new UnexpectedError('Loop limit reached during resolving parameters pipeline execution');
2516
3353
  }
2517
- currentTemplate = unresovedTemplates.find(function (template) {
2518
- return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
3354
+ currentTemplate = unresovedTemplates_1.find(function (template) {
3355
+ return template.dependentParameterNames.every(function (name) {
3356
+ return __spreadArray(__spreadArray([], __read(resovedParameterNames_1), false), __read(RESERVED_PARAMETER_NAMES), false).includes(name);
3357
+ });
2519
3358
  });
2520
3359
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
2521
- throw new UnexpectedError(spaceTrim$1("\n Can not resolve some parameters\n\n Note: This should be catched during validatePipeline\n "));
3360
+ throw new UnexpectedError(
3361
+ // TODO: [🐎] DRY
3362
+ spaceTrim$1(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3363
+ .map(function (_a) {
3364
+ var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
3365
+ return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
3366
+ .map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
3367
+ .join(' and '));
3368
+ })
3369
+ .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 "); }));
2522
3370
  case 1:
2523
3371
  if (!!currentTemplate) return [3 /*break*/, 3];
2524
3372
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
2525
3373
  case 2:
2526
- /* [5] */ _e.sent();
3374
+ /* [5] */ _h.sent();
2527
3375
  return [3 /*break*/, 4];
2528
3376
  case 3:
2529
- unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
3377
+ unresovedTemplates_1 = unresovedTemplates_1.filter(function (template) { return template !== currentTemplate; });
2530
3378
  work_1 = executeSingleTemplate(currentTemplate)
2531
3379
  .then(function () {
2532
- resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
3380
+ resovedParameterNames_1 = __spreadArray(__spreadArray([], __read(resovedParameterNames_1), false), [currentTemplate.resultingParameterName], false);
2533
3381
  })
2534
3382
  .then(function () {
2535
3383
  resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
2536
3384
  });
2537
3385
  resolving_1.push(work_1);
2538
- _e.label = 4;
3386
+ _h.label = 4;
2539
3387
  case 4: return [2 /*return*/];
2540
3388
  }
2541
3389
  });
2542
3390
  };
2543
- _d.label = 2;
2544
- case 2:
2545
- if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
2546
- return [5 /*yield**/, _loop_1()];
2547
- case 3:
2548
- _d.sent();
2549
- return [3 /*break*/, 2];
2550
- case 4: return [4 /*yield*/, Promise.all(resolving_1)];
3391
+ _g.label = 4;
3392
+ case 4:
3393
+ if (!(unresovedTemplates_1.length > 0)) return [3 /*break*/, 6];
3394
+ return [5 /*yield**/, _loop_2()];
2551
3395
  case 5:
2552
- _d.sent();
2553
- return [3 /*break*/, 7];
2554
- case 6:
2555
- error_1 = _d.sent();
3396
+ _g.sent();
3397
+ return [3 /*break*/, 4];
3398
+ case 6: return [4 /*yield*/, Promise.all(resolving_1)];
3399
+ case 7:
3400
+ _g.sent();
3401
+ return [3 /*break*/, 9];
3402
+ case 8:
3403
+ error_1 = _g.sent();
2556
3404
  if (!(error_1 instanceof Error)) {
2557
3405
  throw error_1;
2558
3406
  }
2559
3407
  usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2560
3408
  var result = _a.result;
2561
- return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3409
+ return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2562
3410
  })), false));
2563
- return [2 /*return*/, {
3411
+ outputParameters_1 = filterJustOutputParameters();
3412
+ return [2 /*return*/, deepFreezeWithSameType({
2564
3413
  isSuccessful: false,
2565
- errors: [error_1],
3414
+ errors: __spreadArray([error_1], __read(errors), false),
3415
+ warnings: warnings,
2566
3416
  usage: usage_1,
2567
3417
  executionReport: executionReport,
2568
- outputParameters: parametersToPass,
2569
- }];
2570
- case 7:
2571
- try {
2572
- // Note: Filter ONLY output parameters
2573
- for (_a = __values(pipeline.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
2574
- parameter = _b.value;
2575
- if (parameter.isOutput) {
2576
- continue;
2577
- }
2578
- delete parametersToPass[parameter.name];
2579
- }
2580
- }
2581
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2582
- finally {
2583
- try {
2584
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2585
- }
2586
- finally { if (e_1) throw e_1.error; }
2587
- }
3418
+ outputParameters: outputParameters_1,
3419
+ preparedPipeline: preparedPipeline,
3420
+ })];
3421
+ case 9:
2588
3422
  usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
2589
3423
  var result = _a.result;
2590
- return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3424
+ return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
2591
3425
  })), false));
2592
- return [2 /*return*/, {
3426
+ outputParameters = filterJustOutputParameters();
3427
+ return [2 /*return*/, deepFreezeWithSameType({
2593
3428
  isSuccessful: true,
2594
- errors: [],
3429
+ errors: errors,
3430
+ warnings: warnings,
2595
3431
  usage: usage,
2596
3432
  executionReport: executionReport,
2597
- outputParameters: parametersToPass,
2598
- }];
3433
+ outputParameters: outputParameters,
3434
+ preparedPipeline: preparedPipeline,
3435
+ })];
2599
3436
  }
2600
3437
  });
2601
3438
  }); };
2602
3439
  return pipelineExecutor;
2603
3440
  }
2604
3441
  /**
3442
+ * TODO: Use isVerbose here (not only pass to `preparePipeline`)
3443
+ * TODO: [🧠] Use here `countTotalUsage` and put preparation and prepared pipiline to report
3444
+ * TODO: [🪂] Use maxParallelCount here (not only pass to `preparePipeline`)
3445
+ * TODO: [♈] Probbably move expectations from templates to parameters
2605
3446
  * TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
2606
3447
  * TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
2607
3448
  * Note: CreatePipelineExecutorOptions are just connected to PipelineExecutor so do not extract to types folder
2608
3449
  * TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
3450
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3451
+ * TODO: [🧠][💷] `assertsExecutionSuccessful` should be the method of `PipelineExecutor` result BUT maybe NOT to preserve pure JSON object
2609
3452
  */
2610
3453
 
2611
- function prepareKnowledgeFromMarkdown(options) {
3454
+ /**
3455
+ * @@@
3456
+ */
3457
+ function prepareKnowledgeFromMarkdown(knowledgeContent /* <- TODO: [🖖] (?maybe not) Always the file */, options) {
2612
3458
  return __awaiter(this, void 0, void 0, function () {
2613
- var content, llmTools, _a, isVerbose, collection, prepareKnowledgeFromMarkdownExecutor, _b, prepareTitleExecutor, _c, prepareKeywordsExecutor, _d, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
2614
- var _e, _f, _g;
3459
+ var llmTools, _a, maxParallelCount, _b, isVerbose, collection, prepareKnowledgeFromMarkdownExecutor, _c, prepareTitleExecutor, _d, prepareKeywordsExecutor, _e, result, outputParameters, knowledgePiecesRaw, knowledgeTextPieces, knowledge;
3460
+ var _f, _g, _h;
2615
3461
  var _this = this;
2616
- return __generator(this, function (_h) {
2617
- switch (_h.label) {
3462
+ return __generator(this, function (_j) {
3463
+ switch (_j.label) {
2618
3464
  case 0:
2619
- content = options.content, llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
3465
+ llmTools = options.llmTools, _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a, _b = options.isVerbose, isVerbose = _b === void 0 ? false : _b;
3466
+ TODO_USE(maxParallelCount); // <- [🪂]
2620
3467
  collection = createCollectionFromJson.apply(void 0, __spreadArray([], __read(PipelineCollection), false));
2621
- _b = createPipelineExecutor;
2622
- _e = {};
2623
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md')];
2624
- case 1:
2625
- prepareKnowledgeFromMarkdownExecutor = _b.apply(void 0, [(_e.pipeline = _h.sent(),
2626
- _e.tools = {
2627
- llm: llmTools,
2628
- },
2629
- _e)]);
2630
3468
  _c = createPipelineExecutor;
2631
3469
  _f = {};
2632
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md')];
2633
- case 2:
2634
- prepareTitleExecutor = _c.apply(void 0, [(_f.pipeline = _h.sent(),
3470
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md')];
3471
+ case 1:
3472
+ prepareKnowledgeFromMarkdownExecutor = _c.apply(void 0, [(_f.pipeline = _j.sent(),
2635
3473
  _f.tools = {
2636
3474
  llm: llmTools,
2637
3475
  },
2638
3476
  _f)]);
2639
3477
  _d = createPipelineExecutor;
2640
3478
  _g = {};
2641
- return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md')];
2642
- case 3:
2643
- prepareKeywordsExecutor = _d.apply(void 0, [(_g.pipeline = _h.sent(),
3479
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md')];
3480
+ case 2:
3481
+ prepareTitleExecutor = _d.apply(void 0, [(_g.pipeline = _j.sent(),
2644
3482
  _g.tools = {
2645
3483
  llm: llmTools,
2646
3484
  },
2647
3485
  _g)]);
2648
- return [4 /*yield*/, prepareKnowledgeFromMarkdownExecutor({ content: content })];
3486
+ _e = createPipelineExecutor;
3487
+ _h = {};
3488
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md')];
3489
+ case 3:
3490
+ prepareKeywordsExecutor = _e.apply(void 0, [(_h.pipeline = _j.sent(),
3491
+ _h.tools = {
3492
+ llm: llmTools,
3493
+ },
3494
+ _h)]);
3495
+ return [4 /*yield*/, prepareKnowledgeFromMarkdownExecutor({ knowledgeContent: knowledgeContent })];
2649
3496
  case 4:
2650
- result = _h.sent();
2651
- // TODO: [0] !!! Aggeregate usage
3497
+ result = _j.sent();
2652
3498
  assertsExecutionSuccessful(result);
2653
3499
  outputParameters = result.outputParameters;
2654
- knowledgeRaw = outputParameters.knowledge;
2655
- knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
3500
+ knowledgePiecesRaw = outputParameters.knowledgePieces;
3501
+ knowledgeTextPieces = (knowledgePiecesRaw || '').split('\n---\n');
3502
+ // <- TODO: !!!!! Smarter split and filter out empty pieces
2656
3503
  if (isVerbose) {
2657
3504
  console.info('knowledgeTextPieces:', knowledgeTextPieces);
2658
3505
  }
2659
3506
  return [4 /*yield*/, Promise.all(
2660
- // TODO: !!! Do not send all at once but in chunks
3507
+ // TODO: [🪂] !! Do not send all at once but in chunks
2661
3508
  knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
2662
- var name, title, content, keywords, index, sources, titleResult, _a, titleRaw, keywordsResult, _b, keywordsRaw, embeddingResult, error_1;
3509
+ var name, title, knowledgePieceContent, keywords, index, titleResult, _a, titleRaw, keywordsResult, _b, keywordsRaw, embeddingResult, error_1;
2663
3510
  return __generator(this, function (_c) {
2664
3511
  switch (_c.label) {
2665
3512
  case 0:
2666
3513
  name = "piece-".concat(i);
2667
3514
  title = spaceTrim(knowledgeTextPiece.substring(0, 100));
2668
- content = spaceTrim(knowledgeTextPiece);
3515
+ knowledgePieceContent = spaceTrim(knowledgeTextPiece);
2669
3516
  keywords = [];
2670
3517
  index = [];
2671
- sources = [
2672
- {
2673
- title: 'Markdown document' /* <- TODO: !!! Unhardcode */,
2674
- href: '#' /* <- TODO: !!! Unhardcode */,
2675
- },
2676
- ];
2677
3518
  _c.label = 1;
2678
3519
  case 1:
2679
3520
  _c.trys.push([1, 7, , 8]);
2680
- return [4 /*yield*/, prepareTitleExecutor({ content: content })];
3521
+ return [4 /*yield*/, prepareTitleExecutor({ knowledgePieceContent: knowledgePieceContent })];
2681
3522
  case 2:
2682
3523
  titleResult = _c.sent();
2683
3524
  _a = titleResult.outputParameters.title, titleRaw = _a === void 0 ? 'Untitled' : _a;
2684
3525
  title = spaceTrim(titleRaw) /* <- TODO: Maybe do in pipeline */;
2685
3526
  name = titleToName(title);
2686
- return [4 /*yield*/, prepareKeywordsExecutor({ content: content })];
3527
+ return [4 /*yield*/, prepareKeywordsExecutor({ knowledgePieceContent: knowledgePieceContent })];
2687
3528
  case 3:
2688
3529
  keywordsResult = _c.sent();
2689
3530
  _b = keywordsResult.outputParameters.keywords, keywordsRaw = _b === void 0 ? '' : _b;
@@ -2701,14 +3542,13 @@ function prepareKnowledgeFromMarkdown(options) {
2701
3542
  case 4: return [4 /*yield*/, llmTools.callEmbeddingModel({
2702
3543
  title: "Embedding for ".concat(title) /* <- Note: No impact on embedding result itself, just for logging */,
2703
3544
  parameters: {},
2704
- content: content,
3545
+ content: knowledgePieceContent,
2705
3546
  modelRequirements: {
2706
3547
  modelVariant: 'EMBEDDING',
2707
3548
  },
2708
3549
  })];
2709
3550
  case 5:
2710
3551
  embeddingResult = _c.sent();
2711
- // TODO: [0] !!! Aggeregate usage embeddingResult.usage
2712
3552
  index.push({
2713
3553
  modelName: embeddingResult.modelName,
2714
3554
  position: embeddingResult.content,
@@ -2723,24 +3563,554 @@ function prepareKnowledgeFromMarkdown(options) {
2723
3563
  case 8: return [2 /*return*/, {
2724
3564
  name: name,
2725
3565
  title: title,
2726
- content: content,
3566
+ content: knowledgePieceContent,
2727
3567
  keywords: keywords,
2728
3568
  index: index,
2729
- sources: sources,
3569
+ // <- TODO: [☀] sources,
2730
3570
  }];
2731
3571
  }
2732
3572
  });
2733
3573
  }); }))];
2734
3574
  case 5:
2735
- knowledge = _h.sent();
3575
+ knowledge = _j.sent();
2736
3576
  return [2 /*return*/, knowledge];
2737
3577
  }
2738
3578
  });
2739
- });
2740
- }
3579
+ });
3580
+ }
3581
+ /**
3582
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/markdown`
3583
+ * TODO: [🪂] Do it in parallel 11:11
3584
+ * Note: No need to aggregate usage here, it is done by intercepting the llmTools
3585
+ */
3586
+
3587
+ /**
3588
+ * Prepares the knowle
3589
+ *
3590
+ * @see https://github.com/webgptorg/promptbook/discussions/41
3591
+ */
3592
+ function prepareKnowledgePieces(knowledgeSources, options) {
3593
+ return __awaiter(this, void 0, void 0, function () {
3594
+ var _a, maxParallelCount, knowledgePrepared;
3595
+ var _this = this;
3596
+ return __generator(this, function (_b) {
3597
+ switch (_b.label) {
3598
+ case 0:
3599
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a;
3600
+ knowledgePrepared = [];
3601
+ return [4 /*yield*/, forEachAsync(knowledgeSources, { maxParallelCount: maxParallelCount }, function (knowledgeSource) { return __awaiter(_this, void 0, void 0, function () {
3602
+ var partialPieces, pieces;
3603
+ return __generator(this, function (_a) {
3604
+ switch (_a.label) {
3605
+ case 0: return [4 /*yield*/, prepareKnowledgeFromMarkdown(knowledgeSource.source, // <- TODO: [🐝] !!! Unhardcode markdown, detect which type it is
3606
+ options)];
3607
+ case 1:
3608
+ partialPieces = _a.sent();
3609
+ pieces = partialPieces.map(function (partialPiece) { return (__assign(__assign({}, partialPiece), { sources: [
3610
+ {
3611
+ name: knowledgeSource.name,
3612
+ // line, column <- TODO: [☀]
3613
+ // <- TODO: [❎]
3614
+ },
3615
+ ] })); });
3616
+ knowledgePrepared.push.apply(knowledgePrepared, __spreadArray([], __read(pieces), false));
3617
+ return [2 /*return*/];
3618
+ }
3619
+ });
3620
+ }); })];
3621
+ case 1:
3622
+ _b.sent();
3623
+ return [2 /*return*/, knowledgePrepared];
3624
+ }
3625
+ });
3626
+ });
3627
+ }
3628
+ /*
3629
+ TODO: [🧊] This is how it can look in future
3630
+ > type PrepareKnowledgeKnowledge = {
3631
+ > /**
3632
+ > * Unprepared knowledge
3633
+ > * /
3634
+ > readonly knowledgeSources: Array<KnowledgeSourceJson>;
3635
+ > };
3636
+ >
3637
+ > export async function prepareKnowledgePieces(
3638
+ > knowledge: PrepareKnowledgeKnowledge,
3639
+ > options: PrepareOptions,
3640
+ > ):
3641
+ */
3642
+ /**
3643
+ * TODO: [🐝][🔼] !!! Export via `@promptbook/core`
3644
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3645
+ * Put `knowledgePieces` into `PrepareKnowledgeOptions`
3646
+ * TODO: [🪂] More than max things can run in parallel by acident [1,[2a,2b,_],[3a,3b,_]]
3647
+ * TODO: [🧠][❎] Do here propper M:N mapping
3648
+ * [x] One source can make multiple pieces
3649
+ * [ ] One piece can have multiple sources
3650
+ */
3651
+
3652
+ /**
3653
+ * Intercepts LLM tools and counts total usage of the tools
3654
+ *
3655
+ * @param llmTools LLM tools to be intercepted with usage counting
3656
+ * @returns LLM tools with same functionality with added total cost counting
3657
+ */
3658
+ function countTotalUsage(llmTools) {
3659
+ var _this = this;
3660
+ var totalUsage = ZERO_USAGE;
3661
+ var proxyTools = {
3662
+ get title() {
3663
+ // TODO: [🧠] Maybe put here some suffix
3664
+ return llmTools.title;
3665
+ },
3666
+ get description() {
3667
+ // TODO: [🧠] Maybe put here some suffix
3668
+ return llmTools.description;
3669
+ },
3670
+ listModels: function () {
3671
+ return /* not await */ llmTools.listModels();
3672
+ },
3673
+ get totalUsage() {
3674
+ return totalUsage;
3675
+ },
3676
+ };
3677
+ if (llmTools.callChatModel !== undefined) {
3678
+ proxyTools.callChatModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3679
+ var promptResult;
3680
+ return __generator(this, function (_a) {
3681
+ switch (_a.label) {
3682
+ case 0: return [4 /*yield*/, llmTools.callChatModel(prompt)];
3683
+ case 1:
3684
+ promptResult = _a.sent();
3685
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3686
+ return [2 /*return*/, promptResult];
3687
+ }
3688
+ });
3689
+ }); };
3690
+ }
3691
+ if (llmTools.callCompletionModel !== undefined) {
3692
+ proxyTools.callCompletionModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3693
+ var promptResult;
3694
+ return __generator(this, function (_a) {
3695
+ switch (_a.label) {
3696
+ case 0: return [4 /*yield*/, llmTools.callCompletionModel(prompt)];
3697
+ case 1:
3698
+ promptResult = _a.sent();
3699
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3700
+ return [2 /*return*/, promptResult];
3701
+ }
3702
+ });
3703
+ }); };
3704
+ }
3705
+ if (llmTools.callEmbeddingModel !== undefined) {
3706
+ proxyTools.callEmbeddingModel = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
3707
+ var promptResult;
3708
+ return __generator(this, function (_a) {
3709
+ switch (_a.label) {
3710
+ case 0: return [4 /*yield*/, llmTools.callEmbeddingModel(prompt)];
3711
+ case 1:
3712
+ promptResult = _a.sent();
3713
+ totalUsage = addUsage(totalUsage, promptResult.usage);
3714
+ return [2 /*return*/, promptResult];
3715
+ }
3716
+ });
3717
+ }); };
3718
+ }
3719
+ // <- Note: [🤖]
3720
+ return proxyTools;
3721
+ }
3722
+ /**
3723
+ * TODO: [🔼] !!! Export via `@promptbookcore/`
3724
+ * TODO: [🧠][💸] Maybe make some common abstraction `interceptLlmTools` and use here (or use javascript Proxy?)
3725
+ * TODO: [🧠] Is there some meaningfull way how to test this util
3726
+ * TODO: [🧠][🌯] Maybe a way how to hide ability to `get totalUsage`
3727
+ * > const [llmToolsWithUsage,getUsage] = countTotalUsage(llmTools);
3728
+ * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
3729
+ */
3730
+
3731
+ /**
3732
+ * Prepares the persona for the pipeline
3733
+ *
3734
+ * @see https://github.com/webgptorg/promptbook/discussions/22
3735
+ */
3736
+ function preparePersona(personaDescription, options) {
3737
+ return __awaiter(this, void 0, void 0, function () {
3738
+ var llmTools, _a, isVerbose, collection, preparePersonaExecutor, _b, availableModels, availableModelNames, result, outputParameters, modelRequirementsRaw, modelRequirements, modelName, systemMessage, temperature;
3739
+ var _c;
3740
+ return __generator(this, function (_d) {
3741
+ switch (_d.label) {
3742
+ case 0:
3743
+ llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
3744
+ collection = createCollectionFromJson.apply(void 0, __spreadArray([], __read(PipelineCollection), false));
3745
+ _b = createPipelineExecutor;
3746
+ _c = {};
3747
+ return [4 /*yield*/, collection.getPipelineByUrl('https://promptbook.studio/promptbook/prepare-persona.ptbk.md')];
3748
+ case 1:
3749
+ preparePersonaExecutor = _b.apply(void 0, [(_c.pipeline = _d.sent(),
3750
+ _c.tools = {
3751
+ llm: llmTools,
3752
+ },
3753
+ _c)]);
3754
+ return [4 /*yield*/, llmTools.listModels()];
3755
+ case 2:
3756
+ availableModels = _d.sent();
3757
+ availableModelNames = availableModels
3758
+ .filter(function (_a) {
3759
+ var modelVariant = _a.modelVariant;
3760
+ return modelVariant === 'CHAT';
3761
+ })
3762
+ .map(function (_a) {
3763
+ var modelName = _a.modelName;
3764
+ return modelName;
3765
+ })
3766
+ .join(',');
3767
+ return [4 /*yield*/, preparePersonaExecutor({ availableModelNames: availableModelNames, personaDescription: personaDescription })];
3768
+ case 3:
3769
+ result = _d.sent();
3770
+ assertsExecutionSuccessful(result);
3771
+ outputParameters = result.outputParameters;
3772
+ modelRequirementsRaw = outputParameters.modelRequirements;
3773
+ modelRequirements = JSON.parse(modelRequirementsRaw);
3774
+ if (isVerbose) {
3775
+ console.info("PERSONA ".concat(personaDescription), modelRequirements);
3776
+ }
3777
+ modelName = modelRequirements.modelName, systemMessage = modelRequirements.systemMessage, temperature = modelRequirements.temperature;
3778
+ return [2 /*return*/, {
3779
+ modelVariant: 'CHAT',
3780
+ modelName: modelName,
3781
+ systemMessage: systemMessage,
3782
+ temperature: temperature,
3783
+ }];
3784
+ }
3785
+ });
3786
+ });
3787
+ }
3788
+ /**
3789
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3790
+ * TODO: [🏢] !! Check validity of `modelName` in pipeline
3791
+ * TODO: [🏢] !! Check validity of `systemMessage` in pipeline
3792
+ * TODO: [🏢] !! Check validity of `temperature` in pipeline
3793
+ */
3794
+
3795
+ /**
3796
+ * @@@
3797
+ */
3798
+ function prepareTemplates(pipeline, options) {
3799
+ return __awaiter(this, void 0, void 0, function () {
3800
+ var _a, maxParallelCount, promptTemplates, parameters, knowledgePiecesCount, promptTemplatesPrepared;
3801
+ var _this = this;
3802
+ return __generator(this, function (_b) {
3803
+ switch (_b.label) {
3804
+ case 0:
3805
+ _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a;
3806
+ promptTemplates = pipeline.promptTemplates, parameters = pipeline.parameters, knowledgePiecesCount = pipeline.knowledgePiecesCount;
3807
+ // TODO: !!!!! Apply samples to each template (if missing and is for the template defined)
3808
+ TODO_USE(parameters);
3809
+ promptTemplatesPrepared = new Array(promptTemplates.length);
3810
+ 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 () {
3811
+ var dependentParameterNames, preparedContent, preparedTemplate;
3812
+ return __generator(this, function (_a) {
3813
+ dependentParameterNames = template.dependentParameterNames;
3814
+ preparedContent = undefined;
3815
+ if (knowledgePiecesCount > 0 && !dependentParameterNames.includes('knowledge')) {
3816
+ preparedContent = spaceTrim$1("\n {content}\n\n ## Knowledge\n\n {knowledge}\n ");
3817
+ // <- TODO: [🧠][🧻] Cutomize shape/language/formatting of the addition to the prompt
3818
+ dependentParameterNames = __spreadArray(__spreadArray([], __read(dependentParameterNames), false), [
3819
+ 'knowledge',
3820
+ ], false);
3821
+ }
3822
+ preparedTemplate = __assign(__assign({}, template), { dependentParameterNames: dependentParameterNames, preparedContent: preparedContent });
3823
+ promptTemplatesPrepared[index] = preparedTemplate;
3824
+ return [2 /*return*/];
3825
+ });
3826
+ }); })];
3827
+ case 1:
3828
+ _b.sent();
3829
+ return [2 /*return*/, { promptTemplatesPrepared: promptTemplatesPrepared }];
3830
+ }
3831
+ });
3832
+ });
3833
+ }
3834
+ /**
3835
+ * TODO: [🧠] Add context to each template (if missing)
3836
+ * TODO: [🧠] What is better name `prepareTemplate` or `prepareTemplateAndParameters`
3837
+ * TODO: [♨] !!! Prepare index the samples and maybe templates
3838
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3839
+ * TODO: Write tests for `preparePipeline`
3840
+ * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
3841
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3842
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3843
+ * TODO: [🧠][🥜]
3844
+ */
3845
+
3846
+ /**
3847
+ * Prepare pipeline from string (markdown) format to JSON format
3848
+ *
3849
+ * Note: This function does not validate logic of the pipeline
3850
+ * Note: This function acts as part of compilation process
3851
+ */
3852
+ function preparePipeline(pipeline, options) {
3853
+ return __awaiter(this, void 0, void 0, function () {
3854
+ var llmTools, _a, maxParallelCount, _b, isVerbose, parameters, promptTemplates,
3855
+ /*
3856
+ <- TODO: [🧠][0] `promptbookVersion` */
3857
+ knowledgeSources /*
3858
+ <- TODO: [🧊] `knowledgePieces` */, personas /*
3859
+ <- TODO: [🧊] `preparations` */, llmToolsWithUsage, currentPreparation, preparations, preparedPersonas, knowledgeSourcesPrepared, partialknowledgePiecesPrepared, knowledgePiecesPrepared, promptTemplatesPrepared /* TODO: parameters: parametersPrepared*/;
3860
+ var _this = this;
3861
+ return __generator(this, function (_c) {
3862
+ switch (_c.label) {
3863
+ case 0:
3864
+ llmTools = options.llmTools, _a = options.maxParallelCount, maxParallelCount = _a === void 0 ? MAX_PARALLEL_COUNT : _a, _b = options.isVerbose, isVerbose = _b === void 0 ? false : _b;
3865
+ parameters = pipeline.parameters, promptTemplates = pipeline.promptTemplates, knowledgeSources = pipeline.knowledgeSources, personas = pipeline.personas;
3866
+ llmToolsWithUsage = countTotalUsage(llmTools);
3867
+ currentPreparation = {
3868
+ id: 1,
3869
+ // TODO: [🍥]> date: $currentDate(),
3870
+ promptbookVersion: PROMPTBOOK_VERSION,
3871
+ modelUsage: ZERO_USAGE,
3872
+ };
3873
+ preparations = [
3874
+ // ...preparations
3875
+ // <- TODO: [🧊]
3876
+ currentPreparation,
3877
+ ];
3878
+ preparedPersonas = new Array(personas.length);
3879
+ 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 () {
3880
+ var modelRequirements, preparedPersona;
3881
+ return __generator(this, function (_a) {
3882
+ switch (_a.label) {
3883
+ case 0: return [4 /*yield*/, preparePersona(persona.description, {
3884
+ llmTools: llmToolsWithUsage,
3885
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3886
+ isVerbose: isVerbose,
3887
+ })];
3888
+ case 1:
3889
+ modelRequirements = _a.sent();
3890
+ preparedPersona = __assign(__assign({}, persona), { modelRequirements: modelRequirements, preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] });
3891
+ preparedPersonas[index] = preparedPersona;
3892
+ return [2 /*return*/];
3893
+ }
3894
+ });
3895
+ }); })];
3896
+ case 1:
3897
+ _c.sent();
3898
+ knowledgeSourcesPrepared = knowledgeSources.map(function (source) { return (__assign(__assign({}, source), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
3899
+ return [4 /*yield*/, prepareKnowledgePieces(knowledgeSources /* <- TODO: [🧊] {knowledgeSources, knowledgePieces} */, {
3900
+ llmTools: llmToolsWithUsage,
3901
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3902
+ isVerbose: isVerbose,
3903
+ })];
3904
+ case 2:
3905
+ partialknowledgePiecesPrepared = _c.sent();
3906
+ knowledgePiecesPrepared = partialknowledgePiecesPrepared.map(function (piece) { return (__assign(__assign({}, piece), { preparationIds: [/* TODO: [🧊] -> */ currentPreparation.id] })); });
3907
+ return [4 /*yield*/, prepareTemplates({
3908
+ parameters: parameters,
3909
+ promptTemplates: promptTemplates,
3910
+ knowledgePiecesCount: knowledgePiecesPrepared.length,
3911
+ }, {
3912
+ llmTools: llmToolsWithUsage,
3913
+ maxParallelCount: maxParallelCount /* <- TODO: [🪂] */,
3914
+ isVerbose: isVerbose,
3915
+ })];
3916
+ case 3:
3917
+ promptTemplatesPrepared = (_c.sent()).promptTemplatesPrepared;
3918
+ // ----- /Templates preparation -----
3919
+ // Note: Count total usage
3920
+ currentPreparation.modelUsage = llmToolsWithUsage.totalUsage;
3921
+ return [2 /*return*/, __assign(__assign({}, pipeline), { promptTemplates: promptTemplatesPrepared, knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations })];
3922
+ }
3923
+ });
3924
+ });
3925
+ }
3926
+ /**
3927
+ * TODO: [🔼] !!! Export via `@promptbook/core`
3928
+ * TODO: Write tests for `preparePipeline`
3929
+ * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
3930
+ * TODO: [🧊] In future one preparation can take data from previous preparation and save tokens and time
3931
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
3932
+ */
3933
+
3934
+ /**
3935
+ * Tests if given string is valid URL.
3936
+ *
3937
+ * Note: This does not check if the file exists only if the path is valid
3938
+ */
3939
+ function isValidFilePath(filePath) {
3940
+ if (typeof filePath !== 'string') {
3941
+ return false;
3942
+ }
3943
+ var filePathSlashes = filePath.split('\\').join('/');
3944
+ // Absolute Unix path: /hello.txt
3945
+ if (/^(\/)/i.test(filePathSlashes)) {
3946
+ return true;
3947
+ }
3948
+ // Absolute Windows path: /hello.txt
3949
+ if (/^([A-Z]{1,2}:\/?)\//i.test(filePathSlashes)) {
3950
+ return true;
3951
+ }
3952
+ // Relative path: ./hello.txt
3953
+ if (/^(\.\.?\/)+/i.test(filePathSlashes)) {
3954
+ return true;
3955
+ }
3956
+ return false;
3957
+ }
3958
+
3959
+ /**
3960
+ * Parses the knowledge command
3961
+ *
3962
+ * @see ./KNOWLEDGE-README.md for more details
3963
+ * @private within the commands folder
3964
+ */
3965
+ var knowledgeCommandParser = {
3966
+ /**
3967
+ * Name of the command
3968
+ */
3969
+ name: 'KNOWLEDGE',
3970
+ /**
3971
+ * BOILERPLATE command can be used in:
3972
+ */
3973
+ usagePlaces: ['PIPELINE_HEAD'],
3974
+ /**
3975
+ * Description of the KNOWLEDGE command
3976
+ */
3977
+ description: "Tells promptbook which external knowledge to use",
3978
+ /**
3979
+ * Link to discussion
3980
+ */
3981
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3982
+ /**
3983
+ * Example usages of the KNOWLEDGE command
3984
+ */
3985
+ examples: [
3986
+ 'KNOWLEDGE https://www.pavolhejny.com/',
3987
+ 'KNOWLEDGE ./hejny-cv.txt',
3988
+ 'KNOWLEDGE ./hejny-cv.md',
3989
+ 'KNOWLEDGE ./hejny-cv.pdf',
3990
+ 'KNOWLEDGE ./hejny-cv.docx',
3991
+ ],
3992
+ /**
3993
+ * Parses the KNOWLEDGE command
3994
+ */
3995
+ parse: function (input) {
3996
+ var args = input.args;
3997
+ var source = args[0];
3998
+ if (source === undefined) {
3999
+ throw new ParsingError("Source is not defined");
4000
+ }
4001
+ if (source.startsWith('http://')) {
4002
+ throw new ParsingError("Source is not secure");
4003
+ }
4004
+ if (!(isValidFilePath(source) || isValidUrl(source))) {
4005
+ throw new ParsingError("Source not valid");
4006
+ }
4007
+ if (source.startsWith('../') || source.startsWith('/') || /^[A-Z]:[\\/]+/i.test(source)) {
4008
+ throw new ParsingError("Source cannot be outside of the .ptbk.md folder");
4009
+ }
4010
+ return {
4011
+ type: 'KNOWLEDGE',
4012
+ source: source,
4013
+ };
4014
+ },
4015
+ /**
4016
+ * Note: Prototype of [🍧] (remove this comment after full implementation)
4017
+ */
4018
+ applyToPipelineJson: function (personaCommand, subjects) {
4019
+ var source = personaCommand.source;
4020
+ var pipelineJson = subjects.pipelineJson;
4021
+ var name = titleToName(source);
4022
+ pipelineJson.knowledgeSources.push({
4023
+ name: name,
4024
+ source: source,
4025
+ });
4026
+ },
4027
+ };
4028
+
2741
4029
  /**
2742
- * 11:11
4030
+ * Parses the persona command
4031
+ *
4032
+ * @see ./PERSONA-README.md for more details
4033
+ * @private within the commands folder
2743
4034
  */
4035
+ var personaCommandParser = {
4036
+ /**
4037
+ * Name of the command
4038
+ */
4039
+ name: 'PERSONA',
4040
+ /**
4041
+ * Aliases for the PERSONA command
4042
+ */
4043
+ aliasNames: ['PERSON'],
4044
+ /**
4045
+ * PERSONA command can be used in:
4046
+ */
4047
+ usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
4048
+ /**
4049
+ * Description of the PERSONA command
4050
+ */
4051
+ description: "Persona command is used to specify who the system is, it will be transformed into system message, top_t,...",
4052
+ /**
4053
+ * Link to discussion
4054
+ */
4055
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
4056
+ /**
4057
+ * Example usages of the PERSONA command
4058
+ */
4059
+ examples: ['PERSONA Jane, skilled copywriter', 'PERSONA Joe, male 28 years old, programmer'],
4060
+ /**
4061
+ * Parses the PERSONA command
4062
+ */
4063
+ parse: function (input) {
4064
+ var rawArgs = input.rawArgs;
4065
+ var _a = __read(rawArgs.split(/[,;:]/, 2), 2), personaNameRaw = _a[0], personaDescriptionRaw = _a[1];
4066
+ var personaName = (personaNameRaw || '').trim();
4067
+ if (personaName === '') {
4068
+ throw new ParsingError("You must set name for the persona");
4069
+ }
4070
+ var personaDescription = (personaDescriptionRaw || '').trim();
4071
+ if (personaDescription === '') {
4072
+ personaDescription = null;
4073
+ }
4074
+ return {
4075
+ type: 'PERSONA',
4076
+ personaName: personaName,
4077
+ personaDescription: personaDescription,
4078
+ };
4079
+ },
4080
+ /**
4081
+ * Note: Prototype of [🍧] (remove this comment after full implementation)
4082
+ */
4083
+ applyToPipelineJson: function (personaCommand, subjects) {
4084
+ var personaName = personaCommand.personaName, personaDescription = personaCommand.personaDescription;
4085
+ var pipelineJson = subjects.pipelineJson, templateJson = subjects.templateJson;
4086
+ if (templateJson !== null) {
4087
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
4088
+ throw new ParsingError("PERSONA command can be used only in PROMPT_TEMPLATE block");
4089
+ }
4090
+ templateJson.personaName = personaName;
4091
+ }
4092
+ var persona = pipelineJson.personas.find(function (persona) { return persona.name === personaName; });
4093
+ if (persona === undefined) {
4094
+ pipelineJson.personas.push({
4095
+ name: personaName,
4096
+ description: personaDescription || '',
4097
+ });
4098
+ return;
4099
+ }
4100
+ if (persona.description === personaDescription) {
4101
+ return;
4102
+ }
4103
+ if (personaDescription === null) {
4104
+ return;
4105
+ }
4106
+ if (persona.description === '') {
4107
+ persona.description = personaDescription;
4108
+ return;
4109
+ }
4110
+ console.warn(spaceTrim("\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 ")));
4111
+ persona.description += spaceTrim('\n\n' + personaDescription);
4112
+ },
4113
+ };
2744
4114
 
2745
4115
  /**
2746
4116
  * Removes Markdown formatting tags from a string.
@@ -2860,7 +4230,7 @@ var blockCommandParser = {
2860
4230
  /**
2861
4231
  * Link to discussion
2862
4232
  */
2863
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
4233
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/64',
2864
4234
  /**
2865
4235
  * Example usages of the BLOCK command
2866
4236
  */
@@ -2880,7 +4250,7 @@ var blockCommandParser = {
2880
4250
  'Knowledge BLOCK',
2881
4251
  // 'Knowledge', // <- Note: [⛱] For execution blocks which are also separate commands shortcut does not work
2882
4252
  //---
2883
- /* TODO: !!!! Not implemented block types will be in examples in future -> */
4253
+ /* Note: Not implemented block types will be in examples in future -> */
2884
4254
  'Instrument BLOCK',
2885
4255
  // 'Instrument', // <- Note: [⛱]
2886
4256
  'Action BLOCK',
@@ -2888,6 +4258,7 @@ var blockCommandParser = {
2888
4258
  //---
2889
4259
  /* <- TODO: [🧠] Maybe dynamic */
2890
4260
  ],
4261
+ // TODO: [♓️] order: -10 /* <- Note: Putting before other commands */
2891
4262
  /**
2892
4263
  * Parses the BLOCK command
2893
4264
  */
@@ -2896,19 +4267,19 @@ var blockCommandParser = {
2896
4267
  normalized = normalized.split('EXAMPLE').join('SAMPLE');
2897
4268
  var blockTypes = BlockTypes.filter(function (blockType) { return normalized.includes(blockType); });
2898
4269
  if (blockTypes.length !== 1) {
2899
- // console.log('!!!', { blockType });
2900
4270
  throw new ParsingError(spaceTrim(function (block) { return "\n Unknown block type in BLOCK command\n\n Supported block types are:\n ".concat(block(BlockTypes.join(', ')), "\n "); }));
2901
4271
  }
2902
- // TODO: !!!! Not supported yet
4272
+ var blockType = blockTypes[0];
2903
4273
  return {
2904
4274
  type: 'BLOCK',
2905
- blockType: blockTypes[0],
4275
+ blockType: blockType,
2906
4276
  };
2907
4277
  },
2908
4278
  };
2909
4279
 
2910
4280
  /**
2911
4281
  * Units of text measurement
4282
+ * @see https://github.com/webgptorg/promptbook/discussions/30
2912
4283
  */
2913
4284
  var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
2914
4285
  /**
@@ -3004,7 +4375,7 @@ var expectCommandParser = {
3004
4375
  /**
3005
4376
  * Link to discussion
3006
4377
  */
3007
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
4378
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/30',
3008
4379
  /**
3009
4380
  * Example usages of the EXPECT command
3010
4381
  */
@@ -3122,7 +4493,7 @@ var jokerCommandParser = {
3122
4493
  /**
3123
4494
  * Link to discussion
3124
4495
  */
3125
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
4496
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/66',
3126
4497
  /**
3127
4498
  * Example usages of the JOKER command
3128
4499
  */
@@ -3145,94 +4516,11 @@ var jokerCommandParser = {
3145
4516
  };
3146
4517
 
3147
4518
  /**
3148
- * Tests if given string is valid URL.
3149
- *
3150
- * Note: This does not check if the file exists only if the path is valid
3151
- */
3152
- function isValidFilePath(filePath) {
3153
- if (typeof filePath !== 'string') {
3154
- return false;
3155
- }
3156
- var filePathSlashes = filePath.split('\\').join('/');
3157
- // Absolute Unix path: /hello.txt
3158
- if (/^(\/)/i.test(filePathSlashes)) {
3159
- return true;
3160
- }
3161
- // Absolute Windows path: /hello.txt
3162
- if (/^([A-Z]{1,2}:\/?)\//i.test(filePathSlashes)) {
3163
- return true;
3164
- }
3165
- // Relative path: ./hello.txt
3166
- if (/^(\.\.?\/)+/i.test(filePathSlashes)) {
3167
- return true;
3168
- }
3169
- return false;
3170
- }
3171
-
3172
- /**
3173
- * Parses the knowledge command
4519
+ * @@@
3174
4520
  *
3175
- * @see ./KNOWLEDGE-README.md for more details
3176
- * @private within the commands folder
4521
+ * @private for `ModelVariant` and `modelCommandParser`
3177
4522
  */
3178
- var knowledgeCommandParser = {
3179
- /**
3180
- * Name of the command
3181
- */
3182
- name: 'KNOWLEDGE',
3183
- /**
3184
- * BOILERPLATE command can be used in:
3185
- */
3186
- usagePlaces: ['PIPELINE_HEAD'],
3187
- /**
3188
- * Description of the KNOWLEDGE command
3189
- */
3190
- description: "Tells promptbook which external knowledge to use",
3191
- /**
3192
- * Link to discussion
3193
- */
3194
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/41',
3195
- /**
3196
- * Example usages of the KNOWLEDGE command
3197
- */
3198
- examples: [
3199
- 'KNOWLEDGE https://www.pavolhejny.com/',
3200
- 'KNOWLEDGE ./hejny-cv.txt',
3201
- 'KNOWLEDGE ./hejny-cv.md',
3202
- 'KNOWLEDGE ./hejny-cv.pdf',
3203
- 'KNOWLEDGE ./hejny-cv.docx',
3204
- ],
3205
- /**
3206
- * Parses the KNOWLEDGE command
3207
- */
3208
- parse: function (input) {
3209
- var args = input.args;
3210
- var source = args[0];
3211
- if (source === undefined) {
3212
- throw new ParsingError("Source is not defined");
3213
- }
3214
- if (source.startsWith('http://')) {
3215
- throw new ParsingError("Source is not secure");
3216
- }
3217
- if (!(isValidFilePath(source) || isValidUrl(source))) {
3218
- throw new ParsingError("Source not valid");
3219
- }
3220
- if (source.startsWith('../') || source.startsWith('/') || /^[A-Z]:[\\/]+/i.test(source)) {
3221
- throw new ParsingError("Source cannot be outside of the .ptbk.md folder");
3222
- }
3223
- return {
3224
- type: 'KNOWLEDGE',
3225
- source: source,
3226
- };
3227
- },
3228
- };
3229
-
3230
4523
  var MODEL_VARIANTS = ['COMPLETION', 'CHAT', 'EMBEDDING' /* <- TODO [🏳] */ /* <- [🤖] */];
3231
- /**
3232
- * TODO: Maybe figure out better word than "variant"
3233
- * TODO: Add here more requirement options like max context size, max tokens, etc.
3234
- * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
3235
- */
3236
4524
 
3237
4525
  /**
3238
4526
  * Parses the model command
@@ -3248,7 +4536,11 @@ var modelCommandParser = {
3248
4536
  /**
3249
4537
  * BOILERPLATE command can be used in:
3250
4538
  */
3251
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
4539
+ usagePlaces: [
4540
+ 'PIPELINE_HEAD',
4541
+ // <- TODO: [🧠][❔] Should there be possibility to set MODEL for entire pipeline?
4542
+ 'PIPELINE_TEMPLATE',
4543
+ ],
3252
4544
  /**
3253
4545
  * Description of the MODEL command
3254
4546
  */
@@ -3256,7 +4548,7 @@ var modelCommandParser = {
3256
4548
  /**
3257
4549
  * Link to discussion
3258
4550
  */
3259
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
4551
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/67',
3260
4552
  /**
3261
4553
  * Example usages of the MODEL command
3262
4554
  */
@@ -3336,11 +4628,11 @@ var parameterCommandParser = {
3336
4628
  /**
3337
4629
  * Link to discussion
3338
4630
  */
3339
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
4631
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/68',
3340
4632
  /**
3341
4633
  * Example usages of the PARAMETER command
3342
4634
  */
3343
- examples: ['PARAMETER {title} Title of the book', 'OUTPUT PARAMETER {content} Content of the book'],
4635
+ examples: ['PARAMETER {title} Title of the book', 'OUTPUT PARAMETER {websiteContent} Content of the book'],
3344
4636
  /**
3345
4637
  * Parses the PARAMETER command
3346
4638
  */
@@ -3370,59 +4662,6 @@ var parameterCommandParser = {
3370
4662
  },
3371
4663
  };
3372
4664
 
3373
- /**
3374
- * Parses the persona command
3375
- *
3376
- * @see ./PERSONA-README.md for more details
3377
- * @private within the commands folder
3378
- */
3379
- var personaCommandParser = {
3380
- /**
3381
- * Name of the command
3382
- */
3383
- name: 'PERSONA',
3384
- /**
3385
- * Aliases for the PERSONA command
3386
- */
3387
- aliasNames: ['PERSON'],
3388
- /**
3389
- * PERSONA command can be used in:
3390
- */
3391
- usagePlaces: ['PIPELINE_HEAD', 'PIPELINE_TEMPLATE'],
3392
- /**
3393
- * Description of the PERSONA command
3394
- */
3395
- description: "Persona command is used to specify who the system is, it will be transformed into system message, top_t,...",
3396
- /**
3397
- * Link to discussion
3398
- */
3399
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/22',
3400
- /**
3401
- * Example usages of the PERSONA command
3402
- */
3403
- examples: ['PERSONA Jane, skilled copywriter', 'PERSONA Joe, male 28 years old, programmer'],
3404
- /**
3405
- * Parses the PERSONA command
3406
- */
3407
- parse: function (input) {
3408
- var rawArgs = input.rawArgs;
3409
- var _a = __read(rawArgs.split(/[,;:]/, 2), 2), personaNameRaw = _a[0], personaDescriptionRaw = _a[1];
3410
- var personaName = (personaNameRaw || '').trim();
3411
- if (personaName === '') {
3412
- throw new ParsingError("You must set name for the persona");
3413
- }
3414
- var personaDescription = (personaDescriptionRaw || '').trim();
3415
- if (personaDescription === '') {
3416
- personaDescription = null;
3417
- }
3418
- return {
3419
- type: 'PERSONA',
3420
- personaName: personaName,
3421
- personaDescription: personaDescription,
3422
- };
3423
- },
3424
- };
3425
-
3426
4665
  function isValidJavascriptName(javascriptName) {
3427
4666
  if (typeof javascriptName !== 'string') {
3428
4667
  return false;
@@ -3453,7 +4692,7 @@ var postprocessCommandParser = {
3453
4692
  /**
3454
4693
  * Link to discussion
3455
4694
  */
3456
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
4695
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/31',
3457
4696
  /**
3458
4697
  * Example usages of the POSTPROCESS command
3459
4698
  */
@@ -3492,11 +4731,8 @@ var promptbookVersionCommandParser = {
3492
4731
  /**
3493
4732
  * Name of the command
3494
4733
  */
3495
- name: 'VERSION',
3496
- /*
3497
- Note: [📇] No need to put here "PROMPTBOOK" alias here
3498
- aliasNames: ['PROMPTBOOK_VERSION'],
3499
- */
4734
+ name: 'PROMPTBOOK_VERSION',
4735
+ aliasNames: ['PTBK_VERSION', 'PTBK_V', 'PTBKV'],
3500
4736
  /**
3501
4737
  * BOILERPLATE command can be used in:
3502
4738
  */
@@ -3508,11 +4744,11 @@ var promptbookVersionCommandParser = {
3508
4744
  /**
3509
4745
  * Link to discussion
3510
4746
  */
3511
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
4747
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/69',
3512
4748
  /**
3513
4749
  * Example usages of the PROMPTBOOK_VERSION command
3514
4750
  */
3515
- examples: ["PROMPTBOOK VERSION ".concat(PROMPTBOOK_VERSION), "VERSION ".concat(PROMPTBOOK_VERSION)],
4751
+ examples: ["PROMPTBOOK VERSION ".concat(PROMPTBOOK_VERSION), "PTBKV ".concat(PROMPTBOOK_VERSION)],
3516
4752
  /**
3517
4753
  * Parses the PROMPTBOOK_VERSION command
3518
4754
  */
@@ -3546,8 +4782,9 @@ var urlCommandParser = {
3546
4782
  * Name of the command
3547
4783
  */
3548
4784
  name: 'URL',
4785
+ aliasNames: ['PIPELINE_URL'],
3549
4786
  /*
3550
- Note: [🛵] No need for alias name because it is already preprocessed
4787
+ Note: [🛵] No need for this alias name because it is already preprocessed
3551
4788
  aliasNames: ['HTTPS'],
3552
4789
  */
3553
4790
  /**
@@ -3561,11 +4798,12 @@ var urlCommandParser = {
3561
4798
  /**
3562
4799
  * Link to discussion
3563
4800
  */
3564
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
4801
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/70',
3565
4802
  /**
3566
4803
  * Example usages of the URL command
3567
4804
  */
3568
4805
  examples: [
4806
+ 'PIPELINE URL https://promptbook.studio/library/write-cv.ptbk.md',
3569
4807
  'URL https://promptbook.studio/library/write-cv.ptbk.md',
3570
4808
  'https://promptbook.studio/library/write-cv.ptbk.md',
3571
4809
  ],
@@ -3609,22 +4847,6 @@ var urlCommandParser = {
3609
4847
  },
3610
4848
  };
3611
4849
 
3612
- /**
3613
- * Returns the same value that is passed as argument.
3614
- * No side effects.
3615
- *
3616
- * Note: It can be usefull for leveling indentation
3617
- *
3618
- * @param value any values
3619
- * @returns the same values
3620
- */
3621
- function just(value) {
3622
- if (value === undefined) {
3623
- return undefined;
3624
- }
3625
- return value;
3626
- }
3627
-
3628
4850
  /**
3629
4851
  * Parses the action command
3630
4852
  *
@@ -3647,7 +4869,7 @@ var actionCommandParser = {
3647
4869
  /**
3648
4870
  * Link to discussion
3649
4871
  */
3650
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
4872
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/72',
3651
4873
  /**
3652
4874
  * Example usages of the ACTION command
3653
4875
  */
@@ -3656,7 +4878,8 @@ var actionCommandParser = {
3656
4878
  * Parses the ACTION command
3657
4879
  */
3658
4880
  parse: function (input) {
3659
- input.args;
4881
+ var args = input.args;
4882
+ TODO_USE(args);
3660
4883
  return {
3661
4884
  type: 'ACTION',
3662
4885
  };
@@ -3685,7 +4908,7 @@ var instrumentCommandParser = {
3685
4908
  /**
3686
4909
  * Link to discussion
3687
4910
  */
3688
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
4911
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/71',
3689
4912
  /**
3690
4913
  * Example usages of the INSTRUMENT command
3691
4914
  */
@@ -3694,7 +4917,8 @@ var instrumentCommandParser = {
3694
4917
  * Parses the INSTRUMENT command
3695
4918
  */
3696
4919
  parse: function (input) {
3697
- input.args;
4920
+ var args = input.args;
4921
+ TODO_USE(args);
3698
4922
  return {
3699
4923
  type: 'INSTRUMENT',
3700
4924
  };
@@ -3727,7 +4951,7 @@ var boilerplateCommandParser = {
3727
4951
  /**
3728
4952
  * Link to discussion
3729
4953
  */
3730
- discussionUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
4954
+ documentationUrl: 'https://github.com/webgptorg/promptbook/discussions/@@',
3731
4955
  /**
3732
4956
  * Example usages of the BOILERPLATE command
3733
4957
  */
@@ -3752,6 +4976,7 @@ var boilerplateCommandParser = {
3752
4976
  };
3753
4977
  /**
3754
4978
  * TODO: [💐] Implement BOILERPLATE command into `pipelineStringToJsonSync` function
4979
+ * Note: [⚪] This should never be in any released package
3755
4980
  */
3756
4981
 
3757
4982
  /**
@@ -3770,7 +4995,7 @@ var COMMANDS = [
3770
4995
  actionCommandParser,
3771
4996
  instrumentCommandParser,
3772
4997
  personaCommandParser,
3773
- boilerplateCommandParser, // <- TODO: !!!! Only in development, remove in production
4998
+ boilerplateCommandParser, // <- TODO: !! Only in development, remove in production
3774
4999
  ];
3775
5000
 
3776
5001
  /**
@@ -3817,10 +5042,6 @@ function parseCommand(raw, usagePlace) {
3817
5042
  .split(' ')
3818
5043
  .map(function (part) { return part.trim(); })
3819
5044
  .filter(function (item) { return item !== ''; })
3820
- // Note: [📇]:
3821
- .filter(function (item) { return !/^PTBK$/i.test(item); })
3822
- .filter(function (item) { return !/^PIPELINE$/i.test(item); })
3823
- .filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
3824
5045
  .map(removeMarkdownFormatting)
3825
5046
  .map(function (item) { return item.trim(); });
3826
5047
  if (items.length === 0 || items[0] === '') {
@@ -3856,18 +5077,18 @@ function parseCommand(raw, usagePlace) {
3856
5077
  }));
3857
5078
  }
3858
5079
  /**
3859
- * !!!
5080
+ * @@@
3860
5081
  */
3861
5082
  function getSupportedCommandsMessage() {
3862
5083
  return COMMANDS.flatMap(function (_a) {
3863
- var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, discussionUrl = _a.discussionUrl;
5084
+ var name = _a.name, aliasNames = _a.aliasNames, description = _a.description, documentationUrl = _a.documentationUrl;
3864
5085
  return __spreadArray([
3865
- "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(discussionUrl, ")")
5086
+ "- **".concat(name, "** ").concat(description, ", see [discussion](").concat(documentationUrl, ")")
3866
5087
  ], __read((aliasNames || []).map(function (aliasName) { return " - **".concat(aliasName, "** Alias for **").concat(name, "**"); })), false);
3867
5088
  }).join('\n');
3868
5089
  }
3869
5090
  /**
3870
- * !!!
5091
+ * @@@
3871
5092
  */
3872
5093
  function parseCommandVariant(input) {
3873
5094
  var e_1, _a;
@@ -3876,7 +5097,6 @@ function parseCommandVariant(input) {
3876
5097
  var _loop_1 = function (commandParser) {
3877
5098
  var name_1 = commandParser.name, aliasNames = commandParser.aliasNames, deprecatedNames = commandParser.deprecatedNames, parse = commandParser.parse;
3878
5099
  var names = __spreadArray(__spreadArray([name_1], __read((aliasNames || [])), false), __read((deprecatedNames || [])), false);
3879
- // console.log('!!!', { commandName, names });
3880
5100
  if (names.includes(commandName)) {
3881
5101
  try {
3882
5102
  return { value: parse({ usagePlace: usagePlace, raw: raw, rawArgs: rawArgs, normalized: normalized, args: args }) };
@@ -3986,25 +5206,42 @@ function extractAllBlocksFromMarkdown(markdown) {
3986
5206
  var e_1, _a;
3987
5207
  var codeBlocks = [];
3988
5208
  var lines = markdown.split('\n');
5209
+ // Note: [0] Ensure that the last block notated by gt > will be closed
5210
+ lines.push('');
3989
5211
  var currentCodeBlock = null;
3990
5212
  try {
3991
5213
  for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
3992
5214
  var line = lines_1_1.value;
5215
+ if (line.startsWith('> ') || line === '>') {
5216
+ if (currentCodeBlock === null) {
5217
+ currentCodeBlock = { blockNotation: '>', language: null, content: '' };
5218
+ } /* not else */
5219
+ if (currentCodeBlock.blockNotation === '>') {
5220
+ if (currentCodeBlock.content !== '') {
5221
+ currentCodeBlock.content += '\n';
5222
+ }
5223
+ currentCodeBlock.content += line.slice(2);
5224
+ }
5225
+ }
5226
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
5227
+ codeBlocks.push(currentCodeBlock);
5228
+ currentCodeBlock = null;
5229
+ }
5230
+ /* not else */
3993
5231
  if (line.startsWith('```')) {
3994
5232
  var language = line.slice(3).trim() || null;
3995
5233
  if (currentCodeBlock === null) {
3996
- currentCodeBlock = { language: language, content: '' };
5234
+ currentCodeBlock = { blockNotation: '```', language: language, content: '' };
3997
5235
  }
3998
5236
  else {
3999
5237
  if (language !== null) {
4000
- // [🌻]
4001
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
5238
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
4002
5239
  }
4003
5240
  codeBlocks.push(currentCodeBlock);
4004
5241
  currentCodeBlock = null;
4005
5242
  }
4006
5243
  }
4007
- else if (currentCodeBlock !== null) {
5244
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
4008
5245
  if (currentCodeBlock.content !== '') {
4009
5246
  currentCodeBlock.content += '\n';
4010
5247
  }
@@ -4020,11 +5257,13 @@ function extractAllBlocksFromMarkdown(markdown) {
4020
5257
  finally { if (e_1) throw e_1.error; }
4021
5258
  }
4022
5259
  if (currentCodeBlock !== null) {
4023
- // [🌻]
4024
- throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
5260
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
4025
5261
  }
4026
5262
  return codeBlocks;
4027
5263
  }
5264
+ /**
5265
+ * TODO: Maybe name for `blockNotation` instead of '```' and '>'
5266
+ */
4028
5267
 
4029
5268
  /**
4030
5269
  * Extracts exactly ONE code block from markdown.
@@ -4042,13 +5281,12 @@ function extractAllBlocksFromMarkdown(markdown) {
4042
5281
  function extractOneBlockFromMarkdown(markdown) {
4043
5282
  var codeBlocks = extractAllBlocksFromMarkdown(markdown);
4044
5283
  if (codeBlocks.length !== 1) {
4045
- // TODO: Report more specific place where the error happened
4046
- throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
5284
+ throw new ParsingError(spaceTrim(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 "); }));
4047
5285
  }
4048
5286
  return codeBlocks[0];
4049
5287
  }
4050
5288
  /***
4051
- * TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
5289
+ * TODO: [🍓][🌻] Decide of this is internal util, external util OR validator/postprocessor
4052
5290
  */
4053
5291
 
4054
5292
  /**
@@ -4058,13 +5296,13 @@ function parseMarkdownSection(value) {
4058
5296
  var _a, _b;
4059
5297
  var lines = value.split('\n');
4060
5298
  if (!lines[0].startsWith('#')) {
4061
- throw new Error('Markdown section must start with heading');
5299
+ throw new ParsingError('Markdown section must start with heading');
4062
5300
  }
4063
5301
  var title = lines[0].replace(/^#+\s*/, '');
4064
5302
  var level = (_b = (_a = lines[0].match(/^#+/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
4065
5303
  var content = spaceTrim(lines.slice(1).join('\n'));
4066
5304
  if (level < 1 || level > 6) {
4067
- throw new Error('Markdown section must have heading level between 1 and 6');
5305
+ throw new ParsingError('Markdown section must have heading level between 1 and 6');
4068
5306
  }
4069
5307
  return { title: title, level: level, content: content };
4070
5308
  }
@@ -4201,203 +5439,14 @@ function removeContentComments(content) {
4201
5439
  }
4202
5440
 
4203
5441
  /**
4204
- * Create difference set of two sets.
4205
- *
4206
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4207
- */
4208
- function difference(a, b, isEqual) {
4209
- var e_1, _a;
4210
- if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
4211
- var diff = new Set();
4212
- var _loop_1 = function (itemA) {
4213
- if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
4214
- diff.add(itemA);
4215
- }
4216
- };
4217
- try {
4218
- for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
4219
- var itemA = _c.value;
4220
- _loop_1(itemA);
4221
- }
4222
- }
4223
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4224
- finally {
4225
- try {
4226
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
4227
- }
4228
- finally { if (e_1) throw e_1.error; }
4229
- }
4230
- return diff;
4231
- }
4232
-
4233
- /**
4234
- * Creates a new set with all elements that are present in either set
4235
- *
4236
- * @deprecated use new javascript set methods instead @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4237
- */
4238
- function union() {
4239
- var e_1, _a, e_2, _b;
4240
- var sets = [];
4241
- for (var _i = 0; _i < arguments.length; _i++) {
4242
- sets[_i] = arguments[_i];
4243
- }
4244
- var union = new Set();
4245
- try {
4246
- for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
4247
- var set = sets_1_1.value;
4248
- try {
4249
- for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
4250
- var item = _d.value;
4251
- union.add(item);
4252
- }
4253
- }
4254
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4255
- finally {
4256
- try {
4257
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
4258
- }
4259
- finally { if (e_2) throw e_2.error; }
4260
- }
4261
- }
4262
- }
4263
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4264
- finally {
4265
- try {
4266
- if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
4267
- }
4268
- finally { if (e_1) throw e_1.error; }
4269
- }
4270
- return union;
4271
- }
4272
-
4273
- /**
4274
- * Parses the template and returns the list of all parameter names
4275
- *
4276
- * @param template the template with parameters in {curly} braces
4277
- * @returns the list of parameter names
4278
- */
4279
- function extractParameters(template) {
4280
- var e_1, _a;
4281
- var matches = template.matchAll(/{\w+}/g);
4282
- var parameterNames = new Set();
4283
- try {
4284
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
4285
- var match = matches_1_1.value;
4286
- var parameterName = match[0].slice(1, -1);
4287
- parameterNames.add(parameterName);
4288
- }
4289
- }
4290
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4291
- finally {
4292
- try {
4293
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
4294
- }
4295
- finally { if (e_1) throw e_1.error; }
4296
- }
4297
- return parameterNames;
4298
- }
4299
-
4300
- /**
4301
- * Parses the given script and returns the list of all used variables that are not defined in the script
4302
- *
4303
- * @param script from which to extract the variables
4304
- * @returns the list of variable names
4305
- * @throws {ParsingError} if the script is invalid
4306
- */
4307
- function extractVariables(script) {
4308
- var variables = new Set();
4309
- script = "(()=>{".concat(script, "})()");
4310
- try {
4311
- for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
4312
- try {
4313
- eval(script);
4314
- }
4315
- catch (error) {
4316
- if (!(error instanceof ReferenceError)) {
4317
- throw error;
4318
- }
4319
- var undefinedName = error.message.split(' ')[0];
4320
- /*
4321
- Note: Parsing the error
4322
- [ReferenceError: thing is not defined]
4323
- */
4324
- if (!undefinedName) {
4325
- throw error;
4326
- }
4327
- if (script.includes(undefinedName + '(')) {
4328
- script = "const ".concat(undefinedName, " = ()=>'';") + script;
4329
- }
4330
- else {
4331
- variables.add(undefinedName);
4332
- script = "const ".concat(undefinedName, " = '';") + script;
4333
- }
4334
- }
4335
- }
4336
- catch (error) {
4337
- if (!(error instanceof Error)) {
4338
- throw error;
4339
- }
4340
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
4341
- }
4342
- return variables;
4343
- }
4344
- /**
4345
- * TODO: [🔣] Support for multiple languages - python, java,...
4346
- */
4347
-
4348
- /**
4349
- * Parses the prompt template and returns the set of all used parameters
4350
- *
4351
- * @param promptTemplate the template with used parameters
4352
- * @returns the set of parameter names
4353
- * @throws {ParsingError} if the script is invalid
4354
- */
4355
- function extractParametersFromPromptTemplate(promptTemplate) {
4356
- var e_1, _a, e_2, _b;
4357
- var parameterNames = new Set();
4358
- try {
4359
- 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()) {
4360
- var parameterName = _d.value;
4361
- parameterNames.add(parameterName);
4362
- }
4363
- }
4364
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
4365
- finally {
4366
- try {
4367
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
4368
- }
4369
- finally { if (e_1) throw e_1.error; }
4370
- }
4371
- if (promptTemplate.blockType === 'SCRIPT') {
4372
- try {
4373
- for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
4374
- var parameterName = _f.value;
4375
- parameterNames.add(parameterName);
4376
- }
4377
- }
4378
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
4379
- finally {
4380
- try {
4381
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
4382
- }
4383
- finally { if (e_2) throw e_2.error; }
4384
- }
4385
- }
4386
- return parameterNames;
4387
- }
4388
- /**
4389
- * TODO: [🔣] If script require contentLanguage
4390
- */
4391
-
4392
- /**
4393
- * Compile promptbook from string (markdown) format to JSON format synchronously
5442
+ * Compile pipeline from string (markdown) format to JSON format synchronously
4394
5443
  *
4395
- * Note: There are two similar functions:
5444
+ * Note: There are 3 similar functions:
4396
5445
  * - `pipelineStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
4397
5446
  * - `pipelineStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
5447
+ * - `preparePipeline` - just one step in the compilation process
4398
5448
  *
4399
5449
  * @param pipelineString {Promptbook} in string markdown format (.ptbk.md)
4400
- * @param options - Options and tools for the compilation
4401
5450
  * @returns {Promptbook} compiled in JSON format (.ptbk.json)
4402
5451
  * @throws {ParsingError} if the promptbook string is not valid
4403
5452
  *
@@ -4413,7 +5462,10 @@ function pipelineStringToJsonSync(pipelineString) {
4413
5462
  description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
4414
5463
  parameters: [],
4415
5464
  promptTemplates: [],
4416
- knowledge: [],
5465
+ knowledgeSources: [],
5466
+ knowledgePieces: [],
5467
+ personas: [],
5468
+ preparations: [],
4417
5469
  };
4418
5470
  // =============================================================
4419
5471
  // Note: 1️⃣ Parsing of the markdown into object
@@ -4443,7 +5495,7 @@ function pipelineStringToJsonSync(pipelineString) {
4443
5495
  existingParameter.description &&
4444
5496
  existingParameter.description !== parameterDescription &&
4445
5497
  parameterDescription) {
4446
- throw new ParsingError(spaceTrim$1(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 "); }));
5498
+ throw new ParsingError(spaceTrim$1(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 "); }));
4447
5499
  }
4448
5500
  if (existingParameter) {
4449
5501
  if (parameterDescription) {
@@ -4462,11 +5514,12 @@ function pipelineStringToJsonSync(pipelineString) {
4462
5514
  // =============================================================
4463
5515
  // Note: 3️⃣ Process pipeline head
4464
5516
  pipelineJson.title = pipelineHead.title;
4465
- // TODO: [1] DRY description
5517
+ // TODO: [🎾][1] DRY description
4466
5518
  var description = pipelineHead.content;
4467
- // Note: Remove codeblocks - TODO: Maybe put this into util (exported from `@promptbool/utils`)
5519
+ // Note: Remove codeblocks - TODO: [🎾] Make util removeAllBlocksFromMarkdown (exported from `@promptbool/utils`)
4468
5520
  description = description.split(/^```.*^```/gms).join('');
4469
- //Note: Remove lists and return statement - TODO: Maybe put this into util (exported from `@promptbool/utils`)
5521
+ description = description.split(/^>.*$/gm).join('');
5522
+ //Note: Remove lists and return statement - TODO: [🎾] Make util (exported from `@promptbool/utils`)
4470
5523
  description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4471
5524
  description = spaceTrim$1(description);
4472
5525
  if (description === '') {
@@ -4494,7 +5547,7 @@ function pipelineStringToJsonSync(pipelineString) {
4494
5547
  pipelineJson.pipelineUrl = command.pipelineUrl.href;
4495
5548
  break;
4496
5549
  case 'KNOWLEDGE':
4497
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5550
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
4498
5551
  break;
4499
5552
  case 'ACTION':
4500
5553
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
@@ -4503,7 +5556,8 @@ function pipelineStringToJsonSync(pipelineString) {
4503
5556
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4504
5557
  break;
4505
5558
  case 'PERSONA':
4506
- console.error(new NotYetImplementedError('Personas are not implemented yet'));
5559
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: null });
5560
+ // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4507
5561
  break;
4508
5562
  case 'BOILERPLATE':
4509
5563
  throw new ParsingError('BOILERPLATE command is only for testing purposes and should not be used in the .ptbk.md file'); // <- TODO: [🚞]
@@ -4526,29 +5580,83 @@ function pipelineStringToJsonSync(pipelineString) {
4526
5580
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
4527
5581
  var templateModelRequirements = __assign({}, defaultModelRequirements);
4528
5582
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
4529
- var dependentParameterNames = new Set();
4530
- var blockType = 'PROMPT_TEMPLATE';
4531
- var jokers = [];
4532
- var postprocessing = [];
4533
- var expectAmount = {};
4534
- var expectFormat = undefined;
4535
- var isBlockTypeChanged = false;
5583
+ var lastLine = section.content.split('\n').pop();
5584
+ var resultingParameterNameMatch = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
5585
+ var resultingParameterName = null;
5586
+ if (resultingParameterNameMatch &&
5587
+ resultingParameterNameMatch.groups !== undefined &&
5588
+ resultingParameterNameMatch.groups.resultingParamName !== undefined) {
5589
+ resultingParameterName = resultingParameterNameMatch.groups.resultingParamName;
5590
+ }
5591
+ var expectResultingParameterName = function () {
5592
+ if (resultingParameterName !== null) {
5593
+ return resultingParameterName;
5594
+ }
5595
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Template section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
5596
+ // TODO: Show code of invalid sections each time + DRY
5597
+ section.content
5598
+ .split('\n')
5599
+ .map(function (line) { return " | ".concat(line); } /* <- TODO: [🚞] */)
5600
+ .join('\n')), "\n "); }));
5601
+ };
5602
+ var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
5603
+ // TODO: [🎾][1] DRY description
5604
+ var description_1 = section.content;
5605
+ // Note: Remove codeblocks - TODO: [🎾]
5606
+ description_1 = description_1.split(/^```.*^```/gms).join('');
5607
+ description_1 = description_1.split(/^>.*$/gm).join('');
5608
+ //Note: Remove lists and return statement - TODO: [🎾]
5609
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5610
+ description_1 = spaceTrim$1(description_1);
5611
+ if (description_1 === '') {
5612
+ description_1 = undefined;
5613
+ }
5614
+ var templateJson = {
5615
+ blockType: 'PROMPT_TEMPLATE',
5616
+ name: titleToName(section.title),
5617
+ title: section.title,
5618
+ description: description_1,
5619
+ modelRequirements: templateModelRequirements,
5620
+ content: content,
5621
+ };
5622
+ /**
5623
+ * This is nessesary because block type can be
5624
+ * - Set zero times, so anticipate 'PROMPT_TEMPLATE'
5625
+ * - Set one time
5626
+ * - Set more times - throw error
5627
+ *
5628
+ * Note: [2]
5629
+ */
5630
+ var isBlockTypeSet = false;
4536
5631
  try {
4537
5632
  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()) {
4538
5633
  var listItem = listItems_2_1.value;
4539
5634
  var command = parseCommand(listItem, 'PIPELINE_TEMPLATE');
5635
+ // TODO [🍧][♓️] List commands and before apply order them
4540
5636
  switch (command.type) {
4541
5637
  // TODO: [🍧] Use here applyToPipelineJson and remove switch statement
4542
5638
  case 'BLOCK':
4543
- if (isBlockTypeChanged) {
5639
+ if (isBlockTypeSet) {
4544
5640
  throw new ParsingError('Block type is already defined in the prompt template. It can be defined only once.');
4545
5641
  }
4546
5642
  if (command.blockType === 'SAMPLE') {
4547
- console.error(new NotYetImplementedError('Block type SAMPLE is not implemented yet'));
5643
+ expectResultingParameterName();
5644
+ var parameter = pipelineJson.parameters.find(function (param) { return param.name === resultingParameterName; });
5645
+ if (parameter === undefined) {
5646
+ throw new UnexpectedError("Can not find parameter {".concat(resultingParameterName, "} to assign sample value"));
5647
+ }
5648
+ parameter.sampleValues = parameter.sampleValues || [];
5649
+ parameter.sampleValues.push(content);
4548
5650
  return "continue-templates";
4549
5651
  }
4550
5652
  if (command.blockType === 'KNOWLEDGE') {
4551
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5653
+ knowledgeCommandParser.applyToPipelineJson({
5654
+ type: 'KNOWLEDGE',
5655
+ source: content, // <- TODO: [🐝] !!! Work with KNOWLEDGE which not referring to the source file or website, but its content itself
5656
+ }, {
5657
+ pipelineJson: pipelineJson,
5658
+ templateJson: templateJson,
5659
+ });
4552
5660
  return "continue-templates";
4553
5661
  }
4554
5662
  if (command.blockType === 'ACTION') {
@@ -4559,35 +5667,37 @@ function pipelineStringToJsonSync(pipelineString) {
4559
5667
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4560
5668
  return "continue-templates";
4561
5669
  }
4562
- blockType = command.blockType;
4563
- isBlockTypeChanged = true;
5670
+ expectResultingParameterName();
5671
+ templateJson.blockType = command.blockType;
5672
+ isBlockTypeSet = true; //<- Note: [2]
4564
5673
  break;
4565
5674
  case 'EXPECT_AMOUNT':
4566
5675
  // eslint-disable-next-line no-case-declarations
4567
5676
  var unit = command.unit.toLowerCase();
4568
- expectAmount[unit] = expectAmount[unit] || {};
5677
+ templateJson.expectations = templateJson.expectations || {};
5678
+ templateJson.expectations[unit] = templateJson.expectations[unit] || {};
4569
5679
  if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
4570
- if (expectAmount[unit].min !== undefined) {
4571
- throw new ParsingError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5680
+ if (templateJson.expectations[unit].min !== undefined) {
5681
+ throw new ParsingError("Already defined minumum ".concat(templateJson.expectations[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4572
5682
  }
4573
- expectAmount[unit].min = command.amount;
5683
+ templateJson.expectations[unit].min = command.amount;
4574
5684
  } /* not else */
4575
5685
  if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
4576
- if (expectAmount[unit].max !== undefined) {
4577
- throw new ParsingError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
5686
+ if (templateJson.expectations[unit].max !== undefined) {
5687
+ throw new ParsingError("Already defined maximum ".concat(templateJson.expectations[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
4578
5688
  }
4579
- expectAmount[unit].max = command.amount;
5689
+ templateJson.expectations[unit].max = command.amount;
4580
5690
  }
4581
5691
  break;
4582
5692
  case 'EXPECT_FORMAT':
4583
- if (expectFormat !== undefined && command.format !== expectFormat) {
4584
- throw new ParsingError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
5693
+ if (templateJson.expectFormat !== undefined && command.format !== templateJson.expectFormat) {
5694
+ throw new ParsingError(spaceTrim$1("\n Expect format is already defined to \"".concat(templateJson.expectFormat, "\".\n Now you try to redefine it by \"").concat(command.format, "\".\n ")));
4585
5695
  }
4586
- expectFormat = command.format;
5696
+ templateJson.expectFormat = command.format;
4587
5697
  break;
4588
5698
  case 'JOKER':
4589
- jokers.push(command.parameterName);
4590
- dependentParameterNames.add(command.parameterName);
5699
+ templateJson.jokerParameterNames = templateJson.jokerParameterNames || [];
5700
+ templateJson.jokerParameterNames.push(command.parameterName);
4591
5701
  break;
4592
5702
  case 'MODEL':
4593
5703
  templateModelRequirements[command.key] = command.value;
@@ -4597,19 +5707,24 @@ function pipelineStringToJsonSync(pipelineString) {
4597
5707
  defineParam(command);
4598
5708
  break;
4599
5709
  case 'POSTPROCESS':
4600
- postprocessing.push(command.functionName);
5710
+ templateJson.postprocessingFunctionNames = templateJson.postprocessingFunctionNames || [];
5711
+ templateJson.postprocessingFunctionNames.push(command.functionName);
4601
5712
  break;
4602
5713
  case 'KNOWLEDGE':
4603
- console.error(new NotYetImplementedError('Knowledge is not implemented yet'));
5714
+ // TODO: [👙] The knowledge is maybe relevant for just this template
5715
+ knowledgeCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
4604
5716
  break;
4605
5717
  case 'ACTION':
5718
+ // TODO: [👙] The action is maybe relevant for just this template
4606
5719
  console.error(new NotYetImplementedError('Actions are not implemented yet'));
4607
5720
  break;
4608
5721
  case 'INSTRUMENT':
5722
+ // TODO: [👙] The instrument is maybe relevant for just this template
4609
5723
  console.error(new NotYetImplementedError('Instruments are not implemented yet'));
4610
5724
  break;
4611
5725
  case 'PERSONA':
4612
- console.error(new NotYetImplementedError('Personas are not implemented yet'));
5726
+ personaCommandParser.applyToPipelineJson(command, { pipelineJson: pipelineJson, templateJson: templateJson });
5727
+ // <- Note: Prototype of [🍧] (remove this comment after full implementation)
4613
5728
  break;
4614
5729
  case 'BOILERPLATE':
4615
5730
  console.error(new ParsingError('BOILERPLATE command is only for testing purposes and should not be used in the .ptbk.md file'));
@@ -4627,70 +5742,29 @@ function pipelineStringToJsonSync(pipelineString) {
4627
5742
  }
4628
5743
  finally { if (e_3) throw e_3.error; }
4629
5744
  }
4630
- var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
4631
- if (blockType === 'SCRIPT') {
5745
+ // TODO: [🍧] Should be done in BLOCK command
5746
+ if (templateJson.blockType === 'SCRIPT') {
4632
5747
  if (!language) {
4633
5748
  throw new ParsingError('You must specify the language of the script in the prompt template');
4634
5749
  }
4635
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5750
+ if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
4636
5751
  throw new ParsingError(spaceTrim$1(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 "); }));
4637
5752
  }
5753
+ templateJson.contentLanguage = language;
4638
5754
  }
4639
- var lastLine = section.content.split('\n').pop();
4640
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
4641
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
4642
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Each section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
4643
- // TODO: Show code of invalid sections each time + DRY
4644
- section.content
4645
- .split('\n')
4646
- .map(function (line) { return " | ".concat(line); } /* <- TODO: [🚞] */)
4647
- .join('\n')), "\n "); }));
4648
- }
4649
- var resultingParameterName = match.groups.resultingParamName;
4650
- // TODO: [1] DRY description
4651
- var description_1 = section.content;
4652
- // Note: Remove codeblocks
4653
- description_1 = description_1.split(/^```.*^```/gms).join('');
4654
- //Note: Remove lists and return statement
4655
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
4656
- description_1 = spaceTrim$1(description_1);
4657
- if (description_1 === '') {
4658
- description_1 = undefined;
4659
- }
4660
- if (Object.keys(jokers).length === 0) {
4661
- jokers = undefined;
4662
- }
4663
- if (Object.keys(expectAmount).length === 0) {
4664
- expectAmount = undefined;
4665
- }
4666
- if (Object.keys(postprocessing).length === 0) {
4667
- postprocessing = undefined;
4668
- }
4669
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, blockType: blockType, content: content })));
5755
+ // TODO: [🍧][❔] Should be done in BLOCK command
4670
5756
  if (templateModelRequirements.modelVariant === undefined) {
4671
5757
  templateModelRequirements.modelVariant = 'CHAT';
4672
5758
  }
4673
- dependentParameterNames = difference(dependentParameterNames, new Set(RESERVED_PARAMETER_NAMES));
4674
- var template = {
4675
- name: titleToName(section.title),
4676
- title: section.title,
4677
- description: description_1,
4678
- dependentParameterNames: Array.from(dependentParameterNames),
4679
- blockType: blockType,
4680
- jokers: jokers,
4681
- postprocessing: postprocessing,
4682
- expectations: expectAmount,
4683
- expectFormat: expectFormat,
4684
- modelRequirements: templateModelRequirements,
4685
- contentLanguage: blockType === 'SCRIPT' ? language : undefined,
4686
- content: content,
4687
- resultingParameterName: resultingParameterName,
4688
- };
4689
- if (blockType !== 'PROMPT_TEMPLATE') {
4690
- delete template.modelRequirements;
5759
+ templateJson.dependentParameterNames = Array.from(extractParameterNamesFromPromptTemplate(templateJson));
5760
+ // TODO: [🍧][❔] Remove this condition - modelRequirements should be put here via BLOCK command not removed when PROMPT_TEMPLATE
5761
+ if (templateJson.blockType !== 'PROMPT_TEMPLATE') {
5762
+ delete templateJson.modelRequirements;
4691
5763
  }
5764
+ // TODO: [🍧] Make this better - for example each command parser can call and apply this
5765
+ templateJson.resultingParameterName = expectResultingParameterName( /* <- Note: This is once more redundant */);
4692
5766
  // TODO: [🍧] What actually about preparation and pushing the block into `promptTemplates`
4693
- pipelineJson.promptTemplates.push(template /* <- !!! */);
5767
+ pipelineJson.promptTemplates.push(templateJson);
4694
5768
  };
4695
5769
  try {
4696
5770
  // =============================================================
@@ -4757,14 +5831,18 @@ function pipelineStringToJsonSync(pipelineString) {
4757
5831
  * TODO: Use spaceTrim more effectively
4758
5832
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
4759
5833
  * TODO: [🥞] Not optimal parsing because `splitMarkdownIntoSections` is executed twice with same string, once through `flattenMarkdown` and second directly here
5834
+ * TODO: [♈] Probbably move expectations from templates to parameters
5835
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
5836
+ * TODO: [🍙] Make some standart order of json properties
4760
5837
  */
4761
5838
 
4762
5839
  /**
4763
- * Compile promptbook from string (markdown) format to JSON format
5840
+ * Compile pipeline from string (markdown) format to JSON format
4764
5841
  *
4765
- * Note: There are two similar functions:
5842
+ * Note: There are 3 similar functions:
4766
5843
  * - `pipelineStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
4767
5844
  * - `pipelineStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
5845
+ * - `preparePipeline` - just one step in the compilation process
4768
5846
  *
4769
5847
  * @param pipelineString {Promptbook} in string markdown format (.ptbk.md)
4770
5848
  * @param options - Options and tools for the compilation
@@ -4775,22 +5853,18 @@ function pipelineStringToJsonSync(pipelineString) {
4775
5853
  * Note: This function acts as compilation process
4776
5854
  */
4777
5855
  function pipelineStringToJson(pipelineString, options) {
4778
- if (options === void 0) { options = {}; }
5856
+ if (options === void 0) { options = { llmTools: null }; }
4779
5857
  return __awaiter(this, void 0, void 0, function () {
4780
- var llmTools, pipelineJson, knowledge;
5858
+ var llmTools, pipelineJson;
4781
5859
  return __generator(this, function (_a) {
4782
5860
  switch (_a.label) {
4783
5861
  case 0:
4784
5862
  llmTools = options.llmTools;
4785
5863
  pipelineJson = pipelineStringToJsonSync(pipelineString);
4786
- if (!llmTools) return [3 /*break*/, 2];
4787
- return [4 /*yield*/, prepareKnowledgeFromMarkdown({
4788
- content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
4789
- llmTools: llmTools,
4790
- })];
5864
+ if (!(llmTools !== null)) return [3 /*break*/, 2];
5865
+ return [4 /*yield*/, preparePipeline(pipelineJson, { llmTools: llmTools })];
4791
5866
  case 1:
4792
- knowledge = _a.sent();
4793
- pipelineJson = __assign(__assign({}, pipelineJson), { knowledge: __spreadArray(__spreadArray([], __read((pipelineJson.knowledge || [])), false), __read(knowledge), false) });
5867
+ pipelineJson = _a.sent();
4794
5868
  _a.label = 2;
4795
5869
  case 2: return [2 /*return*/, pipelineJson];
4796
5870
  }
@@ -4799,12 +5873,13 @@ function pipelineStringToJson(pipelineString, options) {
4799
5873
  }
4800
5874
  /**
4801
5875
  * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
5876
+ * TODO: [🛠] Actions, instruments (and maybe knowledge) => Functions and tools
4802
5877
  */
4803
5878
 
4804
5879
  /**
4805
5880
  * Add or modify an auto-generated section in a markdown file
4806
5881
  *
4807
- * @private within the package
5882
+ * @private within the repository
4808
5883
  */
4809
5884
  function addAutoGeneratedSection(content, options) {
4810
5885
  var sectionName = options.sectionName, sectionContent = options.sectionContent;
@@ -4816,7 +5891,10 @@ function addAutoGeneratedSection(content, options) {
4816
5891
  }
4817
5892
  var placeForSection = removeContentComments(content).match(/^##.*$/im);
4818
5893
  if (!placeForSection) {
4819
- throw new Error("No place where to put the section <!--".concat(sectionName, "-->"));
5894
+ throw new ParsingError(
5895
+ // <- [🧠] Maybe something better than `ParsingError`
5896
+ "No place where to put the section <!--".concat(sectionName, "-->"));
5897
+ // <- [🚞]
4820
5898
  }
4821
5899
  var _a = __read(placeForSection, 1), heading = _a[0];
4822
5900
  return content.replace(heading, "<!--".concat(sectionName, "-->\n").concat(warningLine, "\n").concat(sectionContent, "\n<!--/").concat(sectionName, "-->\n\n").concat(heading));
@@ -4973,6 +6051,28 @@ function prettifyPipelineString(pipelineString, options) {
4973
6051
  * TODO: [🕌] When more than 2 functionalities, split into separate functions
4974
6052
  */
4975
6053
 
6054
+ /**
6055
+ * Stringify the PipelineJson with proper formatting
6056
+ *
6057
+ * Note: [0] It can be used for more JSON types like whole collection of pipelines, single knowledge piece, etc.
6058
+ * Note: In contrast to JSON.stringify, this function ensures that **embedding index** is on single line
6059
+ */
6060
+ function stringifyPipelineJson(pipeline) {
6061
+ var pipelineJsonStringified = JSON.stringify(pipeline, null, 4);
6062
+ for (var i = 0; i < LOOP_LIMIT; i++) {
6063
+ pipelineJsonStringified = pipelineJsonStringified.replace(/(-?0\.\d+),[\n\s]+(-?0\.\d+)/gms, "$1".concat(REPLACING_NONCE, "$2"));
6064
+ }
6065
+ pipelineJsonStringified = pipelineJsonStringified.split(REPLACING_NONCE).join(', ');
6066
+ pipelineJsonStringified += '\n';
6067
+ return pipelineJsonStringified;
6068
+ }
6069
+ /**
6070
+ * TODO: [🐝] Not Working propperly @see https://promptbook.studio/samples/mixed-knowledge.ptbk.md
6071
+ * TODO: [🧠][0] Maybe rename to `stringifyPipelineJson`, `stringifyIndexedJson`,...
6072
+ * TODO: [🧠] Maybe more elegant solution than replacing via regex
6073
+ * TODO: [🍙] Make some standart order of json properties
6074
+ */
6075
+
4976
6076
  /**
4977
6077
  * This error indicates that the pipeline collection cannot be propperly loaded
4978
6078
  */
@@ -4987,6 +6087,20 @@ var CollectionError = /** @class */ (function (_super) {
4987
6087
  return CollectionError;
4988
6088
  }(Error));
4989
6089
 
6090
+ /**
6091
+ * This error type indicates that the version of the pipeline is not matching the expected version
6092
+ */
6093
+ var VersionMismatchError = /** @class */ (function (_super) {
6094
+ __extends(VersionMismatchError, _super);
6095
+ function VersionMismatchError(message, expectedVersion) {
6096
+ var _this = _super.call(this, spaceTrim$1(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;
6097
+ _this.name = 'UnexpectedError';
6098
+ Object.setPrototypeOf(_this, VersionMismatchError.prototype);
6099
+ return _this;
6100
+ }
6101
+ return VersionMismatchError;
6102
+ }(Error));
6103
+
4990
6104
  /**
4991
6105
  * Pretty print an embedding vector for logging
4992
6106
  */
@@ -5013,6 +6127,29 @@ function usageToWorktime(usage) {
5013
6127
  return uncertainNumber;
5014
6128
  }
5015
6129
 
6130
+ /**
6131
+ * Function `usageToHuman` will take usage and convert it to human readable report
6132
+ */
6133
+ function usageToHuman(usage) {
6134
+ var report = 'Usage:';
6135
+ var uncertainNumberToHuman = function (_a) {
6136
+ var value = _a.value, isUncertain = _a.isUncertain;
6137
+ return "".concat(isUncertain ? 'approximately ' : '').concat(Math.round(value * 100) / 100);
6138
+ };
6139
+ report += '\n' + "- Cost ".concat(uncertainNumberToHuman(usage.price), " USD");
6140
+ report += '\n' + "- Saved ".concat(uncertainNumberToHuman(usageToWorktime(usage)), " hours of human time");
6141
+ return spaceTrim(report);
6142
+ }
6143
+ /**
6144
+ * TODO: Use "$1" not "1 USD"
6145
+ * TODO: Use markdown formatting like "Cost approximately **$1**"
6146
+ * TODO: Report in minutes, seconds, days NOT 0.1 hours
6147
+ * TODO: [🧠] Maybe make from `uncertainNumberToHuman` separate exported utility
6148
+ * TODO: When negligible usage, report "Negligible" or just don't report it
6149
+ * TODO: [🧠] Maybe use "~" instead of "approximately"
6150
+ * TODO: [🏛] Maybe make some markdown builder
6151
+ */
6152
+
5016
6153
  /**
5017
6154
  * Delagates the user interaction to a async callback function
5018
6155
  * You need to provide your own implementation of this callback function and its bind to UI.
@@ -5074,6 +6211,9 @@ var SimplePromptInterfaceTools = /** @class */ (function () {
5074
6211
  };
5075
6212
  return SimplePromptInterfaceTools;
5076
6213
  }());
6214
+ /**
6215
+ * Note: [🔵] This code should never be published outside of `@promptbook/browser`
6216
+ */
5077
6217
 
5078
6218
  /**
5079
6219
  * Default options for generating an execution report string
@@ -5086,7 +6226,7 @@ var ExecutionReportStringOptionsDefaults = {
5086
6226
  /**
5087
6227
  * Format either small or big number
5088
6228
  *
5089
- * @private within the package
6229
+ * @private within the repository
5090
6230
  */
5091
6231
  function formatNumber(value) {
5092
6232
  if (value === 0) {
@@ -5106,7 +6246,7 @@ function formatNumber(value) {
5106
6246
  /**
5107
6247
  * Create a markdown table from a 2D array of strings
5108
6248
  *
5109
- * @private within the package
6249
+ * @private within the repository
5110
6250
  */
5111
6251
  function createMarkdownTable(table) {
5112
6252
  var columnWidths = table.reduce(function (widths, row) {
@@ -5134,7 +6274,7 @@ function createMarkdownTable(table) {
5134
6274
  /**
5135
6275
  * Function createMarkdownChart will draw a chart in markdown from ⬛+🟦 tiles
5136
6276
  *
5137
- * @private within the package
6277
+ * @private within the repository
5138
6278
  */
5139
6279
  function createMarkdownChart(options) {
5140
6280
  var e_1, _a;
@@ -5187,7 +6327,7 @@ var MOMENT_ARG_THRESHOLDS = {
5187
6327
  /**
5188
6328
  * Count the duration of working time
5189
6329
  *
5190
- * @private within the package
6330
+ * @private within the repository
5191
6331
  */
5192
6332
  function countWorkingDuration(items) {
5193
6333
  var e_1, _a;
@@ -5344,10 +6484,13 @@ function executionReportJsonToString(executionReportJson, options) {
5344
6484
  if (just(true)) {
5345
6485
  executionReportString +=
5346
6486
  '\n\n\n\n' +
5347
- spaceTrim$1(function (block) { return "\n\n ### Prompt\n\n ```\n ".concat(block(escapeMarkdownBlock(promptExecution.prompt.content)), "\n ```\n\n "); });
6487
+ spaceTrim$1(function (block) {
6488
+ var _a;
6489
+ 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 ");
6490
+ });
5348
6491
  }
5349
6492
  if (promptExecution.result && promptExecution.result.content) {
5350
- executionReportString += '\n\n\n\n' + '### Result';
6493
+ executionReportString += '\n\n\n\n' + '### Result' + '\n\n';
5351
6494
  if (promptExecution.result === undefined) {
5352
6495
  executionReportString += '*No result*';
5353
6496
  }
@@ -5385,5 +6528,5 @@ function executionReportJsonToString(executionReportJson, options) {
5385
6528
  * TODO: [🧠] Allow to filter out some parts of the report by options
5386
6529
  */
5387
6530
 
5388
- export { BlockTypes, CallbackInterfaceTools, CollectionError, ExecutionReportStringOptionsDefaults, ExpectError, NotFoundError, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, SimplePromptInterfaceTools, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, collectionToJson, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, joinLlmExecutionTools, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, prettifyPipelineString, usageToWorktime, validatePipeline };
6531
+ export { BlockTypes, CallbackInterfaceTools, CollectionError, ExecutionReportStringOptionsDefaults, ExpectError, NotFoundError, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, SimplePromptInterfaceTools, UnexpectedError, VersionMismatchError, addUsage, assertsExecutionSuccessful, checkExpectations, collectionToJson, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, joinLlmExecutionTools, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, preparePipeline, prettifyPipelineString, stringifyPipelineJson, unpreparePipeline, usageToHuman, usageToWorktime, validatePipeline };
5389
6532
  //# sourceMappingURL=index.es.js.map