@promptbook/node 0.59.0-3 → 0.59.0-31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.es.js +2540 -1592
- package/esm/index.es.js.map +1 -1
- package/esm/typings/promptbook-library/index.d.ts +25 -0
- package/esm/typings/src/_packages/cli.index.d.ts +10 -0
- package/{umd/typings → esm/typings/src}/_packages/core.index.d.ts +11 -9
- package/esm/typings/src/_packages/node.index.d.ts +4 -0
- package/{umd/typings → esm/typings/src}/_packages/types.index.d.ts +4 -3
- package/esm/typings/src/cli/actions/hello.d.ts +7 -0
- package/esm/typings/src/cli/actions/make.d.ts +7 -0
- package/esm/typings/src/cli/actions/prettify.d.ts +7 -0
- package/esm/typings/{conversion/prettify/prettifyPromptbookStringCli.d.ts → src/cli/promptbookCli.d.ts} +2 -2
- package/esm/typings/{config.d.ts → src/config.d.ts} +4 -0
- package/{umd/typings/conversion/validation/validatePromptbookJson.d.ts → esm/typings/src/conversion/validation/validatePromptbook.d.ts} +2 -2
- package/esm/typings/src/execution/EmbeddingVector.d.ts +8 -0
- package/esm/typings/{execution → src/execution}/PromptResult.d.ts +11 -0
- package/esm/typings/src/execution/embeddingVectorToString.d.ts +5 -0
- package/esm/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +21 -0
- package/{umd/typings → esm/typings/src}/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/esm/typings/{promptbook-library → src/library}/SimplePromptbookLibrary.d.ts +3 -1
- package/esm/typings/{promptbook-library/constructors/createPromptbookLibraryFromDirectory.d.ts → src/library/constructors/createLibraryFromDirectory.d.ts} +2 -2
- package/esm/typings/src/library/constructors/createLibraryFromJson.d.ts +12 -0
- package/esm/typings/{promptbook-library/constructors/createPromptbookLibraryFromPromise.d.ts → src/library/constructors/createLibraryFromPromise.d.ts} +3 -7
- package/{umd/typings/promptbook-library/constructors/createPromptbookLibraryFromUrl.d.ts → esm/typings/src/library/constructors/createLibraryFromUrl.d.ts} +2 -2
- package/esm/typings/{promptbook-library/constructors/createPromptbookSublibrary.d.ts → src/library/constructors/createSublibrary.d.ts} +1 -1
- package/esm/typings/src/library/libraryToJson.d.ts +8 -0
- package/esm/typings/{llm-providers → src/llm-providers}/anthropic-claude/anthropic-claude-models.d.ts +1 -0
- package/{umd/typings → esm/typings/src}/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/{umd/typings → esm/typings/src}/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/{umd/typings → esm/typings/src}/llm-providers/openai/OpenAiExecutionTools.d.ts +9 -0
- package/{umd/typings → esm/typings/src}/llm-providers/openai/computeOpenaiUsage.d.ts +4 -2
- package/{umd/typings → esm/typings/src}/llm-providers/openai/openai-models.d.ts +1 -0
- package/{umd/typings → esm/typings/src}/llm-providers/remote/interfaces/RemoteServerOptions.d.ts +1 -1
- package/esm/typings/{llm-providers → src/llm-providers}/remote/startRemoteServer.d.ts +1 -1
- package/{umd/typings → esm/typings/src}/types/ModelRequirements.d.ts +1 -1
- package/esm/typings/{types → src/types}/PromptbookJson/MaterialKnowledgePieceJson.d.ts +3 -2
- package/package.json +4 -3
- package/umd/index.umd.js +2544 -1596
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/promptbook-library/index.d.ts +25 -0
- package/umd/typings/src/_packages/cli.index.d.ts +10 -0
- package/{esm/typings → umd/typings/src}/_packages/core.index.d.ts +11 -9
- package/umd/typings/src/_packages/node.index.d.ts +4 -0
- package/{esm/typings → umd/typings/src}/_packages/types.index.d.ts +4 -3
- package/umd/typings/src/cli/actions/hello.d.ts +7 -0
- package/umd/typings/src/cli/actions/make.d.ts +7 -0
- package/umd/typings/src/cli/actions/prettify.d.ts +7 -0
- package/umd/typings/{conversion/prettify/prettifyPromptbookStringCli.d.ts → src/cli/promptbookCli.d.ts} +2 -2
- package/umd/typings/{config.d.ts → src/config.d.ts} +4 -0
- package/{esm/typings/conversion/validation/validatePromptbookJson.d.ts → umd/typings/src/conversion/validation/validatePromptbook.d.ts} +2 -2
- package/umd/typings/src/execution/EmbeddingVector.d.ts +8 -0
- package/umd/typings/{execution → src/execution}/PromptResult.d.ts +11 -0
- package/umd/typings/src/execution/embeddingVectorToString.d.ts +5 -0
- package/umd/typings/src/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +21 -0
- package/{esm/typings → umd/typings/src}/knowledge/prepare-knowledge/pdf/prepareKnowledgeFromPdf.d.ts +3 -0
- package/umd/typings/{promptbook-library → src/library}/SimplePromptbookLibrary.d.ts +3 -1
- package/umd/typings/{promptbook-library/constructors/createPromptbookLibraryFromDirectory.d.ts → src/library/constructors/createLibraryFromDirectory.d.ts} +2 -2
- package/umd/typings/src/library/constructors/createLibraryFromJson.d.ts +12 -0
- package/umd/typings/{promptbook-library/constructors/createPromptbookLibraryFromPromise.d.ts → src/library/constructors/createLibraryFromPromise.d.ts} +3 -7
- package/{esm/typings/promptbook-library/constructors/createPromptbookLibraryFromUrl.d.ts → umd/typings/src/library/constructors/createLibraryFromUrl.d.ts} +2 -2
- package/umd/typings/{promptbook-library/constructors/createPromptbookSublibrary.d.ts → src/library/constructors/createSublibrary.d.ts} +1 -1
- package/umd/typings/src/library/libraryToJson.d.ts +8 -0
- package/umd/typings/{llm-providers → src/llm-providers}/anthropic-claude/anthropic-claude-models.d.ts +1 -0
- package/{esm/typings → umd/typings/src}/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -0
- package/{esm/typings → umd/typings/src}/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -0
- package/{esm/typings → umd/typings/src}/llm-providers/openai/OpenAiExecutionTools.d.ts +9 -0
- package/{esm/typings → umd/typings/src}/llm-providers/openai/computeOpenaiUsage.d.ts +4 -2
- package/{esm/typings → umd/typings/src}/llm-providers/openai/openai-models.d.ts +1 -0
- package/{esm/typings → umd/typings/src}/llm-providers/remote/interfaces/RemoteServerOptions.d.ts +1 -1
- package/umd/typings/{llm-providers → src/llm-providers}/remote/startRemoteServer.d.ts +1 -1
- package/{esm/typings → umd/typings/src}/types/ModelRequirements.d.ts +1 -1
- package/umd/typings/{types → src/types}/PromptbookJson/MaterialKnowledgePieceJson.d.ts +3 -2
- package/umd/typings/src/utils/unwrapResult.test.d.ts +1 -0
- package/umd/typings/src/utils/validators/url/isValidUrl.test.d.ts +1 -0
- package/esm/typings/_packages/cli.index.d.ts +0 -10
- package/esm/typings/_packages/node.index.d.ts +0 -4
- package/esm/typings/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +0 -7
- package/esm/typings/promptbook-library/constructors/createPromptbookLibraryFromSources.d.ts +0 -16
- package/umd/typings/_packages/cli.index.d.ts +0 -10
- package/umd/typings/_packages/node.index.d.ts +0 -4
- package/umd/typings/knowledge/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.d.ts +0 -7
- package/umd/typings/promptbook-library/constructors/createPromptbookLibraryFromSources.d.ts +0 -16
- /package/esm/typings/{_packages → src/_packages}/anthropic-claude.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/azure-openai.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/execute-javascript.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/fake-llm.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/langtail.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/openai.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/remote-client.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/remote-server.index.d.ts +0 -0
- /package/esm/typings/{_packages → src/_packages}/utils.index.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/prettify/PrettifyOptions.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/prettify/prettifyPromptbookString.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/prettify/renderPromptbookMermaid.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/promptbookJsonToString.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/promptbookStringToJson.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/promptbookStringToJson.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/extractParametersFromPromptTemplate.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/extractParametersFromPromptTemplate.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/extractVariables.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/extractVariables.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/parseCommand.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/parseCommand.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/parseNumber.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/parseNumber.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/renameParameter.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/renameParameter.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/titleToName.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/utils/titleToName.test.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/validation/_importPromptbook.d.ts +0 -0
- /package/esm/typings/{conversion → src/conversion}/validation/promptbookStringToJson-syntaxErrors.test.d.ts +0 -0
- /package/esm/typings/{conversion/validation/validatePromptbookJson-logicErrors.test.d.ts → src/conversion/validation/validatePromptbook-logicErrors.test.d.ts} +0 -0
- /package/esm/typings/{conversion/validation/validatePromptbookJson.test.d.ts → src/conversion/validation/validatePromptbook.test.d.ts} +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookExecutionError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookLibraryError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookLogicError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookNotFoundError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookReferenceError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/PromptbookSyntaxError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/TemplateError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/UnexpectedError.d.ts +0 -0
- /package/esm/typings/{errors → src/errors}/_ExpectError.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/CommonExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/ExecutionTools.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/LlmExecutionTools.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/PromptbookExecutor.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/ScriptExecutionTools.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/UserInterfaceTools.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/addPromptResultUsage.test.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/assertsExecutionSuccessful.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/createPromptbookExecutor.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/AutomaticTranslator.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/DebugAutomaticTranslator.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/FakeAutomaticTranslator.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/utils/extractMultiplicatedOccurrence.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/utils/extractMultiplicatedOccurrence.test.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/translation/automatic-translate/translateMessages.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/addUsage.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/addUsage.test.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/checkExpectations.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/checkExpectations.test.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/computeUsageCounts.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/forEachAsync.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/replaceParameters.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/replaceParameters.test.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/uncertainNumber.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/usageToWorktime.d.ts +0 -0
- /package/esm/typings/{execution → src/execution}/utils/usageToWorktime.test.d.ts +0 -0
- /package/esm/typings/{expectations → src/expectations}/drafts/isDomainNameFree.d.ts +0 -0
- /package/esm/typings/{expectations → src/expectations}/drafts/isGithubNameFree.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/_common/FormatDefinition.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/csv/ListFormatDefinition.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/index.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/json/JsonFormatDefinition.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/json/utils/isValidJsonString.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/json/utils/isValidJsonString.test.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/list/ListFormatDefinition.d.ts +0 -0
- /package/esm/typings/{formats → src/formats}/xml/XmlFormatDefinition.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/dialogs/callback/CallbackInterfaceTools.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/dialogs/user-interface-execution-tools.test.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/prepare-knowledge/_common/IndexPreparer.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/prepare-knowledge/markdown/playground/markdown-knowledge-playground.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts +0 -0
- /package/esm/typings/{knowledge → src/knowledge}/prepare-knowledge/pdf/prepareKnowledgeFromPdf.test.d.ts +0 -0
- /package/esm/typings/{promptbook-library → src/library}/PromptbookLibrary.d.ts +0 -0
- /package/esm/typings/{llm-providers/mocked/fakeTextToExpectations.test.d.ts → src/library/constructors/createLibraryFromDirectory.test.d.ts} +0 -0
- /package/esm/typings/{llm-providers/mocked/faked-completion.test.d.ts → src/library/constructors/createLibraryFromJson.test.d.ts} +0 -0
- /package/esm/typings/{llm-providers/openai/computeUsage.test.d.ts → src/library/constructors/createLibraryFromPromise.test.d.ts} +0 -0
- /package/esm/typings/{promptbook-library → src/library}/constructors/justTestFsImport.d.ts +0 -0
- /package/esm/typings/{promptbook-library/constructors/createPromptbookLibraryFromDirectory.test.d.ts → src/library/libraryToJson.test.d.ts} +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/anthropic-claude/playground/playground.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/azure-openai/playground/playground.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/langtail/LangtailExecutionTools.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/langtail/LangtailExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/langtail/playground/playground.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/mocked/fakeTextToExpectations.d.ts +0 -0
- /package/{umd/typings → esm/typings/src}/llm-providers/mocked/fakeTextToExpectations.test.d.ts +0 -0
- /package/{umd/typings → esm/typings/src}/llm-providers/mocked/faked-completion.test.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/mocked/joker.test.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/mocked/mocked-chat.test.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/mocked/mocked-completion.test.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/multiple/MultipleLlmExecutionTools.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/multiple/MultipleLlmExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/multiple/playground/playground.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/openai/OpenAiExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/openai/computeUsage.d.ts +0 -0
- /package/{umd/typings → esm/typings/src}/llm-providers/openai/computeUsage.test.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/openai/playground/playground.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/RemoteLlmExecutionTools.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/RemoteLlmExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Error.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Progress.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Request.d.ts +0 -0
- /package/esm/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Response.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/custom-function-async.test.ts.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/custom-function-missing.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/custom-function-with-dependencies.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/custom-function.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/postprocessing.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/script-execution-errors.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/_test/script-execution-tools.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/JavascriptEvalExecutionTools.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/JavascriptEvalExecutionTools.test.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/JavascriptExecutionTools.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/JavascriptExecutionToolsOptions.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/utils/preserve.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/javascript/utils/unknownToString.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/python/PythonExecutionTools.d.ts +0 -0
- /package/esm/typings/{scripting → src/scripting}/typescript/TypescriptExecutionTools.d.ts +0 -0
- /package/esm/typings/{types → src/types}/Command.d.ts +0 -0
- /package/esm/typings/{types → src/types}/ExecutionTypes.d.ts +0 -0
- /package/esm/typings/{types → src/types}/Parameters.d.ts +0 -0
- /package/esm/typings/{types → src/types}/Prompt.d.ts +0 -0
- /package/esm/typings/{types → src/types}/PromptbookJson/KnowledgeJson.d.ts +0 -0
- /package/esm/typings/{types → src/types}/PromptbookJson/PromptTemplateJson.d.ts +0 -0
- /package/esm/typings/{types → src/types}/PromptbookJson/PromptTemplateParameterJson.d.ts +0 -0
- /package/esm/typings/{types → src/types}/PromptbookJson/PromptbookJson.d.ts +0 -0
- /package/esm/typings/{types → src/types}/PromptbookString.d.ts +0 -0
- /package/esm/typings/{types → src/types}/ScriptLanguage.d.ts +0 -0
- /package/esm/typings/{types → src/types}/TaskProgress.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/ExecutionReportJson.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/ExecutionReportString.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/ExecutionReportStringOptions.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/config.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/countWorkingDuration.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/countWorkingDuration.test.d.ts +0 -0
- /package/esm/typings/{types → src/types}/execution-report/executionReportJsonToString.d.ts +0 -0
- /package/esm/typings/{types → src/types}/typeAliasEmoji.d.ts +0 -0
- /package/esm/typings/{types → src/types}/typeAliases.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/FromtoItems.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/emojis.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countCharacters.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countCharacters.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countLines.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countLines.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countPages.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countPages.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countParagraphs.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countParagraphs.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countSentences.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countSentences.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countWords.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/countWords.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/expectation-counters/index.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/extractParameters.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/extractParameters.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/formatNumber.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/formatNumber.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/getCurrentIsoDate.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/isRunningInWhatever.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/just.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/addAutoGeneratedSection.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/addAutoGeneratedSection.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/createMarkdownChart.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/createMarkdownChart.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/createMarkdownTable.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/createMarkdownTable.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/escapeMarkdownBlock.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/escapeMarkdownBlock.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractAllBlocksFromMarkdown.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractAllBlocksFromMarkdown.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractAllListItemsFromMarkdown.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractAllListItemsFromMarkdown.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractOneBlockFromMarkdown.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/extractOneBlockFromMarkdown.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/prettifyMarkdown.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/prettifyMarkdown.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/removeContentComments.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/removeContentComments.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/removeMarkdownFormatting.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown/removeMarkdownFormatting.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown-json/MarkdownStructure.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown-json/countMarkdownStructureDeepness.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown-json/countMarkdownStructureDeepness.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown-json/markdownToMarkdownStructure.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/markdown-json/markdownToMarkdownStructure.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/DIACRITIC_VARIANTS_LETTERS.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/IKeywords.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/capitalize.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/capitalize.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/decapitalize.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/decapitalize.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/isValidKeyword.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/isValidKeyword.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/nameToUriPart.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/nameToUriPart.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/nameToUriParts.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/nameToUriParts.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalize-to-kebab-case.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalize-to-kebab-case.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_PascalCase.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_PascalCase.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_SCREAMING_CASE.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_SCREAMING_CASE.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_camelCase.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_camelCase.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_snake_case.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeTo_snake_case.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeWhitespaces.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/normalizeWhitespaces.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/parseKeywords.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/parseKeywords.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/parseKeywordsFromString.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/parseKeywordsFromString.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/removeDiacritics.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/removeDiacritics.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/searchKeywords.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/normalization/searchKeywords.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/postprocessing/extractBlock.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/removeEmojis.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/removeEmojis.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/removeQuotes.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/removeQuotes.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/difference.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/difference.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/intersection.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/intersection.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/union.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/sets/union.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/trimCodeBlock.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/trimCodeBlock.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/trimEndOfCodeBlock.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/trimEndOfCodeBlock.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/unwrapResult.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/unwrapResult.test.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/validators/url/isValidUrl.d.ts +0 -0
- /package/esm/typings/{utils → src/utils}/validators/url/isValidUrl.test.d.ts +0 -0
- /package/esm/typings/{version.d.ts → src/version.d.ts} +0 -0
- /package/umd/typings/{_packages → src/_packages}/anthropic-claude.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/azure-openai.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/execute-javascript.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/fake-llm.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/langtail.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/openai.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/remote-client.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/remote-server.index.d.ts +0 -0
- /package/umd/typings/{_packages → src/_packages}/utils.index.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/prettify/PrettifyOptions.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/prettify/prettifyPromptbookString.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/prettify/renderPromptbookMermaid.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/promptbookJsonToString.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/promptbookStringToJson.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/promptbookStringToJson.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/extractParametersFromPromptTemplate.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/extractParametersFromPromptTemplate.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/extractVariables.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/extractVariables.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/parseCommand.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/parseCommand.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/parseNumber.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/parseNumber.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/renameParameter.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/renameParameter.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/titleToName.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/utils/titleToName.test.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/validation/_importPromptbook.d.ts +0 -0
- /package/umd/typings/{conversion → src/conversion}/validation/promptbookStringToJson-syntaxErrors.test.d.ts +0 -0
- /package/{esm/typings/promptbook-library/constructors/createPromptbookLibraryFromPromise.test.d.ts → umd/typings/src/conversion/validation/validatePromptbook-logicErrors.test.d.ts} +0 -0
- /package/{esm/typings/promptbook-library/constructors/createPromptbookLibraryFromSources.test.d.ts → umd/typings/src/conversion/validation/validatePromptbook.test.d.ts} +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookExecutionError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookLibraryError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookLogicError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookNotFoundError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookReferenceError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/PromptbookSyntaxError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/TemplateError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/UnexpectedError.d.ts +0 -0
- /package/umd/typings/{errors → src/errors}/_ExpectError.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/CommonExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/ExecutionTools.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/LlmExecutionTools.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/PromptbookExecutor.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/ScriptExecutionTools.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/UserInterfaceTools.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/addPromptResultUsage.test.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/assertsExecutionSuccessful.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/createPromptbookExecutor.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/AutomaticTranslator.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/DebugAutomaticTranslator.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/FakeAutomaticTranslator.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/TranslatorOptions.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/utils/extractMultiplicatedOccurrence.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/automatic-translators/utils/extractMultiplicatedOccurrence.test.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/translation/automatic-translate/translateMessages.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/addUsage.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/addUsage.test.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/checkExpectations.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/checkExpectations.test.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/computeUsageCounts.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/forEachAsync.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/replaceParameters.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/replaceParameters.test.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/uncertainNumber.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/usageToWorktime.d.ts +0 -0
- /package/umd/typings/{execution → src/execution}/utils/usageToWorktime.test.d.ts +0 -0
- /package/umd/typings/{expectations → src/expectations}/drafts/isDomainNameFree.d.ts +0 -0
- /package/umd/typings/{expectations → src/expectations}/drafts/isGithubNameFree.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/_common/FormatDefinition.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/csv/ListFormatDefinition.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/index.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/json/JsonFormatDefinition.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/json/utils/isValidJsonString.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/json/utils/isValidJsonString.test.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/list/ListFormatDefinition.d.ts +0 -0
- /package/umd/typings/{formats → src/formats}/xml/XmlFormatDefinition.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/dialogs/callback/CallbackInterfaceTools.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/dialogs/callback/CallbackInterfaceToolsOptions.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/dialogs/user-interface-execution-tools.test.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/prepare-knowledge/_common/IndexPreparer.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/prepare-knowledge/markdown/playground/markdown-knowledge-playground.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/prepare-knowledge/markdown/prepareKnowledgeFromMarkdown.test.d.ts +0 -0
- /package/umd/typings/{knowledge → src/knowledge}/prepare-knowledge/pdf/prepareKnowledgeFromPdf.test.d.ts +0 -0
- /package/umd/typings/{promptbook-library → src/library}/PromptbookLibrary.d.ts +0 -0
- /package/umd/typings/{conversion/validation/validatePromptbookJson-logicErrors.test.d.ts → src/library/constructors/createLibraryFromDirectory.test.d.ts} +0 -0
- /package/umd/typings/{conversion/validation/validatePromptbookJson.test.d.ts → src/library/constructors/createLibraryFromJson.test.d.ts} +0 -0
- /package/umd/typings/{promptbook-library/constructors/createPromptbookLibraryFromDirectory.test.d.ts → src/library/constructors/createLibraryFromPromise.test.d.ts} +0 -0
- /package/umd/typings/{promptbook-library → src/library}/constructors/justTestFsImport.d.ts +0 -0
- /package/umd/typings/{promptbook-library/constructors/createPromptbookLibraryFromPromise.test.d.ts → src/library/libraryToJson.test.d.ts} +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/anthropic-claude/playground/playground.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/azure-openai/AzureOpenAiExecutionTools.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/azure-openai/AzureOpenAiExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/azure-openai/playground/playground.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/langtail/LangtailExecutionTools.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/langtail/LangtailExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/langtail/playground/playground.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/mocked/fakeTextToExpectations.d.ts +0 -0
- /package/umd/typings/{promptbook-library/constructors/createPromptbookLibraryFromSources.test.d.ts → src/llm-providers/mocked/fakeTextToExpectations.test.d.ts} +0 -0
- /package/umd/typings/{scripting/_test/custom-function-async.test.ts.test.d.ts → src/llm-providers/mocked/faked-completion.test.d.ts} +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/mocked/joker.test.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/mocked/mocked-chat.test.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/mocked/mocked-completion.test.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/multiple/MultipleLlmExecutionTools.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/multiple/MultipleLlmExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/multiple/playground/playground.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/openai/OpenAiExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/openai/computeUsage.d.ts +0 -0
- /package/umd/typings/{scripting/_test/custom-function-missing.test.d.ts → src/llm-providers/openai/computeUsage.test.d.ts} +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/openai/playground/playground.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/RemoteLlmExecutionTools.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/RemoteLlmExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Error.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Progress.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Request.d.ts +0 -0
- /package/umd/typings/{llm-providers → src/llm-providers}/remote/interfaces/Promptbook_Server_Response.d.ts +0 -0
- /package/umd/typings/{scripting/_test/custom-function-with-dependencies.test.d.ts → src/scripting/_test/custom-function-async.test.ts.test.d.ts} +0 -0
- /package/umd/typings/{scripting/_test/custom-function.test.d.ts → src/scripting/_test/custom-function-missing.test.d.ts} +0 -0
- /package/umd/typings/{scripting/_test/postprocessing.test.d.ts → src/scripting/_test/custom-function-with-dependencies.test.d.ts} +0 -0
- /package/umd/typings/{scripting/_test/script-execution-errors.test.d.ts → src/scripting/_test/custom-function.test.d.ts} +0 -0
- /package/umd/typings/{scripting/_test/script-execution-tools.test.d.ts → src/scripting/_test/postprocessing.test.d.ts} +0 -0
- /package/umd/typings/{types/execution-report/countWorkingDuration.test.d.ts → src/scripting/_test/script-execution-errors.test.d.ts} +0 -0
- /package/umd/typings/{utils/expectation-counters/countCharacters.test.d.ts → src/scripting/_test/script-execution-tools.test.d.ts} +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/JavascriptEvalExecutionTools.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/JavascriptEvalExecutionTools.test.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/JavascriptExecutionTools.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/JavascriptExecutionToolsOptions.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/utils/preserve.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/javascript/utils/unknownToString.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/python/PythonExecutionTools.d.ts +0 -0
- /package/umd/typings/{scripting → src/scripting}/typescript/TypescriptExecutionTools.d.ts +0 -0
- /package/umd/typings/{types → src/types}/Command.d.ts +0 -0
- /package/umd/typings/{types → src/types}/ExecutionTypes.d.ts +0 -0
- /package/umd/typings/{types → src/types}/Parameters.d.ts +0 -0
- /package/umd/typings/{types → src/types}/Prompt.d.ts +0 -0
- /package/umd/typings/{types → src/types}/PromptbookJson/KnowledgeJson.d.ts +0 -0
- /package/umd/typings/{types → src/types}/PromptbookJson/PromptTemplateJson.d.ts +0 -0
- /package/umd/typings/{types → src/types}/PromptbookJson/PromptTemplateParameterJson.d.ts +0 -0
- /package/umd/typings/{types → src/types}/PromptbookJson/PromptbookJson.d.ts +0 -0
- /package/umd/typings/{types → src/types}/PromptbookString.d.ts +0 -0
- /package/umd/typings/{types → src/types}/ScriptLanguage.d.ts +0 -0
- /package/umd/typings/{types → src/types}/TaskProgress.d.ts +0 -0
- /package/umd/typings/{types → src/types}/execution-report/ExecutionReportJson.d.ts +0 -0
- /package/umd/typings/{types → src/types}/execution-report/ExecutionReportString.d.ts +0 -0
- /package/umd/typings/{types → src/types}/execution-report/ExecutionReportStringOptions.d.ts +0 -0
- /package/umd/typings/{types → src/types}/execution-report/config.d.ts +0 -0
- /package/umd/typings/{types → src/types}/execution-report/countWorkingDuration.d.ts +0 -0
- /package/umd/typings/{utils/expectation-counters/countLines.test.d.ts → src/types/execution-report/countWorkingDuration.test.d.ts} +0 -0
- /package/umd/typings/{types → src/types}/execution-report/executionReportJsonToString.d.ts +0 -0
- /package/umd/typings/{types → src/types}/typeAliasEmoji.d.ts +0 -0
- /package/umd/typings/{types → src/types}/typeAliases.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/FromtoItems.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/emojis.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countCharacters.d.ts +0 -0
- /package/umd/typings/{utils/expectation-counters/countPages.test.d.ts → src/utils/expectation-counters/countCharacters.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countLines.d.ts +0 -0
- /package/umd/typings/{utils/expectation-counters/countParagraphs.test.d.ts → src/utils/expectation-counters/countLines.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countPages.d.ts +0 -0
- /package/umd/typings/{utils/expectation-counters/countSentences.test.d.ts → src/utils/expectation-counters/countPages.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countParagraphs.d.ts +0 -0
- /package/umd/typings/{utils/expectation-counters/countWords.test.d.ts → src/utils/expectation-counters/countParagraphs.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countSentences.d.ts +0 -0
- /package/umd/typings/{utils/extractParameters.test.d.ts → src/utils/expectation-counters/countSentences.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/countWords.d.ts +0 -0
- /package/umd/typings/{utils/formatNumber.test.d.ts → src/utils/expectation-counters/countWords.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/expectation-counters/index.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/extractParameters.d.ts +0 -0
- /package/umd/typings/{utils/markdown-json/countMarkdownStructureDeepness.test.d.ts → src/utils/extractParameters.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/formatNumber.d.ts +0 -0
- /package/umd/typings/{utils/markdown-json/markdownToMarkdownStructure.test.d.ts → src/utils/formatNumber.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/getCurrentIsoDate.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/isRunningInWhatever.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/just.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/addAutoGeneratedSection.d.ts +0 -0
- /package/umd/typings/{utils/markdown/createMarkdownTable.test.d.ts → src/utils/markdown/addAutoGeneratedSection.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/createMarkdownChart.d.ts +0 -0
- /package/umd/typings/{utils/markdown/escapeMarkdownBlock.test.d.ts → src/utils/markdown/createMarkdownChart.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/createMarkdownTable.d.ts +0 -0
- /package/umd/typings/{utils/markdown/extractAllBlocksFromMarkdown.test.d.ts → src/utils/markdown/createMarkdownTable.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/escapeMarkdownBlock.d.ts +0 -0
- /package/umd/typings/{utils/markdown/extractAllListItemsFromMarkdown.test.d.ts → src/utils/markdown/escapeMarkdownBlock.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/extractAllBlocksFromMarkdown.d.ts +0 -0
- /package/umd/typings/{utils/markdown/extractOneBlockFromMarkdown.test.d.ts → src/utils/markdown/extractAllBlocksFromMarkdown.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/extractAllListItemsFromMarkdown.d.ts +0 -0
- /package/umd/typings/{utils/markdown/prettifyMarkdown.test.d.ts → src/utils/markdown/extractAllListItemsFromMarkdown.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/extractOneBlockFromMarkdown.d.ts +0 -0
- /package/umd/typings/{utils/markdown/removeContentComments.test.d.ts → src/utils/markdown/extractOneBlockFromMarkdown.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/prettifyMarkdown.d.ts +0 -0
- /package/umd/typings/{utils/markdown/removeMarkdownFormatting.test.d.ts → src/utils/markdown/prettifyMarkdown.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/removeContentComments.d.ts +0 -0
- /package/umd/typings/{utils/normalization/capitalize.test.d.ts → src/utils/markdown/removeContentComments.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown/removeMarkdownFormatting.d.ts +0 -0
- /package/umd/typings/{utils/normalization/decapitalize.test.d.ts → src/utils/markdown/removeMarkdownFormatting.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown-json/MarkdownStructure.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/markdown-json/countMarkdownStructureDeepness.d.ts +0 -0
- /package/umd/typings/{utils/markdown/addAutoGeneratedSection.test.d.ts → src/utils/markdown-json/countMarkdownStructureDeepness.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/markdown-json/markdownToMarkdownStructure.d.ts +0 -0
- /package/umd/typings/{utils/markdown/createMarkdownChart.test.d.ts → src/utils/markdown-json/markdownToMarkdownStructure.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/DIACRITIC_VARIANTS_LETTERS.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/IKeywords.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/capitalize.d.ts +0 -0
- /package/umd/typings/{utils/normalization/isValidKeyword.test.d.ts → src/utils/normalization/capitalize.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/decapitalize.d.ts +0 -0
- /package/umd/typings/{utils/normalization/nameToUriPart.test.d.ts → src/utils/normalization/decapitalize.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/isValidKeyword.d.ts +0 -0
- /package/umd/typings/{utils/normalization/nameToUriParts.test.d.ts → src/utils/normalization/isValidKeyword.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/nameToUriPart.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalize-to-kebab-case.test.d.ts → src/utils/normalization/nameToUriPart.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/nameToUriParts.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalizeTo_PascalCase.test.d.ts → src/utils/normalization/nameToUriParts.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalize-to-kebab-case.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalizeTo_SCREAMING_CASE.test.d.ts → src/utils/normalization/normalize-to-kebab-case.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalizeTo_PascalCase.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalizeTo_camelCase.test.d.ts → src/utils/normalization/normalizeTo_PascalCase.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalizeTo_SCREAMING_CASE.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalizeTo_snake_case.test.d.ts → src/utils/normalization/normalizeTo_SCREAMING_CASE.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalizeTo_camelCase.d.ts +0 -0
- /package/umd/typings/{utils/normalization/normalizeWhitespaces.test.d.ts → src/utils/normalization/normalizeTo_camelCase.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalizeTo_snake_case.d.ts +0 -0
- /package/umd/typings/{utils/normalization/parseKeywords.test.d.ts → src/utils/normalization/normalizeTo_snake_case.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/normalizeWhitespaces.d.ts +0 -0
- /package/umd/typings/{utils/normalization/parseKeywordsFromString.test.d.ts → src/utils/normalization/normalizeWhitespaces.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/parseKeywords.d.ts +0 -0
- /package/umd/typings/{utils/normalization/removeDiacritics.test.d.ts → src/utils/normalization/parseKeywords.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/parseKeywordsFromString.d.ts +0 -0
- /package/umd/typings/{utils/normalization/searchKeywords.test.d.ts → src/utils/normalization/parseKeywordsFromString.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/removeDiacritics.d.ts +0 -0
- /package/umd/typings/{utils/removeEmojis.test.d.ts → src/utils/normalization/removeDiacritics.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/normalization/searchKeywords.d.ts +0 -0
- /package/umd/typings/{utils/removeQuotes.test.d.ts → src/utils/normalization/searchKeywords.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/postprocessing/extractBlock.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/removeEmojis.d.ts +0 -0
- /package/umd/typings/{utils/sets/difference.test.d.ts → src/utils/removeEmojis.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/removeQuotes.d.ts +0 -0
- /package/umd/typings/{utils/sets/intersection.test.d.ts → src/utils/removeQuotes.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/sets/difference.d.ts +0 -0
- /package/umd/typings/{utils/sets/union.test.d.ts → src/utils/sets/difference.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/sets/intersection.d.ts +0 -0
- /package/umd/typings/{utils/trimCodeBlock.test.d.ts → src/utils/sets/intersection.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/sets/union.d.ts +0 -0
- /package/umd/typings/{utils/trimEndOfCodeBlock.test.d.ts → src/utils/sets/union.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/trimCodeBlock.d.ts +0 -0
- /package/umd/typings/{utils/unwrapResult.test.d.ts → src/utils/trimCodeBlock.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/trimEndOfCodeBlock.d.ts +0 -0
- /package/umd/typings/{utils/validators/url/isValidUrl.test.d.ts → src/utils/trimEndOfCodeBlock.test.d.ts} +0 -0
- /package/umd/typings/{utils → src/utils}/unwrapResult.d.ts +0 -0
- /package/umd/typings/{utils → src/utils}/validators/url/isValidUrl.d.ts +0 -0
- /package/umd/typings/{version.d.ts → src/version.d.ts} +0 -0
package/esm/index.es.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import colors from 'colors';
|
|
2
|
+
import { access, constants, readFile, readdir } from 'fs/promises';
|
|
2
3
|
import { join } from 'path';
|
|
3
4
|
import spaceTrim$1, { spaceTrim } from 'spacetrim';
|
|
4
5
|
import { format } from 'prettier';
|
|
@@ -123,6 +124,15 @@ function __spreadArray(to, from, pack) {
|
|
|
123
124
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
/**
|
|
128
|
+
* The maximum number of iterations for a loops
|
|
129
|
+
*/
|
|
130
|
+
var LOOP_LIMIT = 1000;
|
|
131
|
+
/**
|
|
132
|
+
* The name of the builded promptbook library made by CLI `promptbook make` and for lookup in `createLibraryFromDirectory`
|
|
133
|
+
*/
|
|
134
|
+
var PROMPTBOOK_MAKED_BASE_FILENAME = "index";
|
|
135
|
+
|
|
126
136
|
/**
|
|
127
137
|
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
128
138
|
*/
|
|
@@ -137,48 +147,60 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
|
137
147
|
return PromptbookSyntaxError;
|
|
138
148
|
}(Error));
|
|
139
149
|
|
|
140
|
-
|
|
141
|
-
function prepareKnowledgeFromMarkdown(options) {
|
|
142
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
143
|
-
return __generator(this, function (_a) {
|
|
144
|
-
return [2 /*return*/, []];
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
}
|
|
150
|
+
var promptbookLibrary = [{title:"Prepare Keywords",promptbookUrl:"https://promptbook.studio/promptbook/prepare-keywords.ptbk.md",promptbookVersion:"0.59.0-30",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"],executionType:"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:[]},{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-30",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"],executionType:"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:[]}];
|
|
148
151
|
|
|
149
152
|
/**
|
|
150
|
-
*
|
|
153
|
+
* This error indicates errors during the execution of the promptbook
|
|
151
154
|
*/
|
|
152
|
-
var
|
|
155
|
+
var PromptbookExecutionError = /** @class */ (function (_super) {
|
|
156
|
+
__extends(PromptbookExecutionError, _super);
|
|
157
|
+
function PromptbookExecutionError(message) {
|
|
158
|
+
var _this = _super.call(this, message) || this;
|
|
159
|
+
_this.name = 'PromptbookExecutionError';
|
|
160
|
+
Object.setPrototypeOf(_this, PromptbookExecutionError.prototype);
|
|
161
|
+
return _this;
|
|
162
|
+
}
|
|
163
|
+
return PromptbookExecutionError;
|
|
164
|
+
}(Error));
|
|
153
165
|
|
|
154
166
|
/**
|
|
155
|
-
*
|
|
167
|
+
* Asserts that the execution of a promptnook is successful
|
|
156
168
|
*
|
|
157
|
-
* @
|
|
169
|
+
* @param executionResult - The partial result of the promptnook execution
|
|
170
|
+
* @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
|
|
158
171
|
*/
|
|
159
|
-
function
|
|
160
|
-
var
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
164
|
-
var section = _c.value;
|
|
165
|
-
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
166
|
-
}
|
|
172
|
+
function assertsExecutionSuccessful(executionResult) {
|
|
173
|
+
var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
|
|
174
|
+
if (isSuccessful === true) {
|
|
175
|
+
return;
|
|
167
176
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
177
|
+
if (errors.length === 0) {
|
|
178
|
+
throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
|
|
179
|
+
}
|
|
180
|
+
else if (errors.length === 1) {
|
|
181
|
+
throw errors[0];
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
|
|
174
185
|
}
|
|
175
|
-
return maxDeepness + 1;
|
|
176
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* TODO: [🧠] Can this return type be better typed than void
|
|
189
|
+
*/
|
|
177
190
|
|
|
178
191
|
/**
|
|
179
|
-
*
|
|
192
|
+
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
180
193
|
*/
|
|
181
|
-
var
|
|
194
|
+
var PromptbookLogicError = /** @class */ (function (_super) {
|
|
195
|
+
__extends(PromptbookLogicError, _super);
|
|
196
|
+
function PromptbookLogicError(message) {
|
|
197
|
+
var _this = _super.call(this, message) || this;
|
|
198
|
+
_this.name = 'PromptbookLogicError';
|
|
199
|
+
Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
|
|
200
|
+
return _this;
|
|
201
|
+
}
|
|
202
|
+
return PromptbookLogicError;
|
|
203
|
+
}(Error));
|
|
182
204
|
|
|
183
205
|
/**
|
|
184
206
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
@@ -195,804 +217,399 @@ var UnexpectedError = /** @class */ (function (_super) {
|
|
|
195
217
|
}(Error));
|
|
196
218
|
|
|
197
219
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* Note: This function does work with code blocks
|
|
201
|
-
* Note: This function does not work with markdown comments
|
|
202
|
-
*
|
|
203
|
-
* @param markdown The markdown string to parse.
|
|
204
|
-
* @returns The MarkdownStructure object.
|
|
220
|
+
* Tests if given string is valid URL.
|
|
205
221
|
*
|
|
206
|
-
*
|
|
222
|
+
* Note: Dataurl are considered perfectly valid.
|
|
207
223
|
*/
|
|
208
|
-
function
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
var current = root;
|
|
213
|
-
var isInsideCodeBlock = false;
|
|
224
|
+
function isValidUrl(url) {
|
|
225
|
+
if (typeof url !== 'string') {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
214
228
|
try {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
218
|
-
if (isInsideCodeBlock || !headingMatch) {
|
|
219
|
-
if (line.startsWith('```')) {
|
|
220
|
-
isInsideCodeBlock = !isInsideCodeBlock;
|
|
221
|
-
}
|
|
222
|
-
current.contentLines.push(line);
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
var level = headingMatch.groups.mark.length;
|
|
226
|
-
var title = headingMatch.groups.title.trim();
|
|
227
|
-
var parent_1 = void 0;
|
|
228
|
-
if (level > current.level) {
|
|
229
|
-
// Note: Going deeper (next section is child of current)
|
|
230
|
-
parent_1 = current;
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
234
|
-
parent_1 = current;
|
|
235
|
-
var loopLimit = LOOP_LIMIT;
|
|
236
|
-
while (parent_1.level !== level - 1) {
|
|
237
|
-
if (loopLimit-- < 0) {
|
|
238
|
-
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
239
|
-
}
|
|
240
|
-
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
241
|
-
// [🌻]
|
|
242
|
-
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
243
|
-
}
|
|
244
|
-
parent_1 = parent_1.parent;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
248
|
-
parent_1.sections.push(section);
|
|
249
|
-
current = section;
|
|
250
|
-
}
|
|
229
|
+
if (url.startsWith('blob:')) {
|
|
230
|
+
url = url.replace(/^blob:/, '');
|
|
251
231
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
232
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
233
|
+
var urlObject = new URL(url);
|
|
234
|
+
if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
|
|
235
|
+
return false;
|
|
257
236
|
}
|
|
258
|
-
|
|
237
|
+
return true;
|
|
259
238
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return markdownStructure;
|
|
239
|
+
catch (error) {
|
|
240
|
+
return false;
|
|
263
241
|
}
|
|
264
|
-
// [🌻]
|
|
265
|
-
throw new Error('The markdown file must have exactly one top-level section.');
|
|
266
|
-
// return root;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* @private
|
|
270
|
-
*/
|
|
271
|
-
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
272
|
-
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
273
|
-
return {
|
|
274
|
-
level: level,
|
|
275
|
-
title: title,
|
|
276
|
-
content: spaceTrim(contentLines.join('\n')),
|
|
277
|
-
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
278
|
-
};
|
|
279
242
|
}
|
|
280
243
|
|
|
281
244
|
/**
|
|
282
|
-
*
|
|
245
|
+
* Validates PromptbookJson if it is logically valid
|
|
283
246
|
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* Note: It flattens nested lists
|
|
287
|
-
* Note: It can not work with html syntax and comments
|
|
247
|
+
* It checks:
|
|
248
|
+
* - if it has correct parameters dependency
|
|
288
249
|
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
250
|
+
* It does NOT check:
|
|
251
|
+
* - if it is valid json
|
|
252
|
+
* - if it is meaningful
|
|
253
|
+
*
|
|
254
|
+
* @param promptbook valid or invalid PromptbookJson
|
|
255
|
+
* @returns the same promptbook if it is logically valid
|
|
256
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
291
257
|
*/
|
|
292
|
-
function
|
|
293
|
-
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
|
|
258
|
+
function validatePromptbook(promptbook) {
|
|
259
|
+
// TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
|
|
260
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
261
|
+
if (promptbook.promptbookUrl !== undefined) {
|
|
262
|
+
if (!isValidUrl(promptbook.promptbookUrl)) {
|
|
263
|
+
// TODO: This should be maybe the syntax error detected during parsing
|
|
264
|
+
throw new PromptbookLogicError("Invalid promptbook URL \"".concat(promptbook.promptbookUrl, "\""));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
268
|
+
if (!Array.isArray(promptbook.parameters)) {
|
|
269
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
270
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
271
|
+
}
|
|
272
|
+
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
273
|
+
if (!Array.isArray(promptbook.promptTemplates)) {
|
|
274
|
+
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
275
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.promptTemplates expected to be an array, but got ".concat(typeof promptbook.promptTemplates, "\n ")));
|
|
276
|
+
}
|
|
277
|
+
var _loop_1 = function (parameter) {
|
|
278
|
+
if (parameter.isInput && parameter.isOutput) {
|
|
279
|
+
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
280
|
+
}
|
|
281
|
+
// Note: Testing that parameter is either intermediate or output BUT not created and unused
|
|
282
|
+
if (!parameter.isInput &&
|
|
283
|
+
!parameter.isOutput &&
|
|
284
|
+
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
285
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
286
|
+
}
|
|
287
|
+
// Note: Testing that parameter is either input or result of some template
|
|
288
|
+
if (!parameter.isInput &&
|
|
289
|
+
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
290
|
+
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
291
|
+
}
|
|
292
|
+
};
|
|
297
293
|
try {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var
|
|
301
|
-
|
|
302
|
-
isInCodeBlock = !isInCodeBlock;
|
|
303
|
-
}
|
|
304
|
-
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
305
|
-
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
306
|
-
listItems.push(listItem);
|
|
307
|
-
}
|
|
294
|
+
// Note: Check each parameter individually
|
|
295
|
+
for (var _e = __values(promptbook.parameters), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
296
|
+
var parameter = _f.value;
|
|
297
|
+
_loop_1(parameter);
|
|
308
298
|
}
|
|
309
299
|
}
|
|
310
300
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
311
301
|
finally {
|
|
312
302
|
try {
|
|
313
|
-
if (
|
|
303
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
314
304
|
}
|
|
315
305
|
finally { if (e_1) throw e_1.error; }
|
|
316
306
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Extracts all code blocks from markdown.
|
|
330
|
-
*
|
|
331
|
-
* Note: There are 3 simmilar function:
|
|
332
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
333
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
334
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
335
|
-
*
|
|
336
|
-
* @param markdown any valid markdown
|
|
337
|
-
* @returns code blocks with language and content
|
|
338
|
-
*
|
|
339
|
-
*/
|
|
340
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
341
|
-
var e_1, _a;
|
|
342
|
-
var codeBlocks = [];
|
|
343
|
-
var lines = markdown.split('\n');
|
|
344
|
-
var currentCodeBlock = null;
|
|
307
|
+
// Note: Check each template individually
|
|
308
|
+
var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
|
|
309
|
+
var isInput = _a.isInput;
|
|
310
|
+
return isInput;
|
|
311
|
+
}).map(function (_a) {
|
|
312
|
+
var name = _a.name;
|
|
313
|
+
return name;
|
|
314
|
+
}));
|
|
345
315
|
try {
|
|
346
|
-
for (var
|
|
347
|
-
var
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
316
|
+
for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
317
|
+
var template = _h.value;
|
|
318
|
+
if (definedParameters.has(template.resultingParameterName)) {
|
|
319
|
+
throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
|
|
320
|
+
}
|
|
321
|
+
definedParameters.add(template.resultingParameterName);
|
|
322
|
+
if (template.executionType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
|
|
323
|
+
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
324
|
+
}
|
|
325
|
+
if (template.jokers && template.jokers.length > 0) {
|
|
326
|
+
if (!template.expectFormat &&
|
|
327
|
+
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
328
|
+
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
352
329
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
330
|
+
try {
|
|
331
|
+
for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
332
|
+
var joker = _k.value;
|
|
333
|
+
if (!template.dependentParameterNames.includes(joker)) {
|
|
334
|
+
throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
|
|
335
|
+
}
|
|
357
336
|
}
|
|
358
|
-
|
|
359
|
-
|
|
337
|
+
}
|
|
338
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
339
|
+
finally {
|
|
340
|
+
try {
|
|
341
|
+
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
342
|
+
}
|
|
343
|
+
finally { if (e_3) throw e_3.error; }
|
|
360
344
|
}
|
|
361
345
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
346
|
+
if (template.expectations) {
|
|
347
|
+
try {
|
|
348
|
+
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
349
|
+
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
350
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
351
|
+
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
352
|
+
}
|
|
353
|
+
if (min !== undefined && min < 0) {
|
|
354
|
+
throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
|
|
355
|
+
}
|
|
356
|
+
if (max !== undefined && max <= 0) {
|
|
357
|
+
throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
362
|
+
finally {
|
|
363
|
+
try {
|
|
364
|
+
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
365
|
+
}
|
|
366
|
+
finally { if (e_4) throw e_4.error; }
|
|
365
367
|
}
|
|
366
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
367
368
|
}
|
|
368
369
|
}
|
|
369
370
|
}
|
|
370
|
-
catch (
|
|
371
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
371
372
|
finally {
|
|
372
373
|
try {
|
|
373
|
-
if (
|
|
374
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
374
375
|
}
|
|
375
|
-
finally { if (
|
|
376
|
+
finally { if (e_2) throw e_2.error; }
|
|
376
377
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
378
|
+
// Note: Detect circular dependencies
|
|
379
|
+
var resovedParameters = promptbook.parameters
|
|
380
|
+
.filter(function (_a) {
|
|
381
|
+
var isInput = _a.isInput;
|
|
382
|
+
return isInput;
|
|
383
|
+
})
|
|
384
|
+
.map(function (_a) {
|
|
385
|
+
var name = _a.name;
|
|
386
|
+
return name;
|
|
387
|
+
});
|
|
388
|
+
var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
389
|
+
var loopLimit = LOOP_LIMIT;
|
|
390
|
+
var _loop_2 = function () {
|
|
391
|
+
if (loopLimit-- < 0) {
|
|
392
|
+
throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbook`');
|
|
393
|
+
}
|
|
394
|
+
var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
|
|
395
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
396
|
+
});
|
|
397
|
+
if (currentlyResovedTemplates.length === 0) {
|
|
398
|
+
throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
399
|
+
.map(function (_a) {
|
|
400
|
+
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
401
|
+
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
402
|
+
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
403
|
+
.join(', '));
|
|
404
|
+
})
|
|
405
|
+
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
|
|
406
|
+
}
|
|
407
|
+
resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
|
|
408
|
+
var resultingParameterName = _a.resultingParameterName;
|
|
409
|
+
return resultingParameterName;
|
|
410
|
+
})), false);
|
|
411
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
|
|
412
|
+
};
|
|
413
|
+
while (unresovedTemplates.length > 0) {
|
|
414
|
+
_loop_2();
|
|
380
415
|
}
|
|
381
|
-
return
|
|
416
|
+
return promptbook;
|
|
382
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* TODO: [🧠] Work with promptbookVersion
|
|
420
|
+
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
421
|
+
* > /**
|
|
422
|
+
* > * Validates PromptbookJson if it is logically valid.
|
|
423
|
+
* > *
|
|
424
|
+
* > * It checks:
|
|
425
|
+
* > * - it has a valid structure
|
|
426
|
+
* > * - ...
|
|
427
|
+
* > ex port function validatePromptbook(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
428
|
+
*/
|
|
383
429
|
|
|
384
430
|
/**
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* Note: If there are multiple or no code blocks the function throws an error
|
|
388
|
-
*
|
|
389
|
-
* Note: There are 3 simmilar function:
|
|
390
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
391
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
392
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
431
|
+
* This error occurs when some expectation is not met in the execution of the pipeline
|
|
393
432
|
*
|
|
394
|
-
* @
|
|
395
|
-
*
|
|
433
|
+
* @private Always catched and rethrown as `PromptbookExecutionError`
|
|
434
|
+
* Note: This is a kindof subtype of PromptbookExecutionError
|
|
396
435
|
*/
|
|
397
|
-
function
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
436
|
+
var ExpectError = /** @class */ (function (_super) {
|
|
437
|
+
__extends(ExpectError, _super);
|
|
438
|
+
function ExpectError(message) {
|
|
439
|
+
var _this = _super.call(this, message) || this;
|
|
440
|
+
_this.name = 'ExpectError';
|
|
441
|
+
Object.setPrototypeOf(_this, ExpectError.prototype);
|
|
442
|
+
return _this;
|
|
402
443
|
}
|
|
403
|
-
return
|
|
404
|
-
}
|
|
405
|
-
/***
|
|
406
|
-
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
407
|
-
*/
|
|
444
|
+
return ExpectError;
|
|
445
|
+
}(Error));
|
|
408
446
|
|
|
409
447
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
* @param {string} content - The string to remove comments from.
|
|
413
|
-
* @returns {string} The input string with all comments removed.
|
|
448
|
+
* Function isValidJsonString will tell you if the string is valid JSON or not
|
|
414
449
|
*/
|
|
415
|
-
function
|
|
416
|
-
|
|
450
|
+
function isValidJsonString(value /* <-[👨⚖️] */) {
|
|
451
|
+
try {
|
|
452
|
+
JSON.parse(value);
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
if (!(error instanceof Error)) {
|
|
457
|
+
throw error;
|
|
458
|
+
}
|
|
459
|
+
if (error.message.includes('Unexpected token')) {
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
417
464
|
}
|
|
418
465
|
|
|
419
466
|
/**
|
|
420
|
-
*
|
|
467
|
+
* The version of the Promptbook library
|
|
421
468
|
*/
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
469
|
+
var PROMPTBOOK_VERSION = '0.59.0-30';
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Function `addUsage` will add multiple usages into one
|
|
473
|
+
*
|
|
474
|
+
* Note: If you provide 0 values, it returns void usage
|
|
475
|
+
*/
|
|
476
|
+
function addUsage() {
|
|
477
|
+
var usageItems = [];
|
|
425
478
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
var
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
479
|
+
usageItems[_i] = arguments[_i];
|
|
480
|
+
}
|
|
481
|
+
var initialStructure = {
|
|
482
|
+
price: { value: 0 },
|
|
483
|
+
input: {
|
|
484
|
+
tokensCount: { value: 0 },
|
|
485
|
+
charactersCount: { value: 0 },
|
|
486
|
+
wordsCount: { value: 0 },
|
|
487
|
+
sentencesCount: { value: 0 },
|
|
488
|
+
linesCount: { value: 0 },
|
|
489
|
+
paragraphsCount: { value: 0 },
|
|
490
|
+
pagesCount: { value: 0 },
|
|
491
|
+
},
|
|
492
|
+
output: {
|
|
493
|
+
tokensCount: { value: 0 },
|
|
494
|
+
charactersCount: { value: 0 },
|
|
495
|
+
wordsCount: { value: 0 },
|
|
496
|
+
sentencesCount: { value: 0 },
|
|
497
|
+
linesCount: { value: 0 },
|
|
498
|
+
paragraphsCount: { value: 0 },
|
|
499
|
+
pagesCount: { value: 0 },
|
|
500
|
+
},
|
|
501
|
+
};
|
|
502
|
+
return usageItems.reduce(function (acc, item) {
|
|
503
|
+
var e_1, _a, e_2, _b;
|
|
504
|
+
var _c;
|
|
505
|
+
acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
|
|
506
|
+
try {
|
|
507
|
+
for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
508
|
+
var key = _e.value;
|
|
509
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
510
|
+
//@ts-ignore
|
|
511
|
+
if (item.input[key]) {
|
|
512
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
513
|
+
//@ts-ignore
|
|
514
|
+
acc.input[key].value += item.input[key].value || 0;
|
|
515
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
516
|
+
//@ts-ignore
|
|
517
|
+
if (item.input[key].isUncertain) {
|
|
518
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
519
|
+
//@ts-ignore
|
|
520
|
+
acc.input[key].isUncertain = true;
|
|
521
|
+
}
|
|
436
522
|
}
|
|
437
523
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
finally { if (e_2) throw e_2.error; }
|
|
524
|
+
}
|
|
525
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
526
|
+
finally {
|
|
527
|
+
try {
|
|
528
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
444
529
|
}
|
|
530
|
+
finally { if (e_1) throw e_1.error; }
|
|
445
531
|
}
|
|
446
|
-
}
|
|
447
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
448
|
-
finally {
|
|
449
532
|
try {
|
|
450
|
-
|
|
533
|
+
for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
534
|
+
var key = _g.value;
|
|
535
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
536
|
+
//@ts-ignore
|
|
537
|
+
if (item.output[key]) {
|
|
538
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
539
|
+
//@ts-ignore
|
|
540
|
+
acc.output[key].value += item.output[key].value || 0;
|
|
541
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
542
|
+
//@ts-ignore
|
|
543
|
+
if (item.output[key].isUncertain) {
|
|
544
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
545
|
+
//@ts-ignore
|
|
546
|
+
acc.output[key].isUncertain = true;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
451
550
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
551
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
552
|
+
finally {
|
|
553
|
+
try {
|
|
554
|
+
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
555
|
+
}
|
|
556
|
+
finally { if (e_2) throw e_2.error; }
|
|
557
|
+
}
|
|
558
|
+
return acc;
|
|
559
|
+
}, initialStructure);
|
|
455
560
|
}
|
|
456
561
|
|
|
457
562
|
/**
|
|
458
|
-
*
|
|
563
|
+
* Counts number of characters in the text
|
|
459
564
|
*/
|
|
460
|
-
|
|
565
|
+
function countCharacters(text) {
|
|
566
|
+
// Remove null characters
|
|
567
|
+
text = text.replace(/\0/g, '');
|
|
568
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
569
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
570
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
571
|
+
text = text.replace(/\p{Extended_Pictographic}(\u{200D}\p{Extended_Pictographic})*/gu, '-');
|
|
572
|
+
return text.length;
|
|
573
|
+
}
|
|
461
574
|
|
|
462
575
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @param template the template with parameters in {curly} braces
|
|
466
|
-
* @returns the list of parameter names
|
|
576
|
+
* Counts number of lines in the text
|
|
467
577
|
*/
|
|
468
|
-
function
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
var parameterNames = new Set();
|
|
472
|
-
try {
|
|
473
|
-
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
474
|
-
var match = matches_1_1.value;
|
|
475
|
-
var parameterName = match[0].slice(1, -1);
|
|
476
|
-
parameterNames.add(parameterName);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
480
|
-
finally {
|
|
481
|
-
try {
|
|
482
|
-
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
483
|
-
}
|
|
484
|
-
finally { if (e_1) throw e_1.error; }
|
|
578
|
+
function countLines(text) {
|
|
579
|
+
if (text === '') {
|
|
580
|
+
return 0;
|
|
485
581
|
}
|
|
486
|
-
return
|
|
582
|
+
return text.split('\n').length;
|
|
487
583
|
}
|
|
488
584
|
|
|
489
585
|
/**
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
* @param script from which to extract the variables
|
|
493
|
-
* @returns the list of variable names
|
|
494
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
586
|
+
* Counts number of pages in the text
|
|
495
587
|
*/
|
|
496
|
-
function
|
|
497
|
-
var
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
try {
|
|
502
|
-
eval(script);
|
|
503
|
-
}
|
|
504
|
-
catch (error) {
|
|
505
|
-
if (!(error instanceof ReferenceError)) {
|
|
506
|
-
throw error;
|
|
507
|
-
}
|
|
508
|
-
var undefinedName = error.message.split(' ')[0];
|
|
509
|
-
/*
|
|
510
|
-
Note: Remapping error
|
|
511
|
-
From: [ReferenceError: thing is not defined],
|
|
512
|
-
To: [Error: Parameter {thing} is not defined],
|
|
513
|
-
*/
|
|
514
|
-
if (!undefinedName) {
|
|
515
|
-
throw error;
|
|
516
|
-
}
|
|
517
|
-
if (script.includes(undefinedName + '(')) {
|
|
518
|
-
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
521
|
-
variables.add(undefinedName);
|
|
522
|
-
script = "const ".concat(undefinedName, " = '';") + script;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
catch (error) {
|
|
527
|
-
if (!(error instanceof Error)) {
|
|
528
|
-
throw error;
|
|
529
|
-
}
|
|
530
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
531
|
-
}
|
|
532
|
-
return variables;
|
|
588
|
+
function countPages(text) {
|
|
589
|
+
var sentencesPerPage = 5; // Assuming each page has 5 sentences
|
|
590
|
+
var sentences = text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
591
|
+
var pageCount = Math.ceil(sentences.length / sentencesPerPage);
|
|
592
|
+
return pageCount;
|
|
533
593
|
}
|
|
534
|
-
/**
|
|
535
|
-
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
536
|
-
*/
|
|
537
594
|
|
|
538
595
|
/**
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
* @param promptTemplate the template with used parameters
|
|
542
|
-
* @returns the set of parameter names
|
|
543
|
-
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
544
|
-
*/
|
|
545
|
-
function extractParametersFromPromptTemplate(promptTemplate) {
|
|
546
|
-
var e_1, _a, e_2, _b;
|
|
547
|
-
var parameterNames = new Set();
|
|
548
|
-
try {
|
|
549
|
-
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()) {
|
|
550
|
-
var parameterName = _d.value;
|
|
551
|
-
parameterNames.add(parameterName);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
555
|
-
finally {
|
|
556
|
-
try {
|
|
557
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
558
|
-
}
|
|
559
|
-
finally { if (e_1) throw e_1.error; }
|
|
560
|
-
}
|
|
561
|
-
if (promptTemplate.executionType === 'SCRIPT') {
|
|
562
|
-
try {
|
|
563
|
-
for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
564
|
-
var parameterName = _f.value;
|
|
565
|
-
parameterNames.add(parameterName);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
569
|
-
finally {
|
|
570
|
-
try {
|
|
571
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
572
|
-
}
|
|
573
|
-
finally { if (e_2) throw e_2.error; }
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
return parameterNames;
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* TODO: [🔣] If script require contentLanguage
|
|
596
|
+
* Counts number of paragraphs in the text
|
|
580
597
|
*/
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
/*
|
|
584
|
-
TODO: Tests
|
|
585
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
586
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
587
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
588
|
-
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
589
|
-
*/
|
|
590
|
-
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
591
|
-
var e_1, _a;
|
|
592
|
-
var charType;
|
|
593
|
-
var lastCharType = 'OTHER';
|
|
594
|
-
var normalizedName = '';
|
|
595
|
-
try {
|
|
596
|
-
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
597
|
-
var char = sentence_1_1.value;
|
|
598
|
-
var normalizedChar = void 0;
|
|
599
|
-
if (/^[a-z]$/.test(char)) {
|
|
600
|
-
charType = 'LOWERCASE';
|
|
601
|
-
normalizedChar = char.toUpperCase();
|
|
602
|
-
}
|
|
603
|
-
else if (/^[A-Z]$/.test(char)) {
|
|
604
|
-
charType = 'UPPERCASE';
|
|
605
|
-
normalizedChar = char;
|
|
606
|
-
}
|
|
607
|
-
else if (/^[0-9]$/.test(char)) {
|
|
608
|
-
charType = 'NUMBER';
|
|
609
|
-
normalizedChar = char;
|
|
610
|
-
}
|
|
611
|
-
else if (/^\/$/.test(char)) {
|
|
612
|
-
charType = 'SLASH';
|
|
613
|
-
normalizedChar = char;
|
|
614
|
-
}
|
|
615
|
-
else {
|
|
616
|
-
charType = 'OTHER';
|
|
617
|
-
normalizedChar = '_';
|
|
618
|
-
}
|
|
619
|
-
if (charType !== lastCharType &&
|
|
620
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
621
|
-
!(lastCharType === 'NUMBER') &&
|
|
622
|
-
!(charType === 'NUMBER')) {
|
|
623
|
-
normalizedName += '_';
|
|
624
|
-
}
|
|
625
|
-
normalizedName += normalizedChar;
|
|
626
|
-
lastCharType = charType;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
630
|
-
finally {
|
|
631
|
-
try {
|
|
632
|
-
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
633
|
-
}
|
|
634
|
-
finally { if (e_1) throw e_1.error; }
|
|
635
|
-
}
|
|
636
|
-
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
637
|
-
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
638
|
-
normalizedName = normalizedName.replace(/^_/, '');
|
|
639
|
-
normalizedName = normalizedName.replace(/_$/, '');
|
|
640
|
-
return normalizedName;
|
|
598
|
+
function countParagraphs(text) {
|
|
599
|
+
return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
|
|
641
600
|
}
|
|
642
|
-
/**
|
|
643
|
-
* TODO: [🌺] Use some intermediate util splitWords
|
|
644
|
-
*/
|
|
645
|
-
|
|
646
|
-
/**
|
|
647
|
-
* Execution type describes the way how the block is executed
|
|
648
|
-
*
|
|
649
|
-
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
650
|
-
*/
|
|
651
|
-
var ExecutionTypes = [
|
|
652
|
-
'PROMPT_TEMPLATE',
|
|
653
|
-
'SIMPLE_TEMPLATE',
|
|
654
|
-
'SCRIPT',
|
|
655
|
-
'PROMPT_DIALOG',
|
|
656
|
-
// <- [🥻] Insert here when making new command
|
|
657
|
-
];
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Units of text measurement
|
|
661
|
-
*/
|
|
662
|
-
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
|
|
663
|
-
/**
|
|
664
|
-
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
665
|
-
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
666
|
-
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
667
|
-
*/
|
|
668
601
|
|
|
669
602
|
/**
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
* @param {string} str - The string to remove Markdown tags from.
|
|
673
|
-
* @returns {string} The input string with all Markdown tags removed.
|
|
603
|
+
* Split text into sentences
|
|
674
604
|
*/
|
|
675
|
-
function
|
|
676
|
-
|
|
677
|
-
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
678
|
-
// Remove italic formatting
|
|
679
|
-
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
680
|
-
// Remove code formatting
|
|
681
|
-
str = str.replace(/`(.*?)`/g, '$1');
|
|
682
|
-
return str;
|
|
605
|
+
function splitIntoSentences(text) {
|
|
606
|
+
return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
|
|
683
607
|
}
|
|
684
|
-
|
|
685
608
|
/**
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
689
|
-
* Note: it also works only with decimal numbers
|
|
690
|
-
*
|
|
691
|
-
* @returns parsed number
|
|
692
|
-
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
693
|
-
*
|
|
694
|
-
* @private within the parseCommand
|
|
609
|
+
* Counts number of sentences in the text
|
|
695
610
|
*/
|
|
696
|
-
function
|
|
697
|
-
|
|
698
|
-
if (typeof value === 'number') {
|
|
699
|
-
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
700
|
-
}
|
|
701
|
-
if (typeof value !== 'string') {
|
|
702
|
-
return 0;
|
|
703
|
-
}
|
|
704
|
-
value = value.trim();
|
|
705
|
-
if (value.startsWith('+')) {
|
|
706
|
-
return parseNumber(value.substring(1));
|
|
707
|
-
}
|
|
708
|
-
if (value.startsWith('-')) {
|
|
709
|
-
var number = parseNumber(value.substring(1));
|
|
710
|
-
if (number === 0) {
|
|
711
|
-
return 0; // <- Note: To prevent -0
|
|
712
|
-
}
|
|
713
|
-
return -number;
|
|
714
|
-
}
|
|
715
|
-
value = value.replace(/,/g, '.');
|
|
716
|
-
value = value.toUpperCase();
|
|
717
|
-
if (value === '') {
|
|
718
|
-
return 0;
|
|
719
|
-
}
|
|
720
|
-
if (value === '♾' || value.startsWith('INF')) {
|
|
721
|
-
return Infinity;
|
|
722
|
-
}
|
|
723
|
-
if (value.includes('/')) {
|
|
724
|
-
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
725
|
-
var numerator = parseNumber(numerator_);
|
|
726
|
-
var denominator = parseNumber(denominator_);
|
|
727
|
-
if (denominator === 0) {
|
|
728
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
729
|
-
}
|
|
730
|
-
return numerator / denominator;
|
|
731
|
-
}
|
|
732
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
733
|
-
return 0;
|
|
734
|
-
}
|
|
735
|
-
if (value.includes('E')) {
|
|
736
|
-
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
737
|
-
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
738
|
-
}
|
|
739
|
-
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
740
|
-
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
741
|
-
}
|
|
742
|
-
var num = parseFloat(value);
|
|
743
|
-
if (isNaN(num)) {
|
|
744
|
-
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
745
|
-
}
|
|
746
|
-
return num;
|
|
747
|
-
}
|
|
748
|
-
/**
|
|
749
|
-
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
750
|
-
*/
|
|
751
|
-
|
|
752
|
-
/**
|
|
753
|
-
* Parses one line of ul/ol to command
|
|
754
|
-
*
|
|
755
|
-
* @returns parsed command object
|
|
756
|
-
* @throws {PromptbookSyntaxError} if the command is invalid
|
|
757
|
-
*
|
|
758
|
-
* @private within the promptbookStringToJson
|
|
759
|
-
*/
|
|
760
|
-
function parseCommand(listItem) {
|
|
761
|
-
var e_1, _a;
|
|
762
|
-
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
763
|
-
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
764
|
-
}
|
|
765
|
-
var type = listItem.trim();
|
|
766
|
-
type = type.split('`').join('');
|
|
767
|
-
type = type.split('"').join('');
|
|
768
|
-
type = type.split("'").join('');
|
|
769
|
-
type = type.split('~').join('');
|
|
770
|
-
type = type.split('[').join('');
|
|
771
|
-
type = type.split(']').join('');
|
|
772
|
-
type = type.split('(').join('');
|
|
773
|
-
type = type.split(')').join('');
|
|
774
|
-
type = normalizeTo_SCREAMING_CASE(type);
|
|
775
|
-
type = type.split('DIALOGUE').join('DIALOG');
|
|
776
|
-
var listItemParts = listItem
|
|
777
|
-
.split(' ')
|
|
778
|
-
.map(function (part) { return part.trim(); })
|
|
779
|
-
.filter(function (item) { return item !== ''; })
|
|
780
|
-
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
781
|
-
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
782
|
-
.map(removeMarkdownFormatting);
|
|
783
|
-
if (type.startsWith('URL') ||
|
|
784
|
-
type.startsWith('PTBK_URL') ||
|
|
785
|
-
type.startsWith('PTBKURL') ||
|
|
786
|
-
type.startsWith('PROMPTBOOK_URL') ||
|
|
787
|
-
type.startsWith('PROMPTBOOKURL') ||
|
|
788
|
-
type.startsWith('HTTPS')) {
|
|
789
|
-
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
790
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
791
|
-
}
|
|
792
|
-
var promptbookUrlString = listItemParts.pop();
|
|
793
|
-
var promptbookUrl = new URL(promptbookUrlString);
|
|
794
|
-
if (promptbookUrl.protocol !== 'https:') {
|
|
795
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
796
|
-
}
|
|
797
|
-
if (promptbookUrl.hash !== '') {
|
|
798
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
799
|
-
}
|
|
800
|
-
return {
|
|
801
|
-
type: 'PROMPTBOOK_URL',
|
|
802
|
-
promptbookUrl: promptbookUrl,
|
|
803
|
-
};
|
|
804
|
-
}
|
|
805
|
-
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
806
|
-
if (listItemParts.length !== 2) {
|
|
807
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
808
|
-
}
|
|
809
|
-
var promptbookVersion = listItemParts.pop();
|
|
810
|
-
// TODO: Validate version
|
|
811
|
-
return {
|
|
812
|
-
type: 'PROMPTBOOK_VERSION',
|
|
813
|
-
promptbookVersion: promptbookVersion,
|
|
814
|
-
};
|
|
815
|
-
}
|
|
816
|
-
else if (type.startsWith('EXECUTE') ||
|
|
817
|
-
type.startsWith('EXEC') ||
|
|
818
|
-
type.startsWith('PROMPT_DIALOG') ||
|
|
819
|
-
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
820
|
-
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
821
|
-
if (executionTypes.length !== 1) {
|
|
822
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
823
|
-
}
|
|
824
|
-
return {
|
|
825
|
-
type: 'EXECUTE',
|
|
826
|
-
executionType: executionTypes[0],
|
|
827
|
-
};
|
|
828
|
-
}
|
|
829
|
-
else if (type.startsWith('MODEL')) {
|
|
830
|
-
// TODO: Make this more elegant and dynamically
|
|
831
|
-
if (type.startsWith('MODEL_VARIANT')) {
|
|
832
|
-
if (type === 'MODEL_VARIANT_CHAT') {
|
|
833
|
-
return {
|
|
834
|
-
type: 'MODEL',
|
|
835
|
-
key: 'modelVariant',
|
|
836
|
-
value: 'CHAT',
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
|
-
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
840
|
-
return {
|
|
841
|
-
type: 'MODEL',
|
|
842
|
-
key: 'modelVariant',
|
|
843
|
-
value: 'COMPLETION',
|
|
844
|
-
};
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
if (type.startsWith('MODEL_NAME')) {
|
|
851
|
-
return {
|
|
852
|
-
type: 'MODEL',
|
|
853
|
-
key: 'modelName',
|
|
854
|
-
value: listItemParts.pop(),
|
|
855
|
-
};
|
|
856
|
-
}
|
|
857
|
-
else {
|
|
858
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
else if (type.startsWith('PARAM') ||
|
|
862
|
-
type.startsWith('INPUT_PARAM') ||
|
|
863
|
-
type.startsWith('OUTPUT_PARAM') ||
|
|
864
|
-
listItem.startsWith('{') ||
|
|
865
|
-
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
866
|
-
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
867
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
868
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
869
|
-
}
|
|
870
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
871
|
-
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
872
|
-
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
873
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
874
|
-
}
|
|
875
|
-
var isInput = type.startsWith('INPUT');
|
|
876
|
-
var isOutput = type.startsWith('OUTPUT');
|
|
877
|
-
if (listItem.startsWith('> {')) {
|
|
878
|
-
isInput = false;
|
|
879
|
-
isOutput = false;
|
|
880
|
-
}
|
|
881
|
-
return {
|
|
882
|
-
type: 'PARAMETER',
|
|
883
|
-
parameterName: parameterName,
|
|
884
|
-
parameterDescription: parameterDescription.trim() || null,
|
|
885
|
-
isInput: isInput,
|
|
886
|
-
isOutput: isOutput,
|
|
887
|
-
};
|
|
888
|
-
}
|
|
889
|
-
else if (type.startsWith('JOKER')) {
|
|
890
|
-
if (listItemParts.length !== 2) {
|
|
891
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
892
|
-
}
|
|
893
|
-
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
894
|
-
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
895
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
896
|
-
}
|
|
897
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
898
|
-
var parameterName = parametersMatch.groups.parameterName;
|
|
899
|
-
return {
|
|
900
|
-
type: 'JOKER',
|
|
901
|
-
parameterName: parameterName,
|
|
902
|
-
};
|
|
903
|
-
}
|
|
904
|
-
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
905
|
-
if (listItemParts.length !== 2) {
|
|
906
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
907
|
-
}
|
|
908
|
-
var functionName = listItemParts.pop();
|
|
909
|
-
return {
|
|
910
|
-
type: 'POSTPROCESS',
|
|
911
|
-
functionName: functionName,
|
|
912
|
-
};
|
|
913
|
-
}
|
|
914
|
-
else if (type.startsWith('EXPECT_JSON')) {
|
|
915
|
-
return {
|
|
916
|
-
type: 'EXPECT_FORMAT',
|
|
917
|
-
format: 'JSON',
|
|
918
|
-
};
|
|
919
|
-
// [🥤]
|
|
920
|
-
}
|
|
921
|
-
else if (type.startsWith('EXPECT')) {
|
|
922
|
-
try {
|
|
923
|
-
listItemParts.shift();
|
|
924
|
-
var sign = void 0;
|
|
925
|
-
var signRaw = listItemParts.shift();
|
|
926
|
-
if (/^exact/i.test(signRaw)) {
|
|
927
|
-
sign = 'EXACTLY';
|
|
928
|
-
}
|
|
929
|
-
else if (/^min/i.test(signRaw)) {
|
|
930
|
-
sign = 'MINIMUM';
|
|
931
|
-
}
|
|
932
|
-
else if (/^max/i.test(signRaw)) {
|
|
933
|
-
sign = 'MAXIMUM';
|
|
934
|
-
}
|
|
935
|
-
else {
|
|
936
|
-
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
937
|
-
}
|
|
938
|
-
var amountRaw = listItemParts.shift();
|
|
939
|
-
var amount = parseNumber(amountRaw);
|
|
940
|
-
if (amount < 0) {
|
|
941
|
-
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
942
|
-
}
|
|
943
|
-
if (amount !== Math.floor(amount)) {
|
|
944
|
-
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
945
|
-
}
|
|
946
|
-
var unitRaw = listItemParts.shift();
|
|
947
|
-
var unit = undefined;
|
|
948
|
-
try {
|
|
949
|
-
for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
|
|
950
|
-
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
951
|
-
var existingUnitText = existingUnit;
|
|
952
|
-
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
953
|
-
if (existingUnitText === 'CHARACTER') {
|
|
954
|
-
existingUnitText = 'CHAR';
|
|
955
|
-
}
|
|
956
|
-
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
957
|
-
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
958
|
-
if (unit !== undefined) {
|
|
959
|
-
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
960
|
-
}
|
|
961
|
-
unit = existingUnit;
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
966
|
-
finally {
|
|
967
|
-
try {
|
|
968
|
-
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
969
|
-
}
|
|
970
|
-
finally { if (e_1) throw e_1.error; }
|
|
971
|
-
}
|
|
972
|
-
if (unit === undefined) {
|
|
973
|
-
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
974
|
-
}
|
|
975
|
-
return {
|
|
976
|
-
type: 'EXPECT_AMOUNT',
|
|
977
|
-
sign: sign,
|
|
978
|
-
unit: unit,
|
|
979
|
-
amount: amount,
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
catch (error) {
|
|
983
|
-
if (!(error instanceof Error)) {
|
|
984
|
-
throw error;
|
|
985
|
-
}
|
|
986
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
987
|
-
}
|
|
988
|
-
/*
|
|
989
|
-
} else if (type.startsWith('__________________')) {
|
|
990
|
-
// <- [🥻] Insert here when making new command
|
|
991
|
-
*/
|
|
992
|
-
}
|
|
993
|
-
else {
|
|
994
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
995
|
-
}
|
|
611
|
+
function countSentences(text) {
|
|
612
|
+
return splitIntoSentences(text).length;
|
|
996
613
|
}
|
|
997
614
|
|
|
998
615
|
var defaultDiacriticsRemovalMap = [
|
|
@@ -1247,674 +864,744 @@ function removeDiacritics(input) {
|
|
|
1247
864
|
});
|
|
1248
865
|
}
|
|
1249
866
|
|
|
1250
|
-
|
|
1251
|
-
|
|
867
|
+
/**
|
|
868
|
+
* Counts number of words in the text
|
|
869
|
+
*/
|
|
870
|
+
function countWords(text) {
|
|
871
|
+
text = text.replace(/[\p{Extended_Pictographic}]/gu, 'a');
|
|
872
|
+
text = removeDiacritics(text);
|
|
873
|
+
return text.split(/[^a-zа-я0-9]+/i).filter(function (word) { return word.length > 0; }).length;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Index of all counter functions
|
|
878
|
+
*/
|
|
879
|
+
var CountUtils = {
|
|
880
|
+
CHARACTERS: countCharacters,
|
|
881
|
+
WORDS: countWords,
|
|
882
|
+
SENTENCES: countSentences,
|
|
883
|
+
PARAGRAPHS: countParagraphs,
|
|
884
|
+
LINES: countLines,
|
|
885
|
+
PAGES: countPages,
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Function checkExpectations will check if the expectations on given value are met
|
|
890
|
+
*
|
|
891
|
+
* Note: There are two simmilar functions:
|
|
892
|
+
* - `checkExpectations` which throws an error if the expectations are not met
|
|
893
|
+
* - `isPassingExpectations` which returns a boolean
|
|
894
|
+
*
|
|
895
|
+
* @throws {ExpectError} if the expectations are not met
|
|
896
|
+
* @returns {void} Nothing
|
|
897
|
+
*/
|
|
898
|
+
function checkExpectations(expectations, value) {
|
|
1252
899
|
var e_1, _a;
|
|
1253
|
-
sentence = removeDiacritics(sentence);
|
|
1254
|
-
var charType;
|
|
1255
|
-
var lastCharType = 'OTHER';
|
|
1256
|
-
var normalizedName = '';
|
|
1257
900
|
try {
|
|
1258
|
-
for (var
|
|
1259
|
-
var
|
|
1260
|
-
var
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
charType = 'UPPERCASE';
|
|
1267
|
-
normalizedChar = char.toLowerCase();
|
|
1268
|
-
}
|
|
1269
|
-
else if (/^[0-9]$/.test(char)) {
|
|
1270
|
-
charType = 'NUMBER';
|
|
1271
|
-
normalizedChar = char;
|
|
1272
|
-
}
|
|
1273
|
-
else if (/^\/$/.test(char)) {
|
|
1274
|
-
charType = 'SLASH';
|
|
1275
|
-
normalizedChar = char;
|
|
1276
|
-
}
|
|
1277
|
-
else {
|
|
1278
|
-
charType = 'OTHER';
|
|
1279
|
-
normalizedChar = '-';
|
|
901
|
+
for (var _b = __values(Object.entries(expectations)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
902
|
+
var _d = __read(_c.value, 2), unit = _d[0], _e = _d[1], max = _e.max, min = _e.min;
|
|
903
|
+
var amount = CountUtils[unit.toUpperCase()](value);
|
|
904
|
+
if (min && amount < min) {
|
|
905
|
+
throw new ExpectError("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
|
|
906
|
+
} /* not else */
|
|
907
|
+
if (max && amount > max) {
|
|
908
|
+
throw new ExpectError("Expected at most ".concat(max, " ").concat(unit, " but got ").concat(amount));
|
|
1280
909
|
}
|
|
1281
|
-
if (charType !== lastCharType &&
|
|
1282
|
-
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
1283
|
-
!(lastCharType === 'NUMBER') &&
|
|
1284
|
-
!(charType === 'NUMBER')) {
|
|
1285
|
-
normalizedName += '-';
|
|
1286
|
-
}
|
|
1287
|
-
normalizedName += normalizedChar;
|
|
1288
|
-
lastCharType = charType;
|
|
1289
910
|
}
|
|
1290
911
|
}
|
|
1291
912
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1292
913
|
finally {
|
|
1293
914
|
try {
|
|
1294
|
-
if (
|
|
915
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1295
916
|
}
|
|
1296
917
|
finally { if (e_1) throw e_1.error; }
|
|
1297
918
|
}
|
|
1298
|
-
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
1299
|
-
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
1300
|
-
normalizedName = normalizedName.replace(/^-/, '');
|
|
1301
|
-
normalizedName = normalizedName.replace(/-$/, '');
|
|
1302
|
-
return normalizedName;
|
|
1303
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* TODO: [💝] Unite object for expecting amount and format
|
|
922
|
+
*/
|
|
1304
923
|
|
|
1305
924
|
/**
|
|
1306
|
-
*
|
|
925
|
+
* This error occurs during the parameter replacement in the template
|
|
1307
926
|
*
|
|
1308
|
-
*
|
|
1309
|
-
* @returns text without emojis
|
|
927
|
+
* Note: This is a kindof subtype of PromptbookExecutionError because it occurs during the execution of the pipeline
|
|
1310
928
|
*/
|
|
1311
|
-
function
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
}
|
|
929
|
+
var TemplateError = /** @class */ (function (_super) {
|
|
930
|
+
__extends(TemplateError, _super);
|
|
931
|
+
function TemplateError(message) {
|
|
932
|
+
var _this = _super.call(this, message) || this;
|
|
933
|
+
_this.name = 'TemplateError';
|
|
934
|
+
Object.setPrototypeOf(_this, TemplateError.prototype);
|
|
935
|
+
return _this;
|
|
936
|
+
}
|
|
937
|
+
return TemplateError;
|
|
938
|
+
}(Error));
|
|
1319
939
|
|
|
1320
940
|
/**
|
|
1321
|
-
*
|
|
941
|
+
* Replaces parameters in template with values from parameters object
|
|
942
|
+
*
|
|
943
|
+
* @param template the template with parameters in {curly} braces
|
|
944
|
+
* @param parameters the object with parameters
|
|
945
|
+
* @returns the template with replaced parameters
|
|
946
|
+
* @throws {TemplateError} if parameter is not defined, not closed, or not opened
|
|
947
|
+
*
|
|
948
|
+
* @private within the createPromptbookExecutor
|
|
1322
949
|
*/
|
|
1323
|
-
function
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
950
|
+
function replaceParameters(template, parameters) {
|
|
951
|
+
var replacedTemplate = template;
|
|
952
|
+
var match;
|
|
953
|
+
var loopLimit = LOOP_LIMIT;
|
|
954
|
+
var _loop_1 = function () {
|
|
955
|
+
if (loopLimit-- < 0) {
|
|
956
|
+
throw new UnexpectedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
957
|
+
}
|
|
958
|
+
var precol = match.groups.precol;
|
|
959
|
+
var parameterName = match.groups.parameterName;
|
|
960
|
+
if (parameterName === '') {
|
|
961
|
+
return "continue";
|
|
962
|
+
}
|
|
963
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
964
|
+
throw new TemplateError('Parameter is already opened or not closed');
|
|
965
|
+
}
|
|
966
|
+
if (parameters[parameterName] === undefined) {
|
|
967
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
968
|
+
}
|
|
969
|
+
var parameterValue = parameters[parameterName];
|
|
970
|
+
if (parameterValue === undefined) {
|
|
971
|
+
throw new TemplateError("Parameter {".concat(parameterName, "} is not defined"));
|
|
972
|
+
}
|
|
973
|
+
parameterValue = parameterValue.toString();
|
|
974
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
975
|
+
parameterValue = parameterValue
|
|
976
|
+
.split('\n')
|
|
977
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
978
|
+
.join('\n');
|
|
979
|
+
}
|
|
980
|
+
replacedTemplate =
|
|
981
|
+
replacedTemplate.substring(0, match.index + precol.length) +
|
|
982
|
+
parameterValue +
|
|
983
|
+
replacedTemplate.substring(match.index + precol.length + parameterName.length + 2);
|
|
984
|
+
};
|
|
985
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
986
|
+
.exec(replacedTemplate))) {
|
|
987
|
+
_loop_1();
|
|
988
|
+
}
|
|
989
|
+
// [💫] Check if there are parameters that are not closed properly
|
|
990
|
+
if (/{\w+$/.test(replacedTemplate)) {
|
|
991
|
+
throw new TemplateError('Parameter is not closed');
|
|
992
|
+
}
|
|
993
|
+
// [💫] Check if there are parameters that are not opened properly
|
|
994
|
+
if (/^\w+}/.test(replacedTemplate)) {
|
|
995
|
+
throw new TemplateError('Parameter is not opened');
|
|
996
|
+
}
|
|
997
|
+
return replacedTemplate;
|
|
1328
998
|
}
|
|
1329
999
|
|
|
1330
1000
|
/**
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
1333
|
-
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
1334
|
-
* @param options - Options and tools for the compilation
|
|
1335
|
-
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
1336
|
-
* @throws {PromptbookSyntaxError} if the promptbook string is not valid
|
|
1001
|
+
* Creates executor function from promptbook and execution tools.
|
|
1337
1002
|
*
|
|
1338
|
-
*
|
|
1339
|
-
*
|
|
1003
|
+
* @returns The executor function
|
|
1004
|
+
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1340
1005
|
*/
|
|
1341
|
-
function
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1006
|
+
function createPromptbookExecutor(options) {
|
|
1007
|
+
var _this = this;
|
|
1008
|
+
var promptbook = options.promptbook, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
|
|
1009
|
+
var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
|
|
1010
|
+
validatePromptbook(promptbook);
|
|
1011
|
+
var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
|
|
1012
|
+
function executeSingleTemplate(currentTemplate) {
|
|
1013
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1014
|
+
var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, 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;
|
|
1015
|
+
var e_2, _j, e_4, _k, e_3, _l, _m;
|
|
1016
|
+
var _this = this;
|
|
1017
|
+
return __generator(this, function (_o) {
|
|
1018
|
+
switch (_o.label) {
|
|
1019
|
+
case 0:
|
|
1020
|
+
name = "promptbook-executor-frame-".concat(currentTemplate.name);
|
|
1021
|
+
title = currentTemplate.title;
|
|
1022
|
+
priority = promptbook.promptTemplates.length - promptbook.promptTemplates.indexOf(currentTemplate);
|
|
1023
|
+
if (!onProgress /* <- [3] */) return [3 /*break*/, 2]; /* <- [3] */
|
|
1024
|
+
return [4 /*yield*/, onProgress({
|
|
1025
|
+
name: name,
|
|
1026
|
+
title: title,
|
|
1027
|
+
isStarted: false,
|
|
1028
|
+
isDone: false,
|
|
1029
|
+
executionType: currentTemplate.executionType,
|
|
1030
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1031
|
+
parameterValue: null,
|
|
1032
|
+
// <- [3]
|
|
1033
|
+
})];
|
|
1034
|
+
case 1:
|
|
1035
|
+
_o.sent();
|
|
1036
|
+
_o.label = 2;
|
|
1037
|
+
case 2:
|
|
1038
|
+
result = null;
|
|
1039
|
+
resultString = null;
|
|
1040
|
+
expectError = null;
|
|
1041
|
+
maxAttempts = currentTemplate.executionType === 'PROMPT_DIALOG' ? Infinity : maxExecutionAttempts;
|
|
1042
|
+
jokers = currentTemplate.jokers || [];
|
|
1043
|
+
attempt = -jokers.length;
|
|
1044
|
+
_o.label = 3;
|
|
1045
|
+
case 3:
|
|
1046
|
+
if (!(attempt < maxAttempts)) return [3 /*break*/, 49];
|
|
1047
|
+
isJokerAttempt = attempt < 0;
|
|
1048
|
+
joker = jokers[jokers.length + attempt];
|
|
1049
|
+
if (isJokerAttempt && !joker) {
|
|
1050
|
+
throw new UnexpectedError("Joker not found in attempt ".concat(attempt));
|
|
1051
|
+
}
|
|
1052
|
+
result = null;
|
|
1053
|
+
resultString = null;
|
|
1054
|
+
expectError = null;
|
|
1055
|
+
if (isJokerAttempt) {
|
|
1056
|
+
if (typeof parametersToPass[joker] === 'undefined') {
|
|
1057
|
+
throw new PromptbookExecutionError("Joker parameter {".concat(joker, "} not defined"));
|
|
1058
|
+
}
|
|
1059
|
+
resultString = parametersToPass[joker];
|
|
1060
|
+
}
|
|
1061
|
+
_o.label = 4;
|
|
1062
|
+
case 4:
|
|
1063
|
+
_o.trys.push([4, 45, 46, 47]);
|
|
1064
|
+
if (!!isJokerAttempt) return [3 /*break*/, 27];
|
|
1065
|
+
_a = currentTemplate.executionType;
|
|
1066
|
+
switch (_a) {
|
|
1067
|
+
case 'SIMPLE_TEMPLATE': return [3 /*break*/, 5];
|
|
1068
|
+
case 'PROMPT_TEMPLATE': return [3 /*break*/, 6];
|
|
1069
|
+
case 'SCRIPT': return [3 /*break*/, 13];
|
|
1070
|
+
case 'PROMPT_DIALOG': return [3 /*break*/, 24];
|
|
1071
|
+
}
|
|
1072
|
+
return [3 /*break*/, 26];
|
|
1073
|
+
case 5:
|
|
1074
|
+
resultString = replaceParameters(currentTemplate.content, parametersToPass);
|
|
1075
|
+
return [3 /*break*/, 27];
|
|
1076
|
+
case 6:
|
|
1077
|
+
prompt = {
|
|
1078
|
+
title: currentTemplate.title,
|
|
1079
|
+
promptbookUrl: "".concat(promptbook.promptbookUrl
|
|
1080
|
+
? promptbook.promptbookUrl
|
|
1081
|
+
: 'anonymous' /* <- TODO: [🧠] How to deal with anonymous PROMPTBOOKs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
|
|
1082
|
+
parameters: parametersToPass,
|
|
1083
|
+
content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
|
|
1084
|
+
modelRequirements: currentTemplate.modelRequirements,
|
|
1085
|
+
expectations: currentTemplate.expectations,
|
|
1086
|
+
expectFormat: currentTemplate.expectFormat,
|
|
1087
|
+
postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
1088
|
+
var errors, _a, _b, scriptTools, error_5, e_5_1;
|
|
1089
|
+
var e_5, _c;
|
|
1090
|
+
return __generator(this, function (_d) {
|
|
1091
|
+
switch (_d.label) {
|
|
1092
|
+
case 0:
|
|
1093
|
+
errors = [];
|
|
1094
|
+
_d.label = 1;
|
|
1095
|
+
case 1:
|
|
1096
|
+
_d.trys.push([1, 8, 9, 10]);
|
|
1097
|
+
_a = __values(tools.script), _b = _a.next();
|
|
1098
|
+
_d.label = 2;
|
|
1099
|
+
case 2:
|
|
1100
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
1101
|
+
scriptTools = _b.value;
|
|
1102
|
+
_d.label = 3;
|
|
1103
|
+
case 3:
|
|
1104
|
+
_d.trys.push([3, 5, , 6]);
|
|
1105
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1106
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1107
|
+
script: "".concat(functionName, "(result)"),
|
|
1108
|
+
parameters: {
|
|
1109
|
+
result: result || '',
|
|
1110
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1111
|
+
},
|
|
1112
|
+
})];
|
|
1113
|
+
case 4: return [2 /*return*/, _d.sent()];
|
|
1114
|
+
case 5:
|
|
1115
|
+
error_5 = _d.sent();
|
|
1116
|
+
if (!(error_5 instanceof Error)) {
|
|
1117
|
+
throw error_5;
|
|
1118
|
+
}
|
|
1119
|
+
errors.push(error_5);
|
|
1120
|
+
return [3 /*break*/, 6];
|
|
1121
|
+
case 6:
|
|
1122
|
+
_b = _a.next();
|
|
1123
|
+
return [3 /*break*/, 2];
|
|
1124
|
+
case 7: return [3 /*break*/, 10];
|
|
1125
|
+
case 8:
|
|
1126
|
+
e_5_1 = _d.sent();
|
|
1127
|
+
e_5 = { error: e_5_1 };
|
|
1128
|
+
return [3 /*break*/, 10];
|
|
1129
|
+
case 9:
|
|
1130
|
+
try {
|
|
1131
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1132
|
+
}
|
|
1133
|
+
finally { if (e_5) throw e_5.error; }
|
|
1134
|
+
return [7 /*endfinally*/];
|
|
1135
|
+
case 10:
|
|
1136
|
+
if (errors.length === 0) {
|
|
1137
|
+
throw new PromptbookExecutionError('Postprocessing in LlmExecutionTools failed because no ScriptExecutionTools were provided');
|
|
1138
|
+
}
|
|
1139
|
+
else if (errors.length === 1) {
|
|
1140
|
+
throw errors[0];
|
|
1141
|
+
}
|
|
1142
|
+
else {
|
|
1143
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
}); }; }),
|
|
1148
|
+
};
|
|
1149
|
+
_b = currentTemplate.modelRequirements.modelVariant;
|
|
1150
|
+
switch (_b) {
|
|
1151
|
+
case 'CHAT': return [3 /*break*/, 7];
|
|
1152
|
+
case 'COMPLETION': return [3 /*break*/, 9];
|
|
1153
|
+
}
|
|
1154
|
+
return [3 /*break*/, 11];
|
|
1155
|
+
case 7: return [4 /*yield*/, tools.llm.gptChat(prompt)];
|
|
1156
|
+
case 8:
|
|
1157
|
+
chatThread = _o.sent();
|
|
1158
|
+
// TODO: [🍬] Destroy chatThread
|
|
1159
|
+
result = chatThread;
|
|
1160
|
+
resultString = chatThread.content;
|
|
1161
|
+
return [3 /*break*/, 12];
|
|
1162
|
+
case 9: return [4 /*yield*/, tools.llm.gptComplete(prompt)];
|
|
1163
|
+
case 10:
|
|
1164
|
+
completionResult = _o.sent();
|
|
1165
|
+
result = completionResult;
|
|
1166
|
+
resultString = completionResult.content;
|
|
1167
|
+
return [3 /*break*/, 12];
|
|
1168
|
+
case 11: throw new PromptbookExecutionError("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
|
|
1169
|
+
case 12: return [3 /*break*/, 27];
|
|
1170
|
+
case 13:
|
|
1171
|
+
if (tools.script.length === 0) {
|
|
1172
|
+
throw new PromptbookExecutionError('No script execution tools are available');
|
|
1173
|
+
}
|
|
1174
|
+
if (!currentTemplate.contentLanguage) {
|
|
1175
|
+
throw new PromptbookExecutionError("Script language is not defined for prompt template \"".concat(currentTemplate.name, "\""));
|
|
1176
|
+
}
|
|
1177
|
+
// TODO: DRY [1]
|
|
1178
|
+
scriptExecutionErrors = [];
|
|
1179
|
+
_o.label = 14;
|
|
1180
|
+
case 14:
|
|
1181
|
+
_o.trys.push([14, 21, 22, 23]);
|
|
1182
|
+
_c = (e_2 = void 0, __values(tools.script)), _d = _c.next();
|
|
1183
|
+
_o.label = 15;
|
|
1184
|
+
case 15:
|
|
1185
|
+
if (!!_d.done) return [3 /*break*/, 20];
|
|
1186
|
+
scriptTools = _d.value;
|
|
1187
|
+
_o.label = 16;
|
|
1188
|
+
case 16:
|
|
1189
|
+
_o.trys.push([16, 18, , 19]);
|
|
1190
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1191
|
+
scriptLanguage: currentTemplate.contentLanguage,
|
|
1192
|
+
script: currentTemplate.content,
|
|
1193
|
+
parameters: parametersToPass,
|
|
1194
|
+
})];
|
|
1195
|
+
case 17:
|
|
1196
|
+
resultString = _o.sent();
|
|
1197
|
+
return [3 /*break*/, 20];
|
|
1198
|
+
case 18:
|
|
1199
|
+
error_2 = _o.sent();
|
|
1200
|
+
if (!(error_2 instanceof Error)) {
|
|
1201
|
+
throw error_2;
|
|
1202
|
+
}
|
|
1203
|
+
scriptExecutionErrors.push(error_2);
|
|
1204
|
+
return [3 /*break*/, 19];
|
|
1205
|
+
case 19:
|
|
1206
|
+
_d = _c.next();
|
|
1207
|
+
return [3 /*break*/, 15];
|
|
1208
|
+
case 20: return [3 /*break*/, 23];
|
|
1209
|
+
case 21:
|
|
1210
|
+
e_2_1 = _o.sent();
|
|
1211
|
+
e_2 = { error: e_2_1 };
|
|
1212
|
+
return [3 /*break*/, 23];
|
|
1213
|
+
case 22:
|
|
1214
|
+
try {
|
|
1215
|
+
if (_d && !_d.done && (_j = _c.return)) _j.call(_c);
|
|
1216
|
+
}
|
|
1217
|
+
finally { if (e_2) throw e_2.error; }
|
|
1218
|
+
return [7 /*endfinally*/];
|
|
1219
|
+
case 23:
|
|
1220
|
+
if (resultString !== null) {
|
|
1221
|
+
return [3 /*break*/, 27];
|
|
1222
|
+
}
|
|
1223
|
+
if (scriptExecutionErrors.length === 1) {
|
|
1224
|
+
throw scriptExecutionErrors[0];
|
|
1225
|
+
}
|
|
1226
|
+
else {
|
|
1227
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors
|
|
1228
|
+
.map(function (error) { return '- ' + error.message; })
|
|
1229
|
+
.join('\n\n')), "\n "); }));
|
|
1230
|
+
}
|
|
1231
|
+
case 24:
|
|
1232
|
+
if (tools.userInterface === undefined) {
|
|
1233
|
+
throw new PromptbookExecutionError('User interface tools are not available');
|
|
1234
|
+
}
|
|
1235
|
+
return [4 /*yield*/, tools.userInterface.promptDialog({
|
|
1236
|
+
promptTitle: currentTemplate.title,
|
|
1237
|
+
promptMessage: replaceParameters(currentTemplate.description || '', parametersToPass),
|
|
1238
|
+
defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
|
|
1239
|
+
// TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
|
|
1240
|
+
placeholder: undefined,
|
|
1241
|
+
priority: priority,
|
|
1242
|
+
})];
|
|
1243
|
+
case 25:
|
|
1244
|
+
// TODO: [🌹] When making next attempt for `PROMPT DIALOG`, preserve the previous user input
|
|
1245
|
+
resultString = _o.sent();
|
|
1246
|
+
return [3 /*break*/, 27];
|
|
1247
|
+
case 26: throw new PromptbookExecutionError(
|
|
1351
1248
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1249
|
+
"Unknown execution type \"".concat(currentTemplate.executionType, "\""));
|
|
1250
|
+
case 27:
|
|
1251
|
+
if (!(!isJokerAttempt && currentTemplate.postprocessing)) return [3 /*break*/, 44];
|
|
1252
|
+
_o.label = 28;
|
|
1253
|
+
case 28:
|
|
1254
|
+
_o.trys.push([28, 42, 43, 44]);
|
|
1255
|
+
_e = (e_4 = void 0, __values(currentTemplate.postprocessing)), _f = _e.next();
|
|
1256
|
+
_o.label = 29;
|
|
1257
|
+
case 29:
|
|
1258
|
+
if (!!_f.done) return [3 /*break*/, 41];
|
|
1259
|
+
functionName = _f.value;
|
|
1260
|
+
// TODO: DRY [1]
|
|
1261
|
+
scriptExecutionErrors = [];
|
|
1262
|
+
postprocessingError = null;
|
|
1263
|
+
_o.label = 30;
|
|
1264
|
+
case 30:
|
|
1265
|
+
_o.trys.push([30, 37, 38, 39]);
|
|
1266
|
+
_g = (e_3 = void 0, __values(tools.script)), _h = _g.next();
|
|
1267
|
+
_o.label = 31;
|
|
1268
|
+
case 31:
|
|
1269
|
+
if (!!_h.done) return [3 /*break*/, 36];
|
|
1270
|
+
scriptTools = _h.value;
|
|
1271
|
+
_o.label = 32;
|
|
1272
|
+
case 32:
|
|
1273
|
+
_o.trys.push([32, 34, , 35]);
|
|
1274
|
+
return [4 /*yield*/, scriptTools.execute({
|
|
1275
|
+
scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
|
|
1276
|
+
script: "".concat(functionName, "(resultString)"),
|
|
1277
|
+
parameters: {
|
|
1278
|
+
resultString: resultString || '',
|
|
1279
|
+
// Note: No ...parametersToPass, because working with result only
|
|
1280
|
+
},
|
|
1281
|
+
})];
|
|
1282
|
+
case 33:
|
|
1283
|
+
resultString = _o.sent();
|
|
1284
|
+
postprocessingError = null;
|
|
1285
|
+
return [3 /*break*/, 36];
|
|
1286
|
+
case 34:
|
|
1287
|
+
error_3 = _o.sent();
|
|
1288
|
+
if (!(error_3 instanceof Error)) {
|
|
1289
|
+
throw error_3;
|
|
1290
|
+
}
|
|
1291
|
+
postprocessingError = error_3;
|
|
1292
|
+
scriptExecutionErrors.push(error_3);
|
|
1293
|
+
return [3 /*break*/, 35];
|
|
1294
|
+
case 35:
|
|
1295
|
+
_h = _g.next();
|
|
1296
|
+
return [3 /*break*/, 31];
|
|
1297
|
+
case 36: return [3 /*break*/, 39];
|
|
1298
|
+
case 37:
|
|
1299
|
+
e_3_1 = _o.sent();
|
|
1300
|
+
e_3 = { error: e_3_1 };
|
|
1301
|
+
return [3 /*break*/, 39];
|
|
1302
|
+
case 38:
|
|
1303
|
+
try {
|
|
1304
|
+
if (_h && !_h.done && (_l = _g.return)) _l.call(_g);
|
|
1305
|
+
}
|
|
1306
|
+
finally { if (e_3) throw e_3.error; }
|
|
1307
|
+
return [7 /*endfinally*/];
|
|
1308
|
+
case 39:
|
|
1309
|
+
if (postprocessingError) {
|
|
1310
|
+
throw postprocessingError;
|
|
1311
|
+
}
|
|
1312
|
+
_o.label = 40;
|
|
1313
|
+
case 40:
|
|
1314
|
+
_f = _e.next();
|
|
1315
|
+
return [3 /*break*/, 29];
|
|
1316
|
+
case 41: return [3 /*break*/, 44];
|
|
1317
|
+
case 42:
|
|
1318
|
+
e_4_1 = _o.sent();
|
|
1319
|
+
e_4 = { error: e_4_1 };
|
|
1320
|
+
return [3 /*break*/, 44];
|
|
1321
|
+
case 43:
|
|
1322
|
+
try {
|
|
1323
|
+
if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
|
|
1324
|
+
}
|
|
1325
|
+
finally { if (e_4) throw e_4.error; }
|
|
1326
|
+
return [7 /*endfinally*/];
|
|
1327
|
+
case 44:
|
|
1328
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1329
|
+
if (currentTemplate.expectFormat) {
|
|
1330
|
+
if (currentTemplate.expectFormat === 'JSON') {
|
|
1331
|
+
if (!isValidJsonString(resultString || '')) {
|
|
1332
|
+
throw new ExpectError('Expected valid JSON string');
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
// TODO: [💝] Unite object for expecting amount and format
|
|
1337
|
+
if (currentTemplate.expectations) {
|
|
1338
|
+
checkExpectations(currentTemplate.expectations, resultString || '');
|
|
1339
|
+
}
|
|
1340
|
+
return [3 /*break*/, 49];
|
|
1341
|
+
case 45:
|
|
1342
|
+
error_4 = _o.sent();
|
|
1343
|
+
if (!(error_4 instanceof ExpectError)) {
|
|
1344
|
+
throw error_4;
|
|
1345
|
+
}
|
|
1346
|
+
expectError = error_4;
|
|
1347
|
+
return [3 /*break*/, 47];
|
|
1348
|
+
case 46:
|
|
1349
|
+
if (!isJokerAttempt &&
|
|
1350
|
+
currentTemplate.executionType === 'PROMPT_TEMPLATE' &&
|
|
1351
|
+
prompt
|
|
1352
|
+
// <- Note: [2] When some expected parameter is not defined, error will occur in replaceParameters
|
|
1353
|
+
// In that case we don’t want to make a report about it because it’s not a llm execution error
|
|
1354
|
+
) {
|
|
1355
|
+
// TODO: [🧠] Maybe put other executionTypes into report
|
|
1356
|
+
executionReport.promptExecutions.push({
|
|
1357
|
+
prompt: {
|
|
1358
|
+
title: currentTemplate.title /* <- Note: If title in promptbook contains emojis, pass it innto report */,
|
|
1359
|
+
content: prompt.content,
|
|
1360
|
+
modelRequirements: prompt.modelRequirements,
|
|
1361
|
+
expectations: prompt.expectations,
|
|
1362
|
+
expectFormat: prompt.expectFormat,
|
|
1363
|
+
// <- Note: Do want to pass ONLY wanted information to the report
|
|
1364
|
+
},
|
|
1365
|
+
result: result || undefined,
|
|
1366
|
+
error: expectError || undefined,
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
return [7 /*endfinally*/];
|
|
1370
|
+
case 47:
|
|
1371
|
+
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
1372
|
+
throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
|
|
1373
|
+
}
|
|
1374
|
+
_o.label = 48;
|
|
1375
|
+
case 48:
|
|
1376
|
+
attempt++;
|
|
1377
|
+
return [3 /*break*/, 3];
|
|
1378
|
+
case 49:
|
|
1379
|
+
if (resultString === null) {
|
|
1380
|
+
throw new UnexpectedError('Something went wrong and prompt result is null');
|
|
1381
|
+
}
|
|
1382
|
+
if (onProgress /* <- [3] */) {
|
|
1383
|
+
onProgress({
|
|
1384
|
+
name: name,
|
|
1385
|
+
title: title,
|
|
1386
|
+
isStarted: true,
|
|
1387
|
+
isDone: true,
|
|
1388
|
+
executionType: currentTemplate.executionType,
|
|
1389
|
+
parameterName: currentTemplate.resultingParameterName,
|
|
1390
|
+
parameterValue: resultString,
|
|
1391
|
+
// <- [3]
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[currentTemplate.resultingParameterName] = resultString /* <- Note: Not need to detect parameter collision here because Promptbook checks logic consistency during construction */, _m));
|
|
1395
|
+
return [2 /*return*/];
|
|
1396
|
+
}
|
|
1397
|
+
});
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
|
|
1401
|
+
var e_1, _c;
|
|
1402
|
+
return __generator(this, function (_d) {
|
|
1403
|
+
switch (_d.label) {
|
|
1404
|
+
case 0:
|
|
1405
|
+
parametersToPass = inputParameters;
|
|
1406
|
+
executionReport = {
|
|
1407
|
+
promptbookUrl: promptbook.promptbookUrl,
|
|
1408
|
+
title: promptbook.title,
|
|
1409
|
+
promptbookUsedVersion: PROMPTBOOK_VERSION,
|
|
1410
|
+
promptbookRequestedVersion: promptbook.promptbookVersion,
|
|
1411
|
+
description: promptbook.description,
|
|
1412
|
+
promptExecutions: [],
|
|
1359
1413
|
};
|
|
1360
|
-
|
|
1361
|
-
return [4 /*yield*/, prepareKnowledgeFromMarkdown()];
|
|
1414
|
+
_d.label = 1;
|
|
1362
1415
|
case 1:
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1416
|
+
_d.trys.push([1, 6, , 7]);
|
|
1417
|
+
resovedParameters_1 = promptbook.parameters
|
|
1418
|
+
.filter(function (_a) {
|
|
1419
|
+
var isInput = _a.isInput;
|
|
1420
|
+
return isInput;
|
|
1421
|
+
})
|
|
1422
|
+
.map(function (_a) {
|
|
1423
|
+
var name = _a.name;
|
|
1424
|
+
return name;
|
|
1425
|
+
});
|
|
1426
|
+
unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
1427
|
+
resolving_1 = [];
|
|
1428
|
+
loopLimit = LOOP_LIMIT;
|
|
1429
|
+
_loop_1 = function () {
|
|
1430
|
+
var currentTemplate, work_1;
|
|
1431
|
+
return __generator(this, function (_e) {
|
|
1432
|
+
switch (_e.label) {
|
|
1433
|
+
case 0:
|
|
1434
|
+
if (loopLimit-- < 0) {
|
|
1435
|
+
throw new UnexpectedError('Loop limit reached during resolving parameters promptbook execution');
|
|
1436
|
+
}
|
|
1437
|
+
currentTemplate = unresovedTemplates.find(function (template) {
|
|
1438
|
+
return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
|
|
1439
|
+
});
|
|
1440
|
+
if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
|
|
1441
|
+
throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbook\n "));
|
|
1442
|
+
case 1:
|
|
1443
|
+
if (!!currentTemplate) return [3 /*break*/, 3];
|
|
1444
|
+
/* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
|
|
1445
|
+
case 2:
|
|
1446
|
+
/* [5] */ _e.sent();
|
|
1447
|
+
return [3 /*break*/, 4];
|
|
1448
|
+
case 3:
|
|
1449
|
+
unresovedTemplates = unresovedTemplates.filter(function (template) { return template !== currentTemplate; });
|
|
1450
|
+
work_1 = executeSingleTemplate(currentTemplate)
|
|
1451
|
+
.then(function () {
|
|
1452
|
+
resovedParameters_1 = __spreadArray(__spreadArray([], __read(resovedParameters_1), false), [currentTemplate.resultingParameterName], false);
|
|
1453
|
+
})
|
|
1454
|
+
.then(function () {
|
|
1455
|
+
resolving_1 = resolving_1.filter(function (w) { return w !== work_1; });
|
|
1456
|
+
});
|
|
1457
|
+
resolving_1.push(work_1);
|
|
1458
|
+
_e.label = 4;
|
|
1459
|
+
case 4: return [2 /*return*/];
|
|
1384
1460
|
}
|
|
1385
|
-
}
|
|
1386
|
-
else {
|
|
1387
|
-
promptbookJson.parameters.push({
|
|
1388
|
-
name: parameterName,
|
|
1389
|
-
description: parameterDescription || undefined,
|
|
1390
|
-
isInput: isInput,
|
|
1391
|
-
isOutput: isOutput,
|
|
1392
|
-
});
|
|
1393
|
-
}
|
|
1461
|
+
});
|
|
1394
1462
|
};
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
if (
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1463
|
+
_d.label = 2;
|
|
1464
|
+
case 2:
|
|
1465
|
+
if (!(unresovedTemplates.length > 0)) return [3 /*break*/, 4];
|
|
1466
|
+
return [5 /*yield**/, _loop_1()];
|
|
1467
|
+
case 3:
|
|
1468
|
+
_d.sent();
|
|
1469
|
+
return [3 /*break*/, 2];
|
|
1470
|
+
case 4: return [4 /*yield*/, Promise.all(resolving_1)];
|
|
1471
|
+
case 5:
|
|
1472
|
+
_d.sent();
|
|
1473
|
+
return [3 /*break*/, 7];
|
|
1474
|
+
case 6:
|
|
1475
|
+
error_1 = _d.sent();
|
|
1476
|
+
if (!(error_1 instanceof Error)) {
|
|
1477
|
+
throw error_1;
|
|
1409
1478
|
}
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1479
|
+
usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1480
|
+
var result = _a.result;
|
|
1481
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1482
|
+
})), false));
|
|
1483
|
+
return [2 /*return*/, {
|
|
1484
|
+
isSuccessful: false,
|
|
1485
|
+
errors: [error_1],
|
|
1486
|
+
usage: usage_1,
|
|
1487
|
+
executionReport: executionReport,
|
|
1488
|
+
outputParameters: parametersToPass,
|
|
1489
|
+
}];
|
|
1490
|
+
case 7:
|
|
1413
1491
|
try {
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
1420
|
-
break;
|
|
1421
|
-
case 'PROMPTBOOK_VERSION':
|
|
1422
|
-
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
1423
|
-
break;
|
|
1424
|
-
case 'MODEL':
|
|
1425
|
-
defaultModelRequirements[command.key] = command.value;
|
|
1426
|
-
break;
|
|
1427
|
-
case 'PARAMETER':
|
|
1428
|
-
addParam(command);
|
|
1429
|
-
break;
|
|
1430
|
-
default:
|
|
1431
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
1492
|
+
// Note: Filter ONLY output parameters
|
|
1493
|
+
for (_a = __values(promptbook.parameters), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1494
|
+
parameter = _b.value;
|
|
1495
|
+
if (parameter.isOutput) {
|
|
1496
|
+
continue;
|
|
1432
1497
|
}
|
|
1498
|
+
delete parametersToPass[parameter.name];
|
|
1433
1499
|
}
|
|
1434
1500
|
}
|
|
1435
1501
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1436
1502
|
finally {
|
|
1437
1503
|
try {
|
|
1438
|
-
if (
|
|
1504
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1439
1505
|
}
|
|
1440
1506
|
finally { if (e_1) throw e_1.error; }
|
|
1441
1507
|
}
|
|
1442
|
-
|
|
1443
|
-
var
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
var isExecutionTypeChanged = false;
|
|
1454
|
-
try {
|
|
1455
|
-
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()) {
|
|
1456
|
-
var listItem = listItems_2_1.value;
|
|
1457
|
-
var command = parseCommand(listItem);
|
|
1458
|
-
switch (command.type) {
|
|
1459
|
-
case 'JOKER':
|
|
1460
|
-
jokers.push(command.parameterName);
|
|
1461
|
-
dependentParameterNames.add(command.parameterName);
|
|
1462
|
-
break;
|
|
1463
|
-
case 'EXECUTE':
|
|
1464
|
-
if (isExecutionTypeChanged) {
|
|
1465
|
-
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
1466
|
-
}
|
|
1467
|
-
executionType = command.executionType;
|
|
1468
|
-
isExecutionTypeChanged = true;
|
|
1469
|
-
break;
|
|
1470
|
-
case 'MODEL':
|
|
1471
|
-
templateModelRequirements[command.key] = command.value;
|
|
1472
|
-
break;
|
|
1473
|
-
case 'PARAMETER':
|
|
1474
|
-
// Note: This is just for detecting resulitng parameter name
|
|
1475
|
-
addParam(command);
|
|
1476
|
-
break;
|
|
1477
|
-
case 'POSTPROCESS':
|
|
1478
|
-
postprocessing.push(command.functionName);
|
|
1479
|
-
break;
|
|
1480
|
-
case 'EXPECT_AMOUNT':
|
|
1481
|
-
// eslint-disable-next-line no-case-declarations
|
|
1482
|
-
var unit = command.unit.toLowerCase();
|
|
1483
|
-
expectAmount[unit] = expectAmount[unit] || {};
|
|
1484
|
-
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
1485
|
-
if (expectAmount[unit].min !== undefined) {
|
|
1486
|
-
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
1487
|
-
}
|
|
1488
|
-
expectAmount[unit].min = command.amount;
|
|
1489
|
-
} /* not else */
|
|
1490
|
-
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
1491
|
-
if (expectAmount[unit].max !== undefined) {
|
|
1492
|
-
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
1493
|
-
}
|
|
1494
|
-
expectAmount[unit].max = command.amount;
|
|
1495
|
-
}
|
|
1496
|
-
break;
|
|
1497
|
-
case 'EXPECT_FORMAT':
|
|
1498
|
-
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
1499
|
-
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
1500
|
-
}
|
|
1501
|
-
expectFormat = command.format;
|
|
1502
|
-
break;
|
|
1503
|
-
default:
|
|
1504
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
}
|
|
1508
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1509
|
-
finally {
|
|
1510
|
-
try {
|
|
1511
|
-
if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
|
|
1512
|
-
}
|
|
1513
|
-
finally { if (e_3) throw e_3.error; }
|
|
1514
|
-
}
|
|
1515
|
-
var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
|
|
1516
|
-
if (executionType === 'SCRIPT') {
|
|
1517
|
-
if (!language) {
|
|
1518
|
-
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
1519
|
-
}
|
|
1520
|
-
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
1521
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
var lastLine = section.content.split('\n').pop();
|
|
1525
|
-
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
1526
|
-
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
1527
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
1528
|
-
// TODO: Show code of invalid sections each time + DRY
|
|
1529
|
-
section.content
|
|
1530
|
-
.split('\n')
|
|
1531
|
-
.map(function (line) { return "> ".concat(line); })
|
|
1532
|
-
.join('\n')), "\n "); }));
|
|
1533
|
-
}
|
|
1534
|
-
var resultingParameterName = match.groups.resultingParamName;
|
|
1535
|
-
// TODO: [1] DRY description
|
|
1536
|
-
var description_1 = section.content;
|
|
1537
|
-
// Note: Remove codeblocks
|
|
1538
|
-
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
1539
|
-
//Note: Remove lists and return statement
|
|
1540
|
-
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
1541
|
-
description_1 = spaceTrim(description_1);
|
|
1542
|
-
if (description_1 === '') {
|
|
1543
|
-
description_1 = undefined;
|
|
1544
|
-
}
|
|
1545
|
-
if (Object.keys(jokers).length === 0) {
|
|
1546
|
-
jokers = undefined;
|
|
1547
|
-
}
|
|
1548
|
-
if (Object.keys(expectAmount).length === 0) {
|
|
1549
|
-
expectAmount = undefined;
|
|
1550
|
-
}
|
|
1551
|
-
if (Object.keys(postprocessing).length === 0) {
|
|
1552
|
-
postprocessing = undefined;
|
|
1553
|
-
}
|
|
1554
|
-
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
1555
|
-
if (templateModelRequirements.modelVariant === undefined) {
|
|
1556
|
-
templateModelRequirements.modelVariant = 'CHAT';
|
|
1557
|
-
}
|
|
1558
|
-
promptbookJson.promptTemplates.push({
|
|
1559
|
-
name: titleToName(section.title),
|
|
1560
|
-
title: section.title,
|
|
1561
|
-
description: description_1,
|
|
1562
|
-
dependentParameterNames: Array.from(dependentParameterNames),
|
|
1563
|
-
executionType: executionType,
|
|
1564
|
-
jokers: jokers,
|
|
1565
|
-
postprocessing: postprocessing,
|
|
1566
|
-
expectations: expectAmount,
|
|
1567
|
-
expectFormat: expectFormat,
|
|
1568
|
-
modelRequirements: templateModelRequirements,
|
|
1569
|
-
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
1570
|
-
content: content,
|
|
1571
|
-
resultingParameterName: resultingParameterName,
|
|
1572
|
-
});
|
|
1573
|
-
};
|
|
1574
|
-
try {
|
|
1575
|
-
for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1576
|
-
section = _b.value;
|
|
1577
|
-
_loop_1(section);
|
|
1578
|
-
}
|
|
1579
|
-
}
|
|
1580
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1581
|
-
finally {
|
|
1582
|
-
try {
|
|
1583
|
-
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
1584
|
-
}
|
|
1585
|
-
finally { if (e_2) throw e_2.error; }
|
|
1586
|
-
}
|
|
1587
|
-
// =============================================================
|
|
1588
|
-
return [2 /*return*/, promptbookJson];
|
|
1508
|
+
usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
|
|
1509
|
+
var result = _a.result;
|
|
1510
|
+
return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
|
|
1511
|
+
})), false));
|
|
1512
|
+
return [2 /*return*/, {
|
|
1513
|
+
isSuccessful: true,
|
|
1514
|
+
errors: [],
|
|
1515
|
+
usage: usage,
|
|
1516
|
+
executionReport: executionReport,
|
|
1517
|
+
outputParameters: parametersToPass,
|
|
1518
|
+
}];
|
|
1589
1519
|
}
|
|
1590
1520
|
});
|
|
1591
|
-
});
|
|
1521
|
+
}); };
|
|
1522
|
+
return promptbookExecutor;
|
|
1592
1523
|
}
|
|
1593
1524
|
/**
|
|
1594
|
-
* TODO:
|
|
1595
|
-
* TODO:
|
|
1596
|
-
*
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
/**
|
|
1600
|
-
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
1525
|
+
* TODO: [🧠] When not meet expectations in PROMPT_DIALOG, make some way to tell the user
|
|
1526
|
+
* TODO: [👧] Strongly type the executors to avoid need of remove nullables whtn noUncheckedIndexedAccess in tsconfig.json
|
|
1527
|
+
* Note: CreatePromptbookExecutorOptions are just connected to PromptbookExecutor so do not extract to types folder
|
|
1528
|
+
* TODO: [🧠][3] transparent = (report intermediate parameters) / opaque execution = (report only output parameters) progress reporting mode
|
|
1601
1529
|
*/
|
|
1602
|
-
var PromptbookLogicError = /** @class */ (function (_super) {
|
|
1603
|
-
__extends(PromptbookLogicError, _super);
|
|
1604
|
-
function PromptbookLogicError(message) {
|
|
1605
|
-
var _this = _super.call(this, message) || this;
|
|
1606
|
-
_this.name = 'PromptbookLogicError';
|
|
1607
|
-
Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
|
|
1608
|
-
return _this;
|
|
1609
|
-
}
|
|
1610
|
-
return PromptbookLogicError;
|
|
1611
|
-
}(Error));
|
|
1612
1530
|
|
|
1613
1531
|
/**
|
|
1614
|
-
*
|
|
1532
|
+
* Prettify the html code
|
|
1615
1533
|
*
|
|
1616
|
-
*
|
|
1534
|
+
* @param content raw html code
|
|
1535
|
+
* @returns formatted html code
|
|
1617
1536
|
*/
|
|
1618
|
-
function
|
|
1619
|
-
if (typeof url !== 'string') {
|
|
1620
|
-
return false;
|
|
1621
|
-
}
|
|
1537
|
+
function prettifyMarkdown(content) {
|
|
1622
1538
|
try {
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1539
|
+
return format(content, {
|
|
1540
|
+
parser: 'markdown',
|
|
1541
|
+
plugins: [parserHtml],
|
|
1542
|
+
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
1543
|
+
endOfLine: 'lf',
|
|
1544
|
+
tabWidth: 4,
|
|
1545
|
+
singleQuote: true,
|
|
1546
|
+
trailingComma: 'all',
|
|
1547
|
+
arrowParens: 'always',
|
|
1548
|
+
printWidth: 120,
|
|
1549
|
+
htmlWhitespaceSensitivity: 'ignore',
|
|
1550
|
+
jsxBracketSameLine: false,
|
|
1551
|
+
bracketSpacing: true,
|
|
1552
|
+
});
|
|
1632
1553
|
}
|
|
1633
1554
|
catch (error) {
|
|
1634
|
-
|
|
1555
|
+
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
1556
|
+
error: error,
|
|
1557
|
+
html: content,
|
|
1558
|
+
});
|
|
1559
|
+
return content;
|
|
1635
1560
|
}
|
|
1636
1561
|
}
|
|
1637
1562
|
|
|
1638
1563
|
/**
|
|
1639
|
-
*
|
|
1640
|
-
*
|
|
1641
|
-
* It checks:
|
|
1642
|
-
* - if it has correct parameters dependency
|
|
1564
|
+
* Makes first letter of a string uppercase
|
|
1643
1565
|
*
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1566
|
+
*/
|
|
1567
|
+
function capitalize(word) {
|
|
1568
|
+
return word.substring(0, 1).toUpperCase() + word.substring(1);
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
/**
|
|
1572
|
+
* Converts promptbook in JSON format to string format
|
|
1647
1573
|
*
|
|
1648
|
-
* @param
|
|
1649
|
-
* @returns
|
|
1650
|
-
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1574
|
+
* @param promptbookJson Promptbook in JSON format (.ptbk.json)
|
|
1575
|
+
* @returns Promptbook in string format (.ptbk.md)
|
|
1651
1576
|
*/
|
|
1652
|
-
function
|
|
1653
|
-
|
|
1654
|
-
var
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
// TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
|
|
1662
|
-
if (!Array.isArray(promptbook.parameters)) {
|
|
1663
|
-
// TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
|
|
1664
|
-
throw new PromptbookSyntaxError(spaceTrim("\n Promptbook is valid JSON but with wrong structure\n\n promptbook.parameters expected to be an array, but got ".concat(typeof promptbook.parameters, "\n ")));
|
|
1577
|
+
function promptbookJsonToString(promptbookJson) {
|
|
1578
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
|
|
1579
|
+
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1580
|
+
var promptbookString = "# ".concat(title);
|
|
1581
|
+
if (description) {
|
|
1582
|
+
promptbookString += '\n\n';
|
|
1583
|
+
promptbookString += description;
|
|
1665
1584
|
}
|
|
1666
|
-
// TODO
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1585
|
+
// TODO:> const commands: Array<Command>
|
|
1586
|
+
var commands = [];
|
|
1587
|
+
if (promptbookUrl) {
|
|
1588
|
+
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1670
1589
|
}
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
throw new PromptbookLogicError("Parameter {".concat(parameter.name, "} can not be both input and output"));
|
|
1674
|
-
}
|
|
1675
|
-
// Note: Testing that parameter is either intermediate or output BUT not created and unused
|
|
1676
|
-
if (!parameter.isInput &&
|
|
1677
|
-
!parameter.isOutput &&
|
|
1678
|
-
!promptbook.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
|
|
1679
|
-
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ")));
|
|
1680
|
-
}
|
|
1681
|
-
// Note: Testing that parameter is either input or result of some template
|
|
1682
|
-
if (!parameter.isInput &&
|
|
1683
|
-
!promptbook.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
|
|
1684
|
-
throw new PromptbookLogicError(spaceTrim("\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ")));
|
|
1685
|
-
}
|
|
1686
|
-
};
|
|
1590
|
+
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1591
|
+
promptbookString = prettifyMarkdown(promptbookString);
|
|
1687
1592
|
try {
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1593
|
+
for (var _g = __values(parameters.filter(function (_a) {
|
|
1594
|
+
var isInput = _a.isInput;
|
|
1595
|
+
return isInput;
|
|
1596
|
+
})), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1597
|
+
var parameter = _h.value;
|
|
1598
|
+
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1692
1599
|
}
|
|
1693
1600
|
}
|
|
1694
1601
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1695
1602
|
finally {
|
|
1696
1603
|
try {
|
|
1697
|
-
if (
|
|
1698
|
-
}
|
|
1699
|
-
finally { if (e_1) throw e_1.error; }
|
|
1700
|
-
}
|
|
1701
|
-
// Note: Check each template individually
|
|
1702
|
-
var definedParameters = new Set(promptbook.parameters.filter(function (_a) {
|
|
1703
|
-
var isInput = _a.isInput;
|
|
1704
|
-
return isInput;
|
|
1705
|
-
}).map(function (_a) {
|
|
1706
|
-
var name = _a.name;
|
|
1707
|
-
return name;
|
|
1708
|
-
}));
|
|
1709
|
-
try {
|
|
1710
|
-
for (var _g = __values(promptbook.promptTemplates), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1711
|
-
var template = _h.value;
|
|
1712
|
-
if (definedParameters.has(template.resultingParameterName)) {
|
|
1713
|
-
throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
|
|
1714
|
-
}
|
|
1715
|
-
definedParameters.add(template.resultingParameterName);
|
|
1716
|
-
if (template.executionType === 'PROMPT_TEMPLATE' &&
|
|
1717
|
-
(template.modelRequirements.modelVariant === undefined)) {
|
|
1718
|
-
throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
|
|
1719
|
-
}
|
|
1720
|
-
if (template.jokers && template.jokers.length > 0) {
|
|
1721
|
-
if (!template.expectFormat &&
|
|
1722
|
-
!template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
|
|
1723
|
-
throw new PromptbookLogicError("Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined"));
|
|
1724
|
-
}
|
|
1725
|
-
try {
|
|
1726
|
-
for (var _j = (e_3 = void 0, __values(template.jokers)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
1727
|
-
var joker = _k.value;
|
|
1728
|
-
if (!template.dependentParameterNames.includes(joker)) {
|
|
1729
|
-
throw new PromptbookLogicError("Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in dependentParameterNames"));
|
|
1730
|
-
}
|
|
1731
|
-
}
|
|
1732
|
-
}
|
|
1733
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1734
|
-
finally {
|
|
1735
|
-
try {
|
|
1736
|
-
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
1737
|
-
}
|
|
1738
|
-
finally { if (e_3) throw e_3.error; }
|
|
1739
|
-
}
|
|
1740
|
-
}
|
|
1741
|
-
if (template.expectations) {
|
|
1742
|
-
try {
|
|
1743
|
-
for (var _l = (e_4 = void 0, __values(Object.entries(template.expectations))), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
1744
|
-
var _o = __read(_m.value, 2), unit = _o[0], _p = _o[1], min = _p.min, max = _p.max;
|
|
1745
|
-
if (min !== undefined && max !== undefined && min > max) {
|
|
1746
|
-
throw new PromptbookLogicError("Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")"));
|
|
1747
|
-
}
|
|
1748
|
-
if (min !== undefined && min < 0) {
|
|
1749
|
-
throw new PromptbookLogicError("Min expectation of ".concat(unit, " must be zero or positive"));
|
|
1750
|
-
}
|
|
1751
|
-
if (max !== undefined && max <= 0) {
|
|
1752
|
-
throw new PromptbookLogicError("Max expectation of ".concat(unit, " must be positive"));
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1757
|
-
finally {
|
|
1758
|
-
try {
|
|
1759
|
-
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
1760
|
-
}
|
|
1761
|
-
finally { if (e_4) throw e_4.error; }
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
}
|
|
1765
|
-
}
|
|
1766
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1767
|
-
finally {
|
|
1768
|
-
try {
|
|
1769
|
-
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
1770
|
-
}
|
|
1771
|
-
finally { if (e_2) throw e_2.error; }
|
|
1772
|
-
}
|
|
1773
|
-
// Note: Detect circular dependencies
|
|
1774
|
-
var resovedParameters = promptbook.parameters
|
|
1775
|
-
.filter(function (_a) {
|
|
1776
|
-
var isInput = _a.isInput;
|
|
1777
|
-
return isInput;
|
|
1778
|
-
})
|
|
1779
|
-
.map(function (_a) {
|
|
1780
|
-
var name = _a.name;
|
|
1781
|
-
return name;
|
|
1782
|
-
});
|
|
1783
|
-
var unresovedTemplates = __spreadArray([], __read(promptbook.promptTemplates), false);
|
|
1784
|
-
var loopLimit = LOOP_LIMIT;
|
|
1785
|
-
var _loop_2 = function () {
|
|
1786
|
-
if (loopLimit-- < 0) {
|
|
1787
|
-
throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
|
|
1788
|
-
}
|
|
1789
|
-
var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
|
|
1790
|
-
return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
|
|
1791
|
-
});
|
|
1792
|
-
if (currentlyResovedTemplates.length === 0) {
|
|
1793
|
-
throw new PromptbookLogicError(spaceTrim(function (block) { return "\n\n Can not resolve some parameters\n It may be circular dependencies\n\n Can not resolve:\n ".concat(block(unresovedTemplates
|
|
1794
|
-
.map(function (_a) {
|
|
1795
|
-
var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
|
|
1796
|
-
return "- {".concat(resultingParameterName, "} depends on ").concat(dependentParameterNames
|
|
1797
|
-
.map(function (dependentParameterName) { return "{".concat(dependentParameterName, "}"); })
|
|
1798
|
-
.join(', '));
|
|
1799
|
-
})
|
|
1800
|
-
.join('\n')), "\n\n Resolved:\n ").concat(block(resovedParameters.map(function (name) { return "- {".concat(name, "}"); }).join('\n')), "\n "); }));
|
|
1801
|
-
}
|
|
1802
|
-
resovedParameters = __spreadArray(__spreadArray([], __read(resovedParameters), false), __read(currentlyResovedTemplates.map(function (_a) {
|
|
1803
|
-
var resultingParameterName = _a.resultingParameterName;
|
|
1804
|
-
return resultingParameterName;
|
|
1805
|
-
})), false);
|
|
1806
|
-
unresovedTemplates = unresovedTemplates.filter(function (template) { return !currentlyResovedTemplates.includes(template); });
|
|
1807
|
-
};
|
|
1808
|
-
while (unresovedTemplates.length > 0) {
|
|
1809
|
-
_loop_2();
|
|
1810
|
-
}
|
|
1811
|
-
return promptbook;
|
|
1812
|
-
}
|
|
1813
|
-
/**
|
|
1814
|
-
* TODO: [🧠] Work with promptbookVersion
|
|
1815
|
-
* TODO: Use here some json-schema, Zod or something similar and change it to:
|
|
1816
|
-
* > /**
|
|
1817
|
-
* > * Validates PromptbookJson if it is logically valid.
|
|
1818
|
-
* > *
|
|
1819
|
-
* > * It checks:
|
|
1820
|
-
* > * - it has a valid structure
|
|
1821
|
-
* > * - ...
|
|
1822
|
-
* > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
|
|
1823
|
-
*/
|
|
1824
|
-
|
|
1825
|
-
/**
|
|
1826
|
-
* This error indicates that the promptbook library cannot be propperly loaded
|
|
1827
|
-
*/
|
|
1828
|
-
var PromptbookLibraryError = /** @class */ (function (_super) {
|
|
1829
|
-
__extends(PromptbookLibraryError, _super);
|
|
1830
|
-
function PromptbookLibraryError(message) {
|
|
1831
|
-
var _this = _super.call(this, message) || this;
|
|
1832
|
-
_this.name = 'PromptbookLibraryError';
|
|
1833
|
-
Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
|
|
1834
|
-
return _this;
|
|
1835
|
-
}
|
|
1836
|
-
return PromptbookLibraryError;
|
|
1837
|
-
}(Error));
|
|
1838
|
-
|
|
1839
|
-
/**
|
|
1840
|
-
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
1841
|
-
*/
|
|
1842
|
-
new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
|
|
1843
|
-
/**
|
|
1844
|
-
* Detects if the code is running in a Node.js environment
|
|
1845
|
-
*/
|
|
1846
|
-
var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
1847
|
-
/**
|
|
1848
|
-
* Detects if the code is running in a web worker
|
|
1849
|
-
*/
|
|
1850
|
-
new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
|
|
1851
|
-
|
|
1852
|
-
/**
|
|
1853
|
-
* Prettify the html code
|
|
1854
|
-
*
|
|
1855
|
-
* @param content raw html code
|
|
1856
|
-
* @returns formatted html code
|
|
1857
|
-
*/
|
|
1858
|
-
function prettifyMarkdown(content) {
|
|
1859
|
-
try {
|
|
1860
|
-
return format(content, {
|
|
1861
|
-
parser: 'markdown',
|
|
1862
|
-
plugins: [parserHtml],
|
|
1863
|
-
// TODO: DRY - make some import or auto-copy of .prettierrc
|
|
1864
|
-
endOfLine: 'lf',
|
|
1865
|
-
tabWidth: 4,
|
|
1866
|
-
singleQuote: true,
|
|
1867
|
-
trailingComma: 'all',
|
|
1868
|
-
arrowParens: 'always',
|
|
1869
|
-
printWidth: 120,
|
|
1870
|
-
htmlWhitespaceSensitivity: 'ignore',
|
|
1871
|
-
jsxBracketSameLine: false,
|
|
1872
|
-
bracketSpacing: true,
|
|
1873
|
-
});
|
|
1874
|
-
}
|
|
1875
|
-
catch (error) {
|
|
1876
|
-
console.error('There was an error with prettifying the markdown, using the original as the fallback', {
|
|
1877
|
-
error: error,
|
|
1878
|
-
html: content,
|
|
1879
|
-
});
|
|
1880
|
-
return content;
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
/**
|
|
1885
|
-
* Converts promptbook in JSON format to string format
|
|
1886
|
-
*
|
|
1887
|
-
* @param promptbookJson Promptbook in JSON format (.ptbk.json)
|
|
1888
|
-
* @returns Promptbook in string format (.ptbk.md)
|
|
1889
|
-
*/
|
|
1890
|
-
function promptbookJsonToString(promptbookJson) {
|
|
1891
|
-
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e, e_6, _f;
|
|
1892
|
-
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1893
|
-
var promptbookString = "# ".concat(title);
|
|
1894
|
-
if (description) {
|
|
1895
|
-
promptbookString += '\n\n';
|
|
1896
|
-
promptbookString += description;
|
|
1897
|
-
}
|
|
1898
|
-
// TODO:> const commands: Array<Command>
|
|
1899
|
-
var commands = [];
|
|
1900
|
-
if (promptbookUrl) {
|
|
1901
|
-
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1902
|
-
}
|
|
1903
|
-
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1904
|
-
promptbookString = prettifyMarkdown(promptbookString);
|
|
1905
|
-
try {
|
|
1906
|
-
for (var _g = __values(parameters.filter(function (_a) {
|
|
1907
|
-
var isInput = _a.isInput;
|
|
1908
|
-
return isInput;
|
|
1909
|
-
})), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
1910
|
-
var parameter = _h.value;
|
|
1911
|
-
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1912
|
-
}
|
|
1913
|
-
}
|
|
1914
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1915
|
-
finally {
|
|
1916
|
-
try {
|
|
1917
|
-
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
1604
|
+
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
1918
1605
|
}
|
|
1919
1606
|
finally { if (e_1) throw e_1.error; }
|
|
1920
1607
|
}
|
|
@@ -2018,240 +1705,1496 @@ function promptbookJsonToString(promptbookJson) {
|
|
|
2018
1705
|
if (min === max) {
|
|
2019
1706
|
commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
2020
1707
|
}
|
|
2021
|
-
else {
|
|
2022
|
-
if (min !== undefined) {
|
|
2023
|
-
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
2024
|
-
} /* not else */
|
|
2025
|
-
if (max !== undefined) {
|
|
2026
|
-
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
2027
|
-
}
|
|
1708
|
+
else {
|
|
1709
|
+
if (min !== undefined) {
|
|
1710
|
+
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1711
|
+
} /* not else */
|
|
1712
|
+
if (max !== undefined) {
|
|
1713
|
+
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1719
|
+
finally {
|
|
1720
|
+
try {
|
|
1721
|
+
if (_m && !_m.done && (_f = _l.return)) _f.call(_l);
|
|
1722
|
+
}
|
|
1723
|
+
finally { if (e_6) throw e_6.error; }
|
|
1724
|
+
}
|
|
1725
|
+
} /* not else */
|
|
1726
|
+
if (expectFormat) {
|
|
1727
|
+
if (expectFormat === 'JSON') {
|
|
1728
|
+
// TODO: @deprecated remove
|
|
1729
|
+
commands_1.push("EXPECT JSON");
|
|
1730
|
+
}
|
|
1731
|
+
} /* not else */
|
|
1732
|
+
promptbookString += '\n\n';
|
|
1733
|
+
promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1734
|
+
promptbookString += '\n\n';
|
|
1735
|
+
promptbookString += '```' + contentLanguage;
|
|
1736
|
+
promptbookString += '\n';
|
|
1737
|
+
promptbookString += spaceTrim$1(content);
|
|
1738
|
+
// <- TODO: !!! Escape
|
|
1739
|
+
// <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
|
|
1740
|
+
promptbookString += '\n';
|
|
1741
|
+
promptbookString += '```';
|
|
1742
|
+
promptbookString += '\n\n';
|
|
1743
|
+
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1747
|
+
finally {
|
|
1748
|
+
try {
|
|
1749
|
+
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
|
|
1750
|
+
}
|
|
1751
|
+
finally { if (e_3) throw e_3.error; }
|
|
1752
|
+
}
|
|
1753
|
+
return promptbookString;
|
|
1754
|
+
}
|
|
1755
|
+
/**
|
|
1756
|
+
* @private internal util of promptbookJsonToString
|
|
1757
|
+
*/
|
|
1758
|
+
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
1759
|
+
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
1760
|
+
var parameterString = "{".concat(name, "}");
|
|
1761
|
+
if (description) {
|
|
1762
|
+
parameterString = "".concat(parameterString, " ").concat(description);
|
|
1763
|
+
}
|
|
1764
|
+
return parameterString;
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* TODO: Escape all
|
|
1768
|
+
*/
|
|
1769
|
+
|
|
1770
|
+
/**
|
|
1771
|
+
* This error indicates that promptbook not found in the library
|
|
1772
|
+
*/
|
|
1773
|
+
var PromptbookNotFoundError = /** @class */ (function (_super) {
|
|
1774
|
+
__extends(PromptbookNotFoundError, _super);
|
|
1775
|
+
function PromptbookNotFoundError(message) {
|
|
1776
|
+
var _this = _super.call(this, message) || this;
|
|
1777
|
+
_this.name = 'PromptbookNotFoundError';
|
|
1778
|
+
Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
|
|
1779
|
+
return _this;
|
|
1780
|
+
}
|
|
1781
|
+
return PromptbookNotFoundError;
|
|
1782
|
+
}(Error));
|
|
1783
|
+
|
|
1784
|
+
/**
|
|
1785
|
+
* This error indicates errors in referencing promptbooks between each other
|
|
1786
|
+
*/
|
|
1787
|
+
var PromptbookReferenceError = /** @class */ (function (_super) {
|
|
1788
|
+
__extends(PromptbookReferenceError, _super);
|
|
1789
|
+
function PromptbookReferenceError(message) {
|
|
1790
|
+
var _this = _super.call(this, message) || this;
|
|
1791
|
+
_this.name = 'PromptbookReferenceError';
|
|
1792
|
+
Object.setPrototypeOf(_this, PromptbookReferenceError.prototype);
|
|
1793
|
+
return _this;
|
|
1794
|
+
}
|
|
1795
|
+
return PromptbookReferenceError;
|
|
1796
|
+
}(Error));
|
|
1797
|
+
|
|
1798
|
+
/**
|
|
1799
|
+
* Library of promptbooks that groups together promptbooks for an application.
|
|
1800
|
+
* This implementation is a very thin wrapper around the Array / Map of promptbooks.
|
|
1801
|
+
*
|
|
1802
|
+
* @private use `createLibraryFromJson` instead
|
|
1803
|
+
* @see https://github.com/webgptorg/promptbook#promptbook-library
|
|
1804
|
+
*/
|
|
1805
|
+
var SimplePromptbookLibrary = /** @class */ (function () {
|
|
1806
|
+
/**
|
|
1807
|
+
* Constructs a promptbook library from promptbooks
|
|
1808
|
+
*
|
|
1809
|
+
* @param promptbooks !!!
|
|
1810
|
+
*
|
|
1811
|
+
* @private Use instead `createLibraryFromJson`
|
|
1812
|
+
* Note: During the construction logic of all promptbooks are validated
|
|
1813
|
+
* Note: It is not recommended to use this constructor directly, use `createLibraryFromJson` *(or other variant)* instead
|
|
1814
|
+
*/
|
|
1815
|
+
function SimplePromptbookLibrary() {
|
|
1816
|
+
var e_1, _a;
|
|
1817
|
+
var promptbooks = [];
|
|
1818
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1819
|
+
promptbooks[_i] = arguments[_i];
|
|
1820
|
+
}
|
|
1821
|
+
this.library = new Map();
|
|
1822
|
+
try {
|
|
1823
|
+
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
1824
|
+
var promptbook = promptbooks_1_1.value;
|
|
1825
|
+
if (promptbook.promptbookUrl === undefined) {
|
|
1826
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
1827
|
+
}
|
|
1828
|
+
validatePromptbook(promptbook);
|
|
1829
|
+
// Note: [🦄]
|
|
1830
|
+
if (this.library.has(promptbook.promptbookUrl) &&
|
|
1831
|
+
promptbookJsonToString(promptbook) !==
|
|
1832
|
+
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
1833
|
+
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
|
|
1834
|
+
}
|
|
1835
|
+
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1839
|
+
finally {
|
|
1840
|
+
try {
|
|
1841
|
+
if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
|
|
1842
|
+
}
|
|
1843
|
+
finally { if (e_1) throw e_1.error; }
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Gets all promptbooks in the library
|
|
1848
|
+
*/
|
|
1849
|
+
SimplePromptbookLibrary.prototype.listPromptbooks = function () {
|
|
1850
|
+
return Array.from(this.library.keys());
|
|
1851
|
+
};
|
|
1852
|
+
/**
|
|
1853
|
+
* Gets promptbook by its URL
|
|
1854
|
+
*
|
|
1855
|
+
* Note: This is not a direct fetching from the URL, but a lookup in the library
|
|
1856
|
+
*/
|
|
1857
|
+
SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
|
|
1858
|
+
var _this = this;
|
|
1859
|
+
var promptbook = this.library.get(url);
|
|
1860
|
+
if (!promptbook) {
|
|
1861
|
+
if (this.listPromptbooks().length === 0) {
|
|
1862
|
+
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
1863
|
+
}
|
|
1864
|
+
throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
1865
|
+
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
1866
|
+
.join('\n')), "\n\n "); }));
|
|
1867
|
+
}
|
|
1868
|
+
return promptbook;
|
|
1869
|
+
};
|
|
1870
|
+
/**
|
|
1871
|
+
* Checks whether given prompt was defined in any promptbook in the library
|
|
1872
|
+
*/
|
|
1873
|
+
SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
|
|
1874
|
+
return true;
|
|
1875
|
+
};
|
|
1876
|
+
return SimplePromptbookLibrary;
|
|
1877
|
+
}());
|
|
1878
|
+
|
|
1879
|
+
/**
|
|
1880
|
+
* Creates PromptbookLibrary from array of PromptbookJson or PromptbookString
|
|
1881
|
+
*
|
|
1882
|
+
* Note: Functions `libraryToJson` and `createLibraryFromJson` are complementary
|
|
1883
|
+
* Note: During the construction syntax and logic of all sources are validated
|
|
1884
|
+
*
|
|
1885
|
+
* @param promptbookSources
|
|
1886
|
+
* @returns PromptbookLibrary
|
|
1887
|
+
*/
|
|
1888
|
+
function createLibraryFromJson() {
|
|
1889
|
+
var promptbooks = [];
|
|
1890
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1891
|
+
promptbooks[_i] = arguments[_i];
|
|
1892
|
+
}
|
|
1893
|
+
return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
/* tslint:disable */
|
|
1897
|
+
function normalizeToKebabCase(sentence) {
|
|
1898
|
+
var e_1, _a;
|
|
1899
|
+
sentence = removeDiacritics(sentence);
|
|
1900
|
+
var charType;
|
|
1901
|
+
var lastCharType = 'OTHER';
|
|
1902
|
+
var normalizedName = '';
|
|
1903
|
+
try {
|
|
1904
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
1905
|
+
var char = sentence_1_1.value;
|
|
1906
|
+
var normalizedChar = void 0;
|
|
1907
|
+
if (/^[a-z]$/.test(char)) {
|
|
1908
|
+
charType = 'LOWERCASE';
|
|
1909
|
+
normalizedChar = char;
|
|
1910
|
+
}
|
|
1911
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
1912
|
+
charType = 'UPPERCASE';
|
|
1913
|
+
normalizedChar = char.toLowerCase();
|
|
1914
|
+
}
|
|
1915
|
+
else if (/^[0-9]$/.test(char)) {
|
|
1916
|
+
charType = 'NUMBER';
|
|
1917
|
+
normalizedChar = char;
|
|
1918
|
+
}
|
|
1919
|
+
else if (/^\/$/.test(char)) {
|
|
1920
|
+
charType = 'SLASH';
|
|
1921
|
+
normalizedChar = char;
|
|
1922
|
+
}
|
|
1923
|
+
else {
|
|
1924
|
+
charType = 'OTHER';
|
|
1925
|
+
normalizedChar = '-';
|
|
1926
|
+
}
|
|
1927
|
+
if (charType !== lastCharType &&
|
|
1928
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
1929
|
+
!(lastCharType === 'NUMBER') &&
|
|
1930
|
+
!(charType === 'NUMBER')) {
|
|
1931
|
+
normalizedName += '-';
|
|
1932
|
+
}
|
|
1933
|
+
normalizedName += normalizedChar;
|
|
1934
|
+
lastCharType = charType;
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1938
|
+
finally {
|
|
1939
|
+
try {
|
|
1940
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
1941
|
+
}
|
|
1942
|
+
finally { if (e_1) throw e_1.error; }
|
|
1943
|
+
}
|
|
1944
|
+
normalizedName = normalizedName.split(/-+/g).join('-');
|
|
1945
|
+
normalizedName = normalizedName.split(/-?\/-?/g).join('/');
|
|
1946
|
+
normalizedName = normalizedName.replace(/^-/, '');
|
|
1947
|
+
normalizedName = normalizedName.replace(/-$/, '');
|
|
1948
|
+
return normalizedName;
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
function prepareKnowledgeFromMarkdown(options) {
|
|
1952
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1953
|
+
var content, llmTools, _a, isVerbose, library, prepareKnowledgeFromMarkdownPromptbook, prepareKnowledgeFromMarkdownExecutor, prepareKeywordsPromptbook, prepareKeywordsExecutor, result, outputParameters, knowledgeRaw, knowledgeTextPieces, knowledge;
|
|
1954
|
+
var _this = this;
|
|
1955
|
+
return __generator(this, function (_b) {
|
|
1956
|
+
switch (_b.label) {
|
|
1957
|
+
case 0:
|
|
1958
|
+
content = options.content, llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
|
|
1959
|
+
library = createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookLibrary), false));
|
|
1960
|
+
return [4 /*yield*/, library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md')];
|
|
1961
|
+
case 1:
|
|
1962
|
+
prepareKnowledgeFromMarkdownPromptbook = _b.sent();
|
|
1963
|
+
prepareKnowledgeFromMarkdownExecutor = createPromptbookExecutor({
|
|
1964
|
+
promptbook: prepareKnowledgeFromMarkdownPromptbook,
|
|
1965
|
+
tools: {
|
|
1966
|
+
llm: llmTools,
|
|
1967
|
+
script: [
|
|
1968
|
+
/* <- TODO: Allow to just keep script undefined */
|
|
1969
|
+
],
|
|
1970
|
+
},
|
|
1971
|
+
});
|
|
1972
|
+
return [4 /*yield*/, library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-keywords.ptbk.md')];
|
|
1973
|
+
case 2:
|
|
1974
|
+
prepareKeywordsPromptbook = _b.sent();
|
|
1975
|
+
prepareKeywordsExecutor = createPromptbookExecutor({
|
|
1976
|
+
promptbook: prepareKeywordsPromptbook,
|
|
1977
|
+
tools: {
|
|
1978
|
+
llm: llmTools,
|
|
1979
|
+
script: [
|
|
1980
|
+
/* <- TODO: Allow to just keep script undefined */
|
|
1981
|
+
],
|
|
1982
|
+
},
|
|
1983
|
+
});
|
|
1984
|
+
return [4 /*yield*/, prepareKnowledgeFromMarkdownExecutor({ content: content })];
|
|
1985
|
+
case 3:
|
|
1986
|
+
result = _b.sent();
|
|
1987
|
+
assertsExecutionSuccessful(result);
|
|
1988
|
+
outputParameters = result.outputParameters;
|
|
1989
|
+
knowledgeRaw = outputParameters.knowledge;
|
|
1990
|
+
knowledgeTextPieces = (knowledgeRaw || '').split('\n---\n');
|
|
1991
|
+
if (isVerbose) {
|
|
1992
|
+
console.info('knowledgeTextPieces:', knowledgeTextPieces);
|
|
1993
|
+
}
|
|
1994
|
+
return [4 /*yield*/, Promise.all(knowledgeTextPieces.map(function (knowledgeTextPiece, i) { return __awaiter(_this, void 0, void 0, function () {
|
|
1995
|
+
var name, title, content, keywords, index, sources, result_1, _a, outputParameters_1, keywordsRaw, error_1;
|
|
1996
|
+
return __generator(this, function (_b) {
|
|
1997
|
+
switch (_b.label) {
|
|
1998
|
+
case 0:
|
|
1999
|
+
name = "piece-".concat(i);
|
|
2000
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 100));
|
|
2001
|
+
content = spaceTrim$1(knowledgeTextPiece);
|
|
2002
|
+
keywords = [];
|
|
2003
|
+
index = [];
|
|
2004
|
+
sources = [];
|
|
2005
|
+
_b.label = 1;
|
|
2006
|
+
case 1:
|
|
2007
|
+
_b.trys.push([1, 3, , 4]);
|
|
2008
|
+
// TODO: !!!! Summarize name and title from the content
|
|
2009
|
+
title = spaceTrim$1(knowledgeTextPiece.substring(0, 30));
|
|
2010
|
+
name = normalizeToKebabCase(title);
|
|
2011
|
+
return [4 /*yield*/, prepareKeywordsExecutor({ content: content })];
|
|
2012
|
+
case 2:
|
|
2013
|
+
result_1 = _b.sent();
|
|
2014
|
+
_a = result_1.outputParameters, outputParameters_1 = _a === void 0 ? {} : _a;
|
|
2015
|
+
keywordsRaw = outputParameters_1.keywords;
|
|
2016
|
+
keywords = (keywordsRaw || '')
|
|
2017
|
+
.split(',')
|
|
2018
|
+
.map(function (keyword) { return keyword.trim(); })
|
|
2019
|
+
.filter(function (keyword) { return keyword !== ''; });
|
|
2020
|
+
if (isVerbose) {
|
|
2021
|
+
console.info("Keywords for \"".concat(title, "\":"), keywords);
|
|
2022
|
+
}
|
|
2023
|
+
// ---
|
|
2024
|
+
// TODO: !!!! Index through LLM model
|
|
2025
|
+
index.push({
|
|
2026
|
+
modelName: 'fake-model',
|
|
2027
|
+
position: new Array(25).fill(0).map(function () { return Math.random() * 2 - 1; }),
|
|
2028
|
+
});
|
|
2029
|
+
return [3 /*break*/, 4];
|
|
2030
|
+
case 3:
|
|
2031
|
+
error_1 = _b.sent();
|
|
2032
|
+
console.error(error_1);
|
|
2033
|
+
return [3 /*break*/, 4];
|
|
2034
|
+
case 4: return [2 /*return*/, {
|
|
2035
|
+
name: name,
|
|
2036
|
+
title: title,
|
|
2037
|
+
content: content,
|
|
2038
|
+
keywords: keywords,
|
|
2039
|
+
index: index,
|
|
2040
|
+
sources: sources,
|
|
2041
|
+
}];
|
|
2042
|
+
}
|
|
2043
|
+
});
|
|
2044
|
+
}); }))];
|
|
2045
|
+
case 4:
|
|
2046
|
+
knowledge = _b.sent();
|
|
2047
|
+
return [2 /*return*/, knowledge];
|
|
2048
|
+
}
|
|
2049
|
+
});
|
|
2050
|
+
});
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
/**
|
|
2054
|
+
* Supported script languages
|
|
2055
|
+
*/
|
|
2056
|
+
var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
|
|
2057
|
+
|
|
2058
|
+
/**
|
|
2059
|
+
* Computes the deepness of the markdown structure.
|
|
2060
|
+
*
|
|
2061
|
+
* @private within the library
|
|
2062
|
+
*/
|
|
2063
|
+
function countMarkdownStructureDeepness(markdownStructure) {
|
|
2064
|
+
var e_1, _a;
|
|
2065
|
+
var maxDeepness = 0;
|
|
2066
|
+
try {
|
|
2067
|
+
for (var _b = __values(markdownStructure.sections), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2068
|
+
var section = _c.value;
|
|
2069
|
+
maxDeepness = Math.max(maxDeepness, countMarkdownStructureDeepness(section));
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2073
|
+
finally {
|
|
2074
|
+
try {
|
|
2075
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2076
|
+
}
|
|
2077
|
+
finally { if (e_1) throw e_1.error; }
|
|
2078
|
+
}
|
|
2079
|
+
return maxDeepness + 1;
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
/**
|
|
2083
|
+
* Parse a markdown string into a MarkdownStructure object.
|
|
2084
|
+
*
|
|
2085
|
+
* Note: This function does work with code blocks
|
|
2086
|
+
* Note: This function does not work with markdown comments
|
|
2087
|
+
*
|
|
2088
|
+
* @param markdown The markdown string to parse.
|
|
2089
|
+
* @returns The MarkdownStructure object.
|
|
2090
|
+
*
|
|
2091
|
+
* @private within the library
|
|
2092
|
+
*/
|
|
2093
|
+
function markdownToMarkdownStructure(markdown) {
|
|
2094
|
+
var e_1, _a;
|
|
2095
|
+
var lines = markdown.split('\n');
|
|
2096
|
+
var root = { level: 0, title: '', contentLines: [], sections: [], parent: null };
|
|
2097
|
+
var current = root;
|
|
2098
|
+
var isInsideCodeBlock = false;
|
|
2099
|
+
try {
|
|
2100
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2101
|
+
var line = lines_1_1.value;
|
|
2102
|
+
var headingMatch = line.match(/^(?<mark>#{1,6})\s(?<title>.*)/);
|
|
2103
|
+
if (isInsideCodeBlock || !headingMatch) {
|
|
2104
|
+
if (line.startsWith('```')) {
|
|
2105
|
+
isInsideCodeBlock = !isInsideCodeBlock;
|
|
2106
|
+
}
|
|
2107
|
+
current.contentLines.push(line);
|
|
2108
|
+
}
|
|
2109
|
+
else {
|
|
2110
|
+
var level = headingMatch.groups.mark.length;
|
|
2111
|
+
var title = headingMatch.groups.title.trim();
|
|
2112
|
+
var parent_1 = void 0;
|
|
2113
|
+
if (level > current.level) {
|
|
2114
|
+
// Note: Going deeper (next section is child of current)
|
|
2115
|
+
parent_1 = current;
|
|
2116
|
+
}
|
|
2117
|
+
else {
|
|
2118
|
+
// Note: Going up or staying at the same level (next section is sibling or parent or grandparent,... of current)
|
|
2119
|
+
parent_1 = current;
|
|
2120
|
+
var loopLimit = LOOP_LIMIT;
|
|
2121
|
+
while (parent_1.level !== level - 1) {
|
|
2122
|
+
if (loopLimit-- < 0) {
|
|
2123
|
+
throw new UnexpectedError('Loop limit reached during parsing of markdown structure in `markdownToMarkdownStructure`');
|
|
2124
|
+
}
|
|
2125
|
+
if (parent_1.parent === null /* <- Note: We are in root */) {
|
|
2126
|
+
// [🌻]
|
|
2127
|
+
throw new Error(spaceTrim("\n The file has an invalid structure.\n The markdown file must have exactly one top-level section.\n "));
|
|
2128
|
+
}
|
|
2129
|
+
parent_1 = parent_1.parent;
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
var section = { level: level, title: title, contentLines: [], sections: [], parent: parent_1 };
|
|
2133
|
+
parent_1.sections.push(section);
|
|
2134
|
+
current = section;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2139
|
+
finally {
|
|
2140
|
+
try {
|
|
2141
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2142
|
+
}
|
|
2143
|
+
finally { if (e_1) throw e_1.error; }
|
|
2144
|
+
}
|
|
2145
|
+
if (root.sections.length === 1) {
|
|
2146
|
+
var markdownStructure = parsingMarkdownStructureToMarkdownStructure(root.sections[0]);
|
|
2147
|
+
return markdownStructure;
|
|
2148
|
+
}
|
|
2149
|
+
// [🌻]
|
|
2150
|
+
throw new Error('The markdown file must have exactly one top-level section.');
|
|
2151
|
+
// return root;
|
|
2152
|
+
}
|
|
2153
|
+
/**
|
|
2154
|
+
* @private
|
|
2155
|
+
*/
|
|
2156
|
+
function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
|
|
2157
|
+
var level = parsingMarkdownStructure.level, title = parsingMarkdownStructure.title, contentLines = parsingMarkdownStructure.contentLines, sections = parsingMarkdownStructure.sections;
|
|
2158
|
+
return {
|
|
2159
|
+
level: level,
|
|
2160
|
+
title: title,
|
|
2161
|
+
content: spaceTrim(contentLines.join('\n')),
|
|
2162
|
+
sections: sections.map(parsingMarkdownStructureToMarkdownStructure),
|
|
2163
|
+
};
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
/**
|
|
2167
|
+
* Utility function to extract all list items from markdown
|
|
2168
|
+
*
|
|
2169
|
+
* Note: It works with both ul and ol
|
|
2170
|
+
* Note: It omits list items in code blocks
|
|
2171
|
+
* Note: It flattens nested lists
|
|
2172
|
+
* Note: It can not work with html syntax and comments
|
|
2173
|
+
*
|
|
2174
|
+
* @param markdown any valid markdown
|
|
2175
|
+
* @returns
|
|
2176
|
+
*/
|
|
2177
|
+
function extractAllListItemsFromMarkdown(markdown) {
|
|
2178
|
+
var e_1, _a;
|
|
2179
|
+
var lines = markdown.split('\n');
|
|
2180
|
+
var listItems = [];
|
|
2181
|
+
var isInCodeBlock = false;
|
|
2182
|
+
try {
|
|
2183
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2184
|
+
var line = lines_1_1.value;
|
|
2185
|
+
var trimmedLine = line.trim();
|
|
2186
|
+
if (trimmedLine.startsWith('```')) {
|
|
2187
|
+
isInCodeBlock = !isInCodeBlock;
|
|
2188
|
+
}
|
|
2189
|
+
if (!isInCodeBlock && (trimmedLine.startsWith('-') || trimmedLine.match(/^\d+\./))) {
|
|
2190
|
+
var listItem = trimmedLine.replace(/^-|\d+\./, '').trim();
|
|
2191
|
+
listItems.push(listItem);
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2196
|
+
finally {
|
|
2197
|
+
try {
|
|
2198
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2199
|
+
}
|
|
2200
|
+
finally { if (e_1) throw e_1.error; }
|
|
2201
|
+
}
|
|
2202
|
+
return listItems;
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
/**
|
|
2206
|
+
* Extracts all code blocks from markdown.
|
|
2207
|
+
*
|
|
2208
|
+
* Note: There are 3 simmilar function:
|
|
2209
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2210
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2211
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2212
|
+
*
|
|
2213
|
+
* @param markdown any valid markdown
|
|
2214
|
+
* @returns code blocks with language and content
|
|
2215
|
+
*
|
|
2216
|
+
*/
|
|
2217
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
2218
|
+
var e_1, _a;
|
|
2219
|
+
var codeBlocks = [];
|
|
2220
|
+
var lines = markdown.split('\n');
|
|
2221
|
+
var currentCodeBlock = null;
|
|
2222
|
+
try {
|
|
2223
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2224
|
+
var line = lines_1_1.value;
|
|
2225
|
+
if (line.startsWith('```')) {
|
|
2226
|
+
var language = line.slice(3).trim() || null;
|
|
2227
|
+
if (currentCodeBlock === null) {
|
|
2228
|
+
currentCodeBlock = { language: language, content: '' };
|
|
2229
|
+
}
|
|
2230
|
+
else {
|
|
2231
|
+
if (language !== null) {
|
|
2232
|
+
// [🌻]
|
|
2233
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
2234
|
+
}
|
|
2235
|
+
codeBlocks.push(currentCodeBlock);
|
|
2236
|
+
currentCodeBlock = null;
|
|
2237
|
+
}
|
|
2238
|
+
}
|
|
2239
|
+
else if (currentCodeBlock !== null) {
|
|
2240
|
+
if (currentCodeBlock.content !== '') {
|
|
2241
|
+
currentCodeBlock.content += '\n';
|
|
2242
|
+
}
|
|
2243
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2248
|
+
finally {
|
|
2249
|
+
try {
|
|
2250
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2251
|
+
}
|
|
2252
|
+
finally { if (e_1) throw e_1.error; }
|
|
2253
|
+
}
|
|
2254
|
+
if (currentCodeBlock !== null) {
|
|
2255
|
+
// [🌻]
|
|
2256
|
+
throw new Error("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
2257
|
+
}
|
|
2258
|
+
return codeBlocks;
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
/**
|
|
2262
|
+
* Extracts exactly ONE code block from markdown.
|
|
2263
|
+
*
|
|
2264
|
+
* Note: If there are multiple or no code blocks the function throws an error
|
|
2265
|
+
*
|
|
2266
|
+
* Note: There are 3 simmilar function:
|
|
2267
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2268
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2269
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2270
|
+
*
|
|
2271
|
+
* @param markdown any valid markdown
|
|
2272
|
+
* @returns code block with language and content
|
|
2273
|
+
*/
|
|
2274
|
+
function extractOneBlockFromMarkdown(markdown) {
|
|
2275
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
2276
|
+
if (codeBlocks.length !== 1) {
|
|
2277
|
+
// TODO: Report more specific place where the error happened
|
|
2278
|
+
throw new Error(/* <- [🌻] */ 'There should be exactly one code block in the markdown');
|
|
2279
|
+
}
|
|
2280
|
+
return codeBlocks[0];
|
|
2281
|
+
}
|
|
2282
|
+
/***
|
|
2283
|
+
* TODO: [🍓][🌻] !!! Decide of this is internal util, external util OR validator/postprocessor
|
|
2284
|
+
*/
|
|
2285
|
+
|
|
2286
|
+
/**
|
|
2287
|
+
* Removes HTML or Markdown comments from a string.
|
|
2288
|
+
*
|
|
2289
|
+
* @param {string} content - The string to remove comments from.
|
|
2290
|
+
* @returns {string} The input string with all comments removed.
|
|
2291
|
+
*/
|
|
2292
|
+
function removeContentComments(content) {
|
|
2293
|
+
return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
/**
|
|
2297
|
+
* Creates a new set with all elements that are present in either set
|
|
2298
|
+
*/
|
|
2299
|
+
function union() {
|
|
2300
|
+
var e_1, _a, e_2, _b;
|
|
2301
|
+
var sets = [];
|
|
2302
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2303
|
+
sets[_i] = arguments[_i];
|
|
2304
|
+
}
|
|
2305
|
+
var union = new Set();
|
|
2306
|
+
try {
|
|
2307
|
+
for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
|
|
2308
|
+
var set = sets_1_1.value;
|
|
2309
|
+
try {
|
|
2310
|
+
for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
2311
|
+
var item = _d.value;
|
|
2312
|
+
union.add(item);
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2316
|
+
finally {
|
|
2317
|
+
try {
|
|
2318
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
2319
|
+
}
|
|
2320
|
+
finally { if (e_2) throw e_2.error; }
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2325
|
+
finally {
|
|
2326
|
+
try {
|
|
2327
|
+
if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
|
|
2328
|
+
}
|
|
2329
|
+
finally { if (e_1) throw e_1.error; }
|
|
2330
|
+
}
|
|
2331
|
+
return union;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
/**
|
|
2335
|
+
* Parses the template and returns the list of all parameter names
|
|
2336
|
+
*
|
|
2337
|
+
* @param template the template with parameters in {curly} braces
|
|
2338
|
+
* @returns the list of parameter names
|
|
2339
|
+
*/
|
|
2340
|
+
function extractParameters(template) {
|
|
2341
|
+
var e_1, _a;
|
|
2342
|
+
var matches = template.matchAll(/{\w+}/g);
|
|
2343
|
+
var parameterNames = new Set();
|
|
2344
|
+
try {
|
|
2345
|
+
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
|
|
2346
|
+
var match = matches_1_1.value;
|
|
2347
|
+
var parameterName = match[0].slice(1, -1);
|
|
2348
|
+
parameterNames.add(parameterName);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2352
|
+
finally {
|
|
2353
|
+
try {
|
|
2354
|
+
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
|
|
2355
|
+
}
|
|
2356
|
+
finally { if (e_1) throw e_1.error; }
|
|
2357
|
+
}
|
|
2358
|
+
return parameterNames;
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
/**
|
|
2362
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
2363
|
+
*
|
|
2364
|
+
* @param script from which to extract the variables
|
|
2365
|
+
* @returns the list of variable names
|
|
2366
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2367
|
+
*/
|
|
2368
|
+
function extractVariables(script) {
|
|
2369
|
+
var variables = new Set();
|
|
2370
|
+
script = "(()=>{".concat(script, "})()");
|
|
2371
|
+
try {
|
|
2372
|
+
for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
|
|
2373
|
+
try {
|
|
2374
|
+
eval(script);
|
|
2375
|
+
}
|
|
2376
|
+
catch (error) {
|
|
2377
|
+
if (!(error instanceof ReferenceError)) {
|
|
2378
|
+
throw error;
|
|
2379
|
+
}
|
|
2380
|
+
var undefinedName = error.message.split(' ')[0];
|
|
2381
|
+
/*
|
|
2382
|
+
Note: Remapping error
|
|
2383
|
+
From: [ReferenceError: thing is not defined],
|
|
2384
|
+
To: [Error: Parameter {thing} is not defined],
|
|
2385
|
+
*/
|
|
2386
|
+
if (!undefinedName) {
|
|
2387
|
+
throw error;
|
|
2388
|
+
}
|
|
2389
|
+
if (script.includes(undefinedName + '(')) {
|
|
2390
|
+
script = "const ".concat(undefinedName, " = ()=>'';") + script;
|
|
2391
|
+
}
|
|
2392
|
+
else {
|
|
2393
|
+
variables.add(undefinedName);
|
|
2394
|
+
script = "const ".concat(undefinedName, " = '';") + script;
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
catch (error) {
|
|
2399
|
+
if (!(error instanceof Error)) {
|
|
2400
|
+
throw error;
|
|
2401
|
+
}
|
|
2402
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
|
|
2403
|
+
}
|
|
2404
|
+
return variables;
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2408
|
+
*/
|
|
2409
|
+
|
|
2410
|
+
/**
|
|
2411
|
+
* Parses the prompt template and returns the set of all used parameters
|
|
2412
|
+
*
|
|
2413
|
+
* @param promptTemplate the template with used parameters
|
|
2414
|
+
* @returns the set of parameter names
|
|
2415
|
+
* @throws {PromptbookSyntaxError} if the script is invalid
|
|
2416
|
+
*/
|
|
2417
|
+
function extractParametersFromPromptTemplate(promptTemplate) {
|
|
2418
|
+
var e_1, _a, e_2, _b;
|
|
2419
|
+
var parameterNames = new Set();
|
|
2420
|
+
try {
|
|
2421
|
+
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()) {
|
|
2422
|
+
var parameterName = _d.value;
|
|
2423
|
+
parameterNames.add(parameterName);
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2427
|
+
finally {
|
|
2428
|
+
try {
|
|
2429
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
2430
|
+
}
|
|
2431
|
+
finally { if (e_1) throw e_1.error; }
|
|
2432
|
+
}
|
|
2433
|
+
if (promptTemplate.executionType === 'SCRIPT') {
|
|
2434
|
+
try {
|
|
2435
|
+
for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
2436
|
+
var parameterName = _f.value;
|
|
2437
|
+
parameterNames.add(parameterName);
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
2441
|
+
finally {
|
|
2442
|
+
try {
|
|
2443
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
2444
|
+
}
|
|
2445
|
+
finally { if (e_2) throw e_2.error; }
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
return parameterNames;
|
|
2449
|
+
}
|
|
2450
|
+
/**
|
|
2451
|
+
* TODO: [🔣] If script require contentLanguage
|
|
2452
|
+
*/
|
|
2453
|
+
|
|
2454
|
+
/* tslint:disable */
|
|
2455
|
+
/*
|
|
2456
|
+
TODO: Tests
|
|
2457
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'Moje tabule' })).toEqual('/VtG7sR9rRJqwNEdM2/Moje tabule');
|
|
2458
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: 'ěščřžžýáíúů' })).toEqual('/VtG7sR9rRJqwNEdM2/escrzyaieuu');
|
|
2459
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj');
|
|
2460
|
+
expect(encodeRoutePath({ uriId: 'VtG7sR9rRJqwNEdM2', name: ' ahoj_ahojAhoj ahoj ' })).toEqual('/VtG7sR9rRJqwNEdM2/ahoj-ahoj-ahoj-ahoj');
|
|
2461
|
+
*/
|
|
2462
|
+
function normalizeTo_SCREAMING_CASE(sentence) {
|
|
2463
|
+
var e_1, _a;
|
|
2464
|
+
var charType;
|
|
2465
|
+
var lastCharType = 'OTHER';
|
|
2466
|
+
var normalizedName = '';
|
|
2467
|
+
try {
|
|
2468
|
+
for (var sentence_1 = __values(sentence), sentence_1_1 = sentence_1.next(); !sentence_1_1.done; sentence_1_1 = sentence_1.next()) {
|
|
2469
|
+
var char = sentence_1_1.value;
|
|
2470
|
+
var normalizedChar = void 0;
|
|
2471
|
+
if (/^[a-z]$/.test(char)) {
|
|
2472
|
+
charType = 'LOWERCASE';
|
|
2473
|
+
normalizedChar = char.toUpperCase();
|
|
2474
|
+
}
|
|
2475
|
+
else if (/^[A-Z]$/.test(char)) {
|
|
2476
|
+
charType = 'UPPERCASE';
|
|
2477
|
+
normalizedChar = char;
|
|
2478
|
+
}
|
|
2479
|
+
else if (/^[0-9]$/.test(char)) {
|
|
2480
|
+
charType = 'NUMBER';
|
|
2481
|
+
normalizedChar = char;
|
|
2482
|
+
}
|
|
2483
|
+
else if (/^\/$/.test(char)) {
|
|
2484
|
+
charType = 'SLASH';
|
|
2485
|
+
normalizedChar = char;
|
|
2486
|
+
}
|
|
2487
|
+
else {
|
|
2488
|
+
charType = 'OTHER';
|
|
2489
|
+
normalizedChar = '_';
|
|
2490
|
+
}
|
|
2491
|
+
if (charType !== lastCharType &&
|
|
2492
|
+
!(lastCharType === 'UPPERCASE' && charType === 'LOWERCASE') &&
|
|
2493
|
+
!(lastCharType === 'NUMBER') &&
|
|
2494
|
+
!(charType === 'NUMBER')) {
|
|
2495
|
+
normalizedName += '_';
|
|
2496
|
+
}
|
|
2497
|
+
normalizedName += normalizedChar;
|
|
2498
|
+
lastCharType = charType;
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2502
|
+
finally {
|
|
2503
|
+
try {
|
|
2504
|
+
if (sentence_1_1 && !sentence_1_1.done && (_a = sentence_1.return)) _a.call(sentence_1);
|
|
2505
|
+
}
|
|
2506
|
+
finally { if (e_1) throw e_1.error; }
|
|
2507
|
+
}
|
|
2508
|
+
normalizedName = normalizedName.replace(/_+/g, '_');
|
|
2509
|
+
normalizedName = normalizedName.replace(/_?\/_?/g, '/');
|
|
2510
|
+
normalizedName = normalizedName.replace(/^_/, '');
|
|
2511
|
+
normalizedName = normalizedName.replace(/_$/, '');
|
|
2512
|
+
return normalizedName;
|
|
2513
|
+
}
|
|
2514
|
+
/**
|
|
2515
|
+
* TODO: [🌺] Use some intermediate util splitWords
|
|
2516
|
+
*/
|
|
2517
|
+
|
|
2518
|
+
/**
|
|
2519
|
+
* Execution type describes the way how the block is executed
|
|
2520
|
+
*
|
|
2521
|
+
* @see https://github.com/webgptorg/promptbook#execution-type
|
|
2522
|
+
*/
|
|
2523
|
+
var ExecutionTypes = [
|
|
2524
|
+
'PROMPT_TEMPLATE',
|
|
2525
|
+
'SIMPLE_TEMPLATE',
|
|
2526
|
+
'SCRIPT',
|
|
2527
|
+
'PROMPT_DIALOG',
|
|
2528
|
+
// <- [🥻] Insert here when making new command
|
|
2529
|
+
];
|
|
2530
|
+
|
|
2531
|
+
/**
|
|
2532
|
+
* Units of text measurement
|
|
2533
|
+
*/
|
|
2534
|
+
var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
|
|
2535
|
+
/**
|
|
2536
|
+
* TODO: [💝] Unite object for expecting amount and format - remove expectFormat
|
|
2537
|
+
* TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
|
|
2538
|
+
* TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
|
|
2539
|
+
*/
|
|
2540
|
+
|
|
2541
|
+
/**
|
|
2542
|
+
* Removes Markdown formatting tags from a string.
|
|
2543
|
+
*
|
|
2544
|
+
* @param {string} str - The string to remove Markdown tags from.
|
|
2545
|
+
* @returns {string} The input string with all Markdown tags removed.
|
|
2546
|
+
*/
|
|
2547
|
+
function removeMarkdownFormatting(str) {
|
|
2548
|
+
// Remove bold formatting
|
|
2549
|
+
str = str.replace(/\*\*(.*?)\*\*/g, '$1');
|
|
2550
|
+
// Remove italic formatting
|
|
2551
|
+
str = str.replace(/\*(.*?)\*/g, '$1');
|
|
2552
|
+
// Remove code formatting
|
|
2553
|
+
str = str.replace(/`(.*?)`/g, '$1');
|
|
2554
|
+
return str;
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
/**
|
|
2558
|
+
* Function parseNumber will parse number from string
|
|
2559
|
+
*
|
|
2560
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
2561
|
+
* Note: it also works only with decimal numbers
|
|
2562
|
+
*
|
|
2563
|
+
* @returns parsed number
|
|
2564
|
+
* @throws {PromptbookSyntaxError} if the value is not a number
|
|
2565
|
+
*
|
|
2566
|
+
* @private within the parseCommand
|
|
2567
|
+
*/
|
|
2568
|
+
function parseNumber(value) {
|
|
2569
|
+
var originalValue = value;
|
|
2570
|
+
if (typeof value === 'number') {
|
|
2571
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
2572
|
+
}
|
|
2573
|
+
if (typeof value !== 'string') {
|
|
2574
|
+
return 0;
|
|
2575
|
+
}
|
|
2576
|
+
value = value.trim();
|
|
2577
|
+
if (value.startsWith('+')) {
|
|
2578
|
+
return parseNumber(value.substring(1));
|
|
2579
|
+
}
|
|
2580
|
+
if (value.startsWith('-')) {
|
|
2581
|
+
var number = parseNumber(value.substring(1));
|
|
2582
|
+
if (number === 0) {
|
|
2583
|
+
return 0; // <- Note: To prevent -0
|
|
2584
|
+
}
|
|
2585
|
+
return -number;
|
|
2586
|
+
}
|
|
2587
|
+
value = value.replace(/,/g, '.');
|
|
2588
|
+
value = value.toUpperCase();
|
|
2589
|
+
if (value === '') {
|
|
2590
|
+
return 0;
|
|
2591
|
+
}
|
|
2592
|
+
if (value === '♾' || value.startsWith('INF')) {
|
|
2593
|
+
return Infinity;
|
|
2594
|
+
}
|
|
2595
|
+
if (value.includes('/')) {
|
|
2596
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
2597
|
+
var numerator = parseNumber(numerator_);
|
|
2598
|
+
var denominator = parseNumber(denominator_);
|
|
2599
|
+
if (denominator === 0) {
|
|
2600
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
2601
|
+
}
|
|
2602
|
+
return numerator / denominator;
|
|
2603
|
+
}
|
|
2604
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
2605
|
+
return 0;
|
|
2606
|
+
}
|
|
2607
|
+
if (value.includes('E')) {
|
|
2608
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
2609
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
2610
|
+
}
|
|
2611
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
2612
|
+
throw new PromptbookSyntaxError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
2613
|
+
}
|
|
2614
|
+
var num = parseFloat(value);
|
|
2615
|
+
if (isNaN(num)) {
|
|
2616
|
+
throw new PromptbookSyntaxError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
2617
|
+
}
|
|
2618
|
+
return num;
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
2622
|
+
*/
|
|
2623
|
+
|
|
2624
|
+
/**
|
|
2625
|
+
* Parses one line of ul/ol to command
|
|
2626
|
+
*
|
|
2627
|
+
* @returns parsed command object
|
|
2628
|
+
* @throws {PromptbookSyntaxError} if the command is invalid
|
|
2629
|
+
*
|
|
2630
|
+
* @private within the promptbookStringToJson
|
|
2631
|
+
*/
|
|
2632
|
+
function parseCommand(listItem) {
|
|
2633
|
+
var e_1, _a;
|
|
2634
|
+
if (listItem.includes('\n') || listItem.includes('\r')) {
|
|
2635
|
+
throw new PromptbookSyntaxError('Command can not contain new line characters:');
|
|
2636
|
+
}
|
|
2637
|
+
var type = listItem.trim();
|
|
2638
|
+
type = type.split('`').join('');
|
|
2639
|
+
type = type.split('"').join('');
|
|
2640
|
+
type = type.split("'").join('');
|
|
2641
|
+
type = type.split('~').join('');
|
|
2642
|
+
type = type.split('[').join('');
|
|
2643
|
+
type = type.split(']').join('');
|
|
2644
|
+
type = type.split('(').join('');
|
|
2645
|
+
type = type.split(')').join('');
|
|
2646
|
+
type = normalizeTo_SCREAMING_CASE(type);
|
|
2647
|
+
type = type.split('DIALOGUE').join('DIALOG');
|
|
2648
|
+
var listItemParts = listItem
|
|
2649
|
+
.split(' ')
|
|
2650
|
+
.map(function (part) { return part.trim(); })
|
|
2651
|
+
.filter(function (item) { return item !== ''; })
|
|
2652
|
+
.filter(function (item) { return !/^PTBK$/i.test(item); })
|
|
2653
|
+
.filter(function (item) { return !/^PROMPTBOOK$/i.test(item); })
|
|
2654
|
+
.map(removeMarkdownFormatting);
|
|
2655
|
+
if (type.startsWith('URL') ||
|
|
2656
|
+
type.startsWith('PTBK_URL') ||
|
|
2657
|
+
type.startsWith('PTBKURL') ||
|
|
2658
|
+
type.startsWith('PROMPTBOOK_URL') ||
|
|
2659
|
+
type.startsWith('PROMPTBOOKURL') ||
|
|
2660
|
+
type.startsWith('HTTPS')) {
|
|
2661
|
+
if (!(listItemParts.length === 2 || (listItemParts.length === 1 && type.startsWith('HTTPS')))) {
|
|
2662
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n ")));
|
|
2663
|
+
}
|
|
2664
|
+
var promptbookUrlString = listItemParts.pop();
|
|
2665
|
+
var promptbookUrl = new URL(promptbookUrlString);
|
|
2666
|
+
if (promptbookUrl.protocol !== 'https:') {
|
|
2667
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n Protocol must be HTTPS\n ")));
|
|
2668
|
+
}
|
|
2669
|
+
if (promptbookUrl.hash !== '') {
|
|
2670
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_URL command:\n\n - ".concat(listItem, "\n\n URL must not contain hash\n Hash is used for identification of the prompt template in the pipeline\n ")));
|
|
2671
|
+
}
|
|
2672
|
+
return {
|
|
2673
|
+
type: 'PROMPTBOOK_URL',
|
|
2674
|
+
promptbookUrl: promptbookUrl,
|
|
2675
|
+
};
|
|
2676
|
+
}
|
|
2677
|
+
else if (type.startsWith('PROMPTBOOK_VERSION') || type.startsWith('PTBK_VERSION')) {
|
|
2678
|
+
if (listItemParts.length !== 2) {
|
|
2679
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid PROMPTBOOK_VERSION command:\n\n - ".concat(listItem, "\n ")));
|
|
2680
|
+
}
|
|
2681
|
+
var promptbookVersion = listItemParts.pop();
|
|
2682
|
+
// TODO: Validate version
|
|
2683
|
+
return {
|
|
2684
|
+
type: 'PROMPTBOOK_VERSION',
|
|
2685
|
+
promptbookVersion: promptbookVersion,
|
|
2686
|
+
};
|
|
2687
|
+
}
|
|
2688
|
+
else if (type.startsWith('EXECUTE') ||
|
|
2689
|
+
type.startsWith('EXEC') ||
|
|
2690
|
+
type.startsWith('PROMPT_DIALOG') ||
|
|
2691
|
+
type.startsWith('SIMPLE_TEMPLATE')) {
|
|
2692
|
+
var executionTypes = ExecutionTypes.filter(function (executionType) { return type.includes(executionType); });
|
|
2693
|
+
if (executionTypes.length !== 1) {
|
|
2694
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown execution type in command:\n\n - ".concat(listItem, "\n\n Supported execution types are:\n ").concat(block(ExecutionTypes.join(', ')), "\n "); }));
|
|
2695
|
+
}
|
|
2696
|
+
return {
|
|
2697
|
+
type: 'EXECUTE',
|
|
2698
|
+
executionType: executionTypes[0],
|
|
2699
|
+
};
|
|
2700
|
+
}
|
|
2701
|
+
else if (type.startsWith('MODEL')) {
|
|
2702
|
+
// TODO: Make this more elegant and dynamically
|
|
2703
|
+
if (type.startsWith('MODEL_VARIANT')) {
|
|
2704
|
+
if (type === 'MODEL_VARIANT_CHAT') {
|
|
2705
|
+
return {
|
|
2706
|
+
type: 'MODEL',
|
|
2707
|
+
key: 'modelVariant',
|
|
2708
|
+
value: 'CHAT',
|
|
2709
|
+
};
|
|
2710
|
+
}
|
|
2711
|
+
else if (type === 'MODEL_VARIANT_COMPLETION') {
|
|
2712
|
+
return {
|
|
2713
|
+
type: 'MODEL',
|
|
2714
|
+
key: 'modelVariant',
|
|
2715
|
+
value: 'COMPLETION',
|
|
2716
|
+
};
|
|
2717
|
+
}
|
|
2718
|
+
else {
|
|
2719
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n - ".concat(listItem, "\n\n Supported variants are:\n ").concat(block(['CHAT', 'COMPLETION'].join(', ')), "\n "); }));
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
if (type.startsWith('MODEL_NAME')) {
|
|
2723
|
+
return {
|
|
2724
|
+
type: 'MODEL',
|
|
2725
|
+
key: 'modelName',
|
|
2726
|
+
value: listItemParts.pop(),
|
|
2727
|
+
};
|
|
2728
|
+
}
|
|
2729
|
+
else {
|
|
2730
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Unknown model key in command:\n\n - ".concat(listItem, "\n\n Supported model keys are:\n ").concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
else if (type.startsWith('PARAM') ||
|
|
2734
|
+
type.startsWith('INPUT_PARAM') ||
|
|
2735
|
+
type.startsWith('OUTPUT_PARAM') ||
|
|
2736
|
+
listItem.startsWith('{') ||
|
|
2737
|
+
listItem.startsWith('> {') /* <- Note: This is a bit hack to parse return parameters defined at the end of each section */) {
|
|
2738
|
+
var parametersMatch = listItem.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
|
|
2739
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2740
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2741
|
+
}
|
|
2742
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2743
|
+
var _b = parametersMatch.groups, parameterName = _b.parameterName, parameterDescription = _b.parameterDescription;
|
|
2744
|
+
if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
|
|
2745
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Parameter {".concat(parameterName, "} can not contain another parameter in description:\n\n - ").concat(listItem, "\n ")));
|
|
2746
|
+
}
|
|
2747
|
+
var isInput = type.startsWith('INPUT');
|
|
2748
|
+
var isOutput = type.startsWith('OUTPUT');
|
|
2749
|
+
if (listItem.startsWith('> {')) {
|
|
2750
|
+
isInput = false;
|
|
2751
|
+
isOutput = false;
|
|
2752
|
+
}
|
|
2753
|
+
return {
|
|
2754
|
+
type: 'PARAMETER',
|
|
2755
|
+
parameterName: parameterName,
|
|
2756
|
+
parameterDescription: parameterDescription.trim() || null,
|
|
2757
|
+
isInput: isInput,
|
|
2758
|
+
isOutput: isOutput,
|
|
2759
|
+
};
|
|
2760
|
+
}
|
|
2761
|
+
else if (type.startsWith('JOKER')) {
|
|
2762
|
+
if (listItemParts.length !== 2) {
|
|
2763
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid JOKER command:\n\n - ".concat(listItem, "\n ")));
|
|
2764
|
+
}
|
|
2765
|
+
var parametersMatch = (listItemParts.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
|
|
2766
|
+
if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
|
|
2767
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid parameter in command:\n\n - ".concat(listItem, "\n ")));
|
|
2768
|
+
}
|
|
2769
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2770
|
+
var parameterName = parametersMatch.groups.parameterName;
|
|
2771
|
+
return {
|
|
2772
|
+
type: 'JOKER',
|
|
2773
|
+
parameterName: parameterName,
|
|
2774
|
+
};
|
|
2775
|
+
}
|
|
2776
|
+
else if (type.startsWith('POSTPROCESS') || type.startsWith('POST_PROCESS')) {
|
|
2777
|
+
if (listItemParts.length !== 2) {
|
|
2778
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid POSTPROCESSING command:\n\n - ".concat(listItem, "\n ")));
|
|
2779
|
+
}
|
|
2780
|
+
var functionName = listItemParts.pop();
|
|
2781
|
+
return {
|
|
2782
|
+
type: 'POSTPROCESS',
|
|
2783
|
+
functionName: functionName,
|
|
2784
|
+
};
|
|
2785
|
+
}
|
|
2786
|
+
else if (type.startsWith('EXPECT_JSON')) {
|
|
2787
|
+
return {
|
|
2788
|
+
type: 'EXPECT_FORMAT',
|
|
2789
|
+
format: 'JSON',
|
|
2790
|
+
};
|
|
2791
|
+
// [🥤]
|
|
2792
|
+
}
|
|
2793
|
+
else if (type.startsWith('EXPECT')) {
|
|
2794
|
+
try {
|
|
2795
|
+
listItemParts.shift();
|
|
2796
|
+
var sign = void 0;
|
|
2797
|
+
var signRaw = listItemParts.shift();
|
|
2798
|
+
if (/^exact/i.test(signRaw)) {
|
|
2799
|
+
sign = 'EXACTLY';
|
|
2800
|
+
}
|
|
2801
|
+
else if (/^min/i.test(signRaw)) {
|
|
2802
|
+
sign = 'MINIMUM';
|
|
2803
|
+
}
|
|
2804
|
+
else if (/^max/i.test(signRaw)) {
|
|
2805
|
+
sign = 'MAXIMUM';
|
|
2806
|
+
}
|
|
2807
|
+
else {
|
|
2808
|
+
throw new PromptbookSyntaxError("Invalid sign \"".concat(signRaw, "\", expected EXACTLY, MIN or MAX"));
|
|
2809
|
+
}
|
|
2810
|
+
var amountRaw = listItemParts.shift();
|
|
2811
|
+
var amount = parseNumber(amountRaw);
|
|
2812
|
+
if (amount < 0) {
|
|
2813
|
+
throw new PromptbookSyntaxError('Amount must be positive number or zero');
|
|
2814
|
+
}
|
|
2815
|
+
if (amount !== Math.floor(amount)) {
|
|
2816
|
+
throw new PromptbookSyntaxError('Amount must be whole number');
|
|
2817
|
+
}
|
|
2818
|
+
var unitRaw = listItemParts.shift();
|
|
2819
|
+
var unit = undefined;
|
|
2820
|
+
try {
|
|
2821
|
+
for (var EXPECTATION_UNITS_1 = __values(EXPECTATION_UNITS), EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next(); !EXPECTATION_UNITS_1_1.done; EXPECTATION_UNITS_1_1 = EXPECTATION_UNITS_1.next()) {
|
|
2822
|
+
var existingUnit = EXPECTATION_UNITS_1_1.value;
|
|
2823
|
+
var existingUnitText = existingUnit;
|
|
2824
|
+
existingUnitText = existingUnitText.substring(0, existingUnitText.length - 1);
|
|
2825
|
+
if (existingUnitText === 'CHARACTER') {
|
|
2826
|
+
existingUnitText = 'CHAR';
|
|
2827
|
+
}
|
|
2828
|
+
if (new RegExp("^".concat(existingUnitText.toLowerCase())).test(unitRaw.toLowerCase()) ||
|
|
2829
|
+
new RegExp("^".concat(unitRaw.toLowerCase())).test(existingUnitText.toLowerCase())) {
|
|
2830
|
+
if (unit !== undefined) {
|
|
2831
|
+
throw new PromptbookSyntaxError("Ambiguous unit \"".concat(unitRaw, "\""));
|
|
2832
|
+
}
|
|
2833
|
+
unit = existingUnit;
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2838
|
+
finally {
|
|
2839
|
+
try {
|
|
2840
|
+
if (EXPECTATION_UNITS_1_1 && !EXPECTATION_UNITS_1_1.done && (_a = EXPECTATION_UNITS_1.return)) _a.call(EXPECTATION_UNITS_1);
|
|
2841
|
+
}
|
|
2842
|
+
finally { if (e_1) throw e_1.error; }
|
|
2843
|
+
}
|
|
2844
|
+
if (unit === undefined) {
|
|
2845
|
+
throw new PromptbookSyntaxError("Invalid unit \"".concat(unitRaw, "\""));
|
|
2846
|
+
}
|
|
2847
|
+
return {
|
|
2848
|
+
type: 'EXPECT_AMOUNT',
|
|
2849
|
+
sign: sign,
|
|
2850
|
+
unit: unit,
|
|
2851
|
+
amount: amount,
|
|
2852
|
+
};
|
|
2853
|
+
}
|
|
2854
|
+
catch (error) {
|
|
2855
|
+
if (!(error instanceof Error)) {
|
|
2856
|
+
throw error;
|
|
2857
|
+
}
|
|
2858
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid EXPECT command; ".concat(error.message, ":\n\n - ").concat(listItem, "\n ")));
|
|
2859
|
+
}
|
|
2860
|
+
/*
|
|
2861
|
+
} else if (type.startsWith('__________________')) {
|
|
2862
|
+
// <- [🥻] Insert here when making new command
|
|
2863
|
+
*/
|
|
2864
|
+
}
|
|
2865
|
+
else {
|
|
2866
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Unknown command:\n\n - ".concat(listItem, "\n\n Supported commands are:\n - PROMPTBOOK_URL <url>\n - PROMPTBOOK_VERSION <version>\n - EXECUTE PROMPT TEMPLATE\n - EXECUTE SIMPLE TEMPLATE\n - SIMPLE TEMPLATE\n - EXECUTE SCRIPT\n - EXECUTE PROMPT_DIALOG'\n - PROMPT_DIALOG'\n - MODEL NAME <name>\n - MODEL VARIANT <\"Chat\"|\"Completion\">\n - INPUT PARAM {<name>} <description>\n - OUTPUT PARAM {<name>} <description>\n - POSTPROCESS `{functionName}`\n - JOKER {<name>}\n - EXPECT JSON\n - EXPECT <\"Exactly\"|\"Min\"|\"Max\"> <number> <\"Chars\"|\"Words\"|\"Sentences\"|\"Paragraphs\"|\"Pages\">\n\n ")));
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
/**
|
|
2871
|
+
* Removes emojis from a string and fix whitespaces
|
|
2872
|
+
*
|
|
2873
|
+
* @param text with emojis
|
|
2874
|
+
* @returns text without emojis
|
|
2875
|
+
*/
|
|
2876
|
+
function removeEmojis(text) {
|
|
2877
|
+
// Replace emojis (and also ZWJ sequence) with hyphens
|
|
2878
|
+
text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
|
|
2879
|
+
text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
|
|
2880
|
+
text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
|
|
2881
|
+
text = text.replace(/\p{Extended_Pictographic}/gu, '');
|
|
2882
|
+
return text;
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
/**
|
|
2886
|
+
* Function normalizes title to name which can be used as identifier
|
|
2887
|
+
*/
|
|
2888
|
+
function titleToName(value) {
|
|
2889
|
+
value = removeEmojis(value);
|
|
2890
|
+
value = normalizeToKebabCase(value);
|
|
2891
|
+
// TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
|
|
2892
|
+
return value;
|
|
2893
|
+
}
|
|
2894
|
+
|
|
2895
|
+
/**
|
|
2896
|
+
* Compile promptbook from string (markdown) format to JSON format
|
|
2897
|
+
*
|
|
2898
|
+
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
2899
|
+
* @param options - Options and tools for the compilation
|
|
2900
|
+
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
2901
|
+
* @throws {PromptbookSyntaxError} if the promptbook string is not valid
|
|
2902
|
+
*
|
|
2903
|
+
* Note: This function does not validate logic of the pipeline only the syntax
|
|
2904
|
+
* Note: This function acts as compilation process
|
|
2905
|
+
*/
|
|
2906
|
+
function promptbookStringToJson(promptbookString, options) {
|
|
2907
|
+
if (options === void 0) { options = {}; }
|
|
2908
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2909
|
+
var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
|
|
2910
|
+
var e_1, _c, e_2, _d;
|
|
2911
|
+
return __generator(this, function (_e) {
|
|
2912
|
+
switch (_e.label) {
|
|
2913
|
+
case 0:
|
|
2914
|
+
llmTools = options.llmTools;
|
|
2915
|
+
promptbookJson = {
|
|
2916
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2917
|
+
title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
|
|
2918
|
+
promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
|
|
2919
|
+
promptbookVersion: PROMPTBOOK_VERSION,
|
|
2920
|
+
description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
|
|
2921
|
+
parameters: [],
|
|
2922
|
+
promptTemplates: [],
|
|
2923
|
+
knowledge: [],
|
|
2924
|
+
};
|
|
2925
|
+
if (!llmTools) return [3 /*break*/, 2];
|
|
2926
|
+
return [4 /*yield*/, prepareKnowledgeFromMarkdown({
|
|
2927
|
+
content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
|
|
2928
|
+
llmTools: llmTools,
|
|
2929
|
+
})];
|
|
2930
|
+
case 1:
|
|
2931
|
+
knowledge = _e.sent();
|
|
2932
|
+
console.info('!!!! knowledge', knowledge);
|
|
2933
|
+
_e.label = 2;
|
|
2934
|
+
case 2:
|
|
2935
|
+
// =============================================================
|
|
2936
|
+
// Note: 1️⃣ Normalization of the PROMPTBOOK string
|
|
2937
|
+
promptbookString = removeContentComments(promptbookString);
|
|
2938
|
+
promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
|
|
2939
|
+
promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
|
|
2940
|
+
addParam = function (parameterCommand) {
|
|
2941
|
+
var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
|
|
2942
|
+
var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
2943
|
+
if (existingParameter &&
|
|
2944
|
+
existingParameter.description &&
|
|
2945
|
+
existingParameter.description !== parameterDescription &&
|
|
2946
|
+
parameterDescription) {
|
|
2947
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
|
|
2948
|
+
}
|
|
2949
|
+
if (existingParameter) {
|
|
2950
|
+
if (parameterDescription) {
|
|
2951
|
+
existingParameter.description = parameterDescription;
|
|
2952
|
+
}
|
|
2953
|
+
}
|
|
2954
|
+
else {
|
|
2955
|
+
promptbookJson.parameters.push({
|
|
2956
|
+
name: parameterName,
|
|
2957
|
+
description: parameterDescription || undefined,
|
|
2958
|
+
isInput: isInput,
|
|
2959
|
+
isOutput: isOutput,
|
|
2960
|
+
});
|
|
2961
|
+
}
|
|
2962
|
+
};
|
|
2963
|
+
markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
2964
|
+
markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
2965
|
+
if (markdownStructureDeepness !== 2) {
|
|
2966
|
+
throw new PromptbookSyntaxError(spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
|
|
2967
|
+
}
|
|
2968
|
+
promptbookJson.title = markdownStructure.title;
|
|
2969
|
+
description = markdownStructure.content;
|
|
2970
|
+
// Note: Remove codeblocks
|
|
2971
|
+
description = description.split(/^```.*^```/gms).join('');
|
|
2972
|
+
//Note: Remove lists and return statement
|
|
2973
|
+
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
2974
|
+
description = spaceTrim(description);
|
|
2975
|
+
if (description === '') {
|
|
2976
|
+
description = undefined;
|
|
2977
|
+
}
|
|
2978
|
+
promptbookJson.description = description;
|
|
2979
|
+
defaultModelRequirements = {};
|
|
2980
|
+
listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
|
|
2981
|
+
try {
|
|
2982
|
+
for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
|
|
2983
|
+
listItem = listItems_1_1.value;
|
|
2984
|
+
command = parseCommand(listItem);
|
|
2985
|
+
switch (command.type) {
|
|
2986
|
+
case 'PROMPTBOOK_URL':
|
|
2987
|
+
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
2988
|
+
break;
|
|
2989
|
+
case 'PROMPTBOOK_VERSION':
|
|
2990
|
+
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
2991
|
+
break;
|
|
2992
|
+
case 'MODEL':
|
|
2993
|
+
defaultModelRequirements[command.key] = command.value;
|
|
2994
|
+
break;
|
|
2995
|
+
case 'PARAMETER':
|
|
2996
|
+
addParam(command);
|
|
2997
|
+
break;
|
|
2998
|
+
default:
|
|
2999
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
}
|
|
3003
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3004
|
+
finally {
|
|
3005
|
+
try {
|
|
3006
|
+
if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
|
|
3007
|
+
}
|
|
3008
|
+
finally { if (e_1) throw e_1.error; }
|
|
3009
|
+
}
|
|
3010
|
+
_loop_1 = function (section) {
|
|
3011
|
+
var e_3, _f;
|
|
3012
|
+
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
3013
|
+
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
3014
|
+
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
3015
|
+
var dependentParameterNames = new Set();
|
|
3016
|
+
var executionType = 'PROMPT_TEMPLATE';
|
|
3017
|
+
var jokers = [];
|
|
3018
|
+
var postprocessing = [];
|
|
3019
|
+
var expectAmount = {};
|
|
3020
|
+
var expectFormat = undefined;
|
|
3021
|
+
var isExecutionTypeChanged = false;
|
|
3022
|
+
try {
|
|
3023
|
+
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()) {
|
|
3024
|
+
var listItem = listItems_2_1.value;
|
|
3025
|
+
var command = parseCommand(listItem);
|
|
3026
|
+
switch (command.type) {
|
|
3027
|
+
case 'JOKER':
|
|
3028
|
+
jokers.push(command.parameterName);
|
|
3029
|
+
dependentParameterNames.add(command.parameterName);
|
|
3030
|
+
break;
|
|
3031
|
+
case 'EXECUTE':
|
|
3032
|
+
if (isExecutionTypeChanged) {
|
|
3033
|
+
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
3034
|
+
}
|
|
3035
|
+
executionType = command.executionType;
|
|
3036
|
+
isExecutionTypeChanged = true;
|
|
3037
|
+
break;
|
|
3038
|
+
case 'MODEL':
|
|
3039
|
+
templateModelRequirements[command.key] = command.value;
|
|
3040
|
+
break;
|
|
3041
|
+
case 'PARAMETER':
|
|
3042
|
+
// Note: This is just for detecting resulitng parameter name
|
|
3043
|
+
addParam(command);
|
|
3044
|
+
break;
|
|
3045
|
+
case 'POSTPROCESS':
|
|
3046
|
+
postprocessing.push(command.functionName);
|
|
3047
|
+
break;
|
|
3048
|
+
case 'EXPECT_AMOUNT':
|
|
3049
|
+
// eslint-disable-next-line no-case-declarations
|
|
3050
|
+
var unit = command.unit.toLowerCase();
|
|
3051
|
+
expectAmount[unit] = expectAmount[unit] || {};
|
|
3052
|
+
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
3053
|
+
if (expectAmount[unit].min !== undefined) {
|
|
3054
|
+
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3055
|
+
}
|
|
3056
|
+
expectAmount[unit].min = command.amount;
|
|
3057
|
+
} /* not else */
|
|
3058
|
+
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
3059
|
+
if (expectAmount[unit].max !== undefined) {
|
|
3060
|
+
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3061
|
+
}
|
|
3062
|
+
expectAmount[unit].max = command.amount;
|
|
3063
|
+
}
|
|
3064
|
+
break;
|
|
3065
|
+
case 'EXPECT_FORMAT':
|
|
3066
|
+
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
3067
|
+
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
3068
|
+
}
|
|
3069
|
+
expectFormat = command.format;
|
|
3070
|
+
break;
|
|
3071
|
+
default:
|
|
3072
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
3077
|
+
finally {
|
|
3078
|
+
try {
|
|
3079
|
+
if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
|
|
3080
|
+
}
|
|
3081
|
+
finally { if (e_3) throw e_3.error; }
|
|
3082
|
+
}
|
|
3083
|
+
var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
|
|
3084
|
+
if (executionType === 'SCRIPT') {
|
|
3085
|
+
if (!language) {
|
|
3086
|
+
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
3087
|
+
}
|
|
3088
|
+
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
3089
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
var lastLine = section.content.split('\n').pop();
|
|
3093
|
+
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
3094
|
+
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
3095
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
3096
|
+
// TODO: Show code of invalid sections each time + DRY
|
|
3097
|
+
section.content
|
|
3098
|
+
.split('\n')
|
|
3099
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3100
|
+
.join('\n')), "\n "); }));
|
|
3101
|
+
}
|
|
3102
|
+
var resultingParameterName = match.groups.resultingParamName;
|
|
3103
|
+
// TODO: [1] DRY description
|
|
3104
|
+
var description_1 = section.content;
|
|
3105
|
+
// Note: Remove codeblocks
|
|
3106
|
+
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
3107
|
+
//Note: Remove lists and return statement
|
|
3108
|
+
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
3109
|
+
description_1 = spaceTrim(description_1);
|
|
3110
|
+
if (description_1 === '') {
|
|
3111
|
+
description_1 = undefined;
|
|
3112
|
+
}
|
|
3113
|
+
if (Object.keys(jokers).length === 0) {
|
|
3114
|
+
jokers = undefined;
|
|
2028
3115
|
}
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
3116
|
+
if (Object.keys(expectAmount).length === 0) {
|
|
3117
|
+
expectAmount = undefined;
|
|
3118
|
+
}
|
|
3119
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
3120
|
+
postprocessing = undefined;
|
|
3121
|
+
}
|
|
3122
|
+
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
3123
|
+
if (templateModelRequirements.modelVariant === undefined) {
|
|
3124
|
+
templateModelRequirements.modelVariant = 'CHAT';
|
|
3125
|
+
}
|
|
3126
|
+
var template = {
|
|
3127
|
+
name: titleToName(section.title),
|
|
3128
|
+
title: section.title,
|
|
3129
|
+
description: description_1,
|
|
3130
|
+
dependentParameterNames: Array.from(dependentParameterNames),
|
|
3131
|
+
executionType: executionType,
|
|
3132
|
+
jokers: jokers,
|
|
3133
|
+
postprocessing: postprocessing,
|
|
3134
|
+
expectations: expectAmount,
|
|
3135
|
+
expectFormat: expectFormat,
|
|
3136
|
+
modelRequirements: templateModelRequirements,
|
|
3137
|
+
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
3138
|
+
content: content,
|
|
3139
|
+
resultingParameterName: resultingParameterName,
|
|
3140
|
+
};
|
|
3141
|
+
if (executionType !== 'PROMPT_TEMPLATE') {
|
|
3142
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3143
|
+
delete template.modelRequirements;
|
|
3144
|
+
}
|
|
3145
|
+
promptbookJson.promptTemplates.push(template);
|
|
3146
|
+
};
|
|
2033
3147
|
try {
|
|
2034
|
-
|
|
3148
|
+
for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
3149
|
+
section = _b.value;
|
|
3150
|
+
_loop_1(section);
|
|
3151
|
+
}
|
|
2035
3152
|
}
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
promptbookString += '```' + contentLanguage;
|
|
2049
|
-
promptbookString += '\n';
|
|
2050
|
-
promptbookString += spaceTrim$1(content);
|
|
2051
|
-
// <- TODO: !!! Escape
|
|
2052
|
-
// <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
|
|
2053
|
-
promptbookString += '\n';
|
|
2054
|
-
promptbookString += '```';
|
|
2055
|
-
promptbookString += '\n\n';
|
|
2056
|
-
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
2060
|
-
finally {
|
|
2061
|
-
try {
|
|
2062
|
-
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_c = promptTemplates_1.return)) _c.call(promptTemplates_1);
|
|
2063
|
-
}
|
|
2064
|
-
finally { if (e_3) throw e_3.error; }
|
|
2065
|
-
}
|
|
2066
|
-
return promptbookString;
|
|
2067
|
-
}
|
|
2068
|
-
/**
|
|
2069
|
-
* @private internal util of promptbookJsonToString
|
|
2070
|
-
*/
|
|
2071
|
-
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
2072
|
-
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
2073
|
-
var parameterString = "{".concat(name, "}");
|
|
2074
|
-
if (description) {
|
|
2075
|
-
parameterString = "".concat(parameterString, " ").concat(description);
|
|
2076
|
-
}
|
|
2077
|
-
return parameterString;
|
|
3153
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3154
|
+
finally {
|
|
3155
|
+
try {
|
|
3156
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
3157
|
+
}
|
|
3158
|
+
finally { if (e_2) throw e_2.error; }
|
|
3159
|
+
}
|
|
3160
|
+
// =============================================================
|
|
3161
|
+
return [2 /*return*/, promptbookJson];
|
|
3162
|
+
}
|
|
3163
|
+
});
|
|
3164
|
+
});
|
|
2078
3165
|
}
|
|
2079
3166
|
/**
|
|
2080
|
-
* TODO:
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
/**
|
|
2084
|
-
* This error indicates that promptbook not found in the library
|
|
3167
|
+
* TODO: Report here line/column of error
|
|
3168
|
+
* TODO: Use spaceTrim more effectively
|
|
3169
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
2085
3170
|
*/
|
|
2086
|
-
var PromptbookNotFoundError = /** @class */ (function (_super) {
|
|
2087
|
-
__extends(PromptbookNotFoundError, _super);
|
|
2088
|
-
function PromptbookNotFoundError(message) {
|
|
2089
|
-
var _this = _super.call(this, message) || this;
|
|
2090
|
-
_this.name = 'PromptbookNotFoundError';
|
|
2091
|
-
Object.setPrototypeOf(_this, PromptbookNotFoundError.prototype);
|
|
2092
|
-
return _this;
|
|
2093
|
-
}
|
|
2094
|
-
return PromptbookNotFoundError;
|
|
2095
|
-
}(Error));
|
|
2096
3171
|
|
|
2097
3172
|
/**
|
|
2098
|
-
* This error indicates
|
|
3173
|
+
* This error indicates that the promptbook library cannot be propperly loaded
|
|
2099
3174
|
*/
|
|
2100
|
-
var
|
|
2101
|
-
__extends(
|
|
2102
|
-
function
|
|
3175
|
+
var PromptbookLibraryError = /** @class */ (function (_super) {
|
|
3176
|
+
__extends(PromptbookLibraryError, _super);
|
|
3177
|
+
function PromptbookLibraryError(message) {
|
|
2103
3178
|
var _this = _super.call(this, message) || this;
|
|
2104
|
-
_this.name = '
|
|
2105
|
-
Object.setPrototypeOf(_this,
|
|
3179
|
+
_this.name = 'PromptbookLibraryError';
|
|
3180
|
+
Object.setPrototypeOf(_this, PromptbookLibraryError.prototype);
|
|
2106
3181
|
return _this;
|
|
2107
3182
|
}
|
|
2108
|
-
return
|
|
3183
|
+
return PromptbookLibraryError;
|
|
2109
3184
|
}(Error));
|
|
2110
3185
|
|
|
2111
3186
|
/**
|
|
2112
|
-
*
|
|
2113
|
-
* This implementation is a very thin wrapper around the Array / Map of promptbooks.
|
|
2114
|
-
*
|
|
2115
|
-
* @see https://github.com/webgptorg/promptbook#promptbook-library
|
|
3187
|
+
* Detects if the code is running in a browser environment in main thread (Not in a web worker)
|
|
2116
3188
|
*/
|
|
2117
|
-
|
|
2118
|
-
/**
|
|
2119
|
-
* Constructs a promptbook library from promptbooks
|
|
2120
|
-
*
|
|
2121
|
-
* @param promptbooks !!!
|
|
2122
|
-
*
|
|
2123
|
-
* Note: During the construction logic of all promptbooks are validated
|
|
2124
|
-
* Note: It is not recommended to use this constructor directly, use `createPromptbookLibraryFromSources` *(or other variant)* instead
|
|
2125
|
-
*/
|
|
2126
|
-
function SimplePromptbookLibrary() {
|
|
2127
|
-
var e_1, _a;
|
|
2128
|
-
var promptbooks = [];
|
|
2129
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2130
|
-
promptbooks[_i] = arguments[_i];
|
|
2131
|
-
}
|
|
2132
|
-
this.library = new Map();
|
|
2133
|
-
try {
|
|
2134
|
-
for (var promptbooks_1 = __values(promptbooks), promptbooks_1_1 = promptbooks_1.next(); !promptbooks_1_1.done; promptbooks_1_1 = promptbooks_1.next()) {
|
|
2135
|
-
var promptbook = promptbooks_1_1.value;
|
|
2136
|
-
if (promptbook.promptbookUrl === undefined) {
|
|
2137
|
-
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
|
|
2138
|
-
}
|
|
2139
|
-
validatePromptbookJson(promptbook);
|
|
2140
|
-
// Note: [🦄]
|
|
2141
|
-
if (this.library.has(promptbook.promptbookUrl) &&
|
|
2142
|
-
promptbookJsonToString(promptbook) !==
|
|
2143
|
-
promptbookJsonToString(this.library.get(promptbook.promptbookUrl))) {
|
|
2144
|
-
throw new PromptbookReferenceError(spaceTrim("\n Promptbook with URL \"".concat(promptbook.promptbookUrl, "\" is already in the library\n\n Note: Promptbooks with the same URL are not allowed\n Note: Automatically check whether the promptbooks are the same BUT they are DIFFERENT\n\n ")));
|
|
2145
|
-
}
|
|
2146
|
-
this.library.set(promptbook.promptbookUrl, promptbook);
|
|
2147
|
-
}
|
|
2148
|
-
}
|
|
2149
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2150
|
-
finally {
|
|
2151
|
-
try {
|
|
2152
|
-
if (promptbooks_1_1 && !promptbooks_1_1.done && (_a = promptbooks_1.return)) _a.call(promptbooks_1);
|
|
2153
|
-
}
|
|
2154
|
-
finally { if (e_1) throw e_1.error; }
|
|
2155
|
-
}
|
|
2156
|
-
}
|
|
2157
|
-
/**
|
|
2158
|
-
* Gets all promptbooks in the library
|
|
2159
|
-
*/
|
|
2160
|
-
SimplePromptbookLibrary.prototype.listPromptbooks = function () {
|
|
2161
|
-
return Array.from(this.library.keys());
|
|
2162
|
-
};
|
|
2163
|
-
/**
|
|
2164
|
-
* Gets promptbook by its URL
|
|
2165
|
-
*
|
|
2166
|
-
* Note: This is not a direct fetching from the URL, but a lookup in the library
|
|
2167
|
-
*/
|
|
2168
|
-
SimplePromptbookLibrary.prototype.getPromptbookByUrl = function (url) {
|
|
2169
|
-
var _this = this;
|
|
2170
|
-
var promptbook = this.library.get(url);
|
|
2171
|
-
if (!promptbook) {
|
|
2172
|
-
if (this.listPromptbooks().length === 0) {
|
|
2173
|
-
throw new PromptbookNotFoundError(spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
|
|
2174
|
-
}
|
|
2175
|
-
throw new PromptbookNotFoundError(spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
|
|
2176
|
-
.map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
|
|
2177
|
-
.join('\n')), "\n\n "); }));
|
|
2178
|
-
}
|
|
2179
|
-
return promptbook;
|
|
2180
|
-
};
|
|
2181
|
-
/**
|
|
2182
|
-
* Checks whether given prompt was defined in any promptbook in the library
|
|
2183
|
-
*/
|
|
2184
|
-
SimplePromptbookLibrary.prototype.isResponsibleForPrompt = function (prompt) {
|
|
2185
|
-
return true;
|
|
2186
|
-
};
|
|
2187
|
-
return SimplePromptbookLibrary;
|
|
2188
|
-
}());
|
|
2189
|
-
|
|
3189
|
+
new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
|
|
2190
3190
|
/**
|
|
2191
|
-
*
|
|
2192
|
-
*
|
|
2193
|
-
* Note: You can combine `PromptbookString` (`.ptbk.md`) with `PromptbookJson` BUT it is not recommended
|
|
2194
|
-
* Note: During the construction syntax and logic of all sources are validated
|
|
2195
|
-
*
|
|
2196
|
-
* @param promptbookSources
|
|
2197
|
-
* @returns PromptbookLibrary
|
|
3191
|
+
* Detects if the code is running in a Node.js environment
|
|
2198
3192
|
*/
|
|
2199
|
-
|
|
2200
|
-
var promptbookSources = [];
|
|
2201
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2202
|
-
promptbookSources[_i] = arguments[_i];
|
|
2203
|
-
}
|
|
2204
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2205
|
-
var promptbooks, promptbookSources_1, promptbookSources_1_1, source, promptbook, e_1_1;
|
|
2206
|
-
var e_1, _a;
|
|
2207
|
-
return __generator(this, function (_b) {
|
|
2208
|
-
switch (_b.label) {
|
|
2209
|
-
case 0:
|
|
2210
|
-
promptbooks = new Array();
|
|
2211
|
-
_b.label = 1;
|
|
2212
|
-
case 1:
|
|
2213
|
-
_b.trys.push([1, 8, 9, 10]);
|
|
2214
|
-
promptbookSources_1 = __values(promptbookSources), promptbookSources_1_1 = promptbookSources_1.next();
|
|
2215
|
-
_b.label = 2;
|
|
2216
|
-
case 2:
|
|
2217
|
-
if (!!promptbookSources_1_1.done) return [3 /*break*/, 7];
|
|
2218
|
-
source = promptbookSources_1_1.value;
|
|
2219
|
-
promptbook = void 0;
|
|
2220
|
-
if (!(typeof source === 'string')) return [3 /*break*/, 4];
|
|
2221
|
-
return [4 /*yield*/, promptbookStringToJson(source)];
|
|
2222
|
-
case 3:
|
|
2223
|
-
// Note: When directly creating from string, no need to validate the source
|
|
2224
|
-
// The validation is performed always before execution
|
|
2225
|
-
promptbook = _b.sent();
|
|
2226
|
-
return [3 /*break*/, 5];
|
|
2227
|
-
case 4:
|
|
2228
|
-
promptbook = source;
|
|
2229
|
-
_b.label = 5;
|
|
2230
|
-
case 5:
|
|
2231
|
-
promptbooks.push(promptbook);
|
|
2232
|
-
_b.label = 6;
|
|
2233
|
-
case 6:
|
|
2234
|
-
promptbookSources_1_1 = promptbookSources_1.next();
|
|
2235
|
-
return [3 /*break*/, 2];
|
|
2236
|
-
case 7: return [3 /*break*/, 10];
|
|
2237
|
-
case 8:
|
|
2238
|
-
e_1_1 = _b.sent();
|
|
2239
|
-
e_1 = { error: e_1_1 };
|
|
2240
|
-
return [3 /*break*/, 10];
|
|
2241
|
-
case 9:
|
|
2242
|
-
try {
|
|
2243
|
-
if (promptbookSources_1_1 && !promptbookSources_1_1.done && (_a = promptbookSources_1.return)) _a.call(promptbookSources_1);
|
|
2244
|
-
}
|
|
2245
|
-
finally { if (e_1) throw e_1.error; }
|
|
2246
|
-
return [7 /*endfinally*/];
|
|
2247
|
-
case 10: return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
|
|
2248
|
-
}
|
|
2249
|
-
});
|
|
2250
|
-
});
|
|
2251
|
-
}
|
|
3193
|
+
var isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
2252
3194
|
/**
|
|
2253
|
-
*
|
|
3195
|
+
* Detects if the code is running in a web worker
|
|
2254
3196
|
*/
|
|
3197
|
+
new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
|
|
2255
3198
|
|
|
2256
3199
|
/**
|
|
2257
3200
|
* Constructs Promptbook from async sources
|
|
@@ -2260,19 +3203,19 @@ function createPromptbookLibraryFromSources() {
|
|
|
2260
3203
|
* - Factory function that returns Promise of array of PromptbookJson or PromptbookString
|
|
2261
3204
|
*
|
|
2262
3205
|
* Note: This is useful as internal tool for other constructor functions like
|
|
2263
|
-
* `
|
|
3206
|
+
* `createLibraryFromUrl` or `createLibraryFromDirectory`
|
|
2264
3207
|
* Consider using those functions instead of this one
|
|
2265
3208
|
*
|
|
2266
3209
|
* Note: The function does NOT return promise it returns the library directly which waits for the sources to be resolved
|
|
2267
3210
|
* when error occurs in given promise or factory function, it is thrown during `listPromptbooks` or `getPromptbookByUrl` call
|
|
2268
3211
|
*
|
|
2269
|
-
* Note: Consider using `
|
|
3212
|
+
* Note: Consider using `createLibraryFromDirectory` or `createLibraryFromUrl`
|
|
2270
3213
|
*
|
|
2271
3214
|
* @param promptbookSourcesPromiseOrFactory
|
|
2272
3215
|
* @returns PromptbookLibrary
|
|
2273
3216
|
* @deprecated Do not use, it will became internal tool for other constructor functions
|
|
2274
3217
|
*/
|
|
2275
|
-
function
|
|
3218
|
+
function createLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
2276
3219
|
var library;
|
|
2277
3220
|
function forSources() {
|
|
2278
3221
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -2287,9 +3230,7 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
|
2287
3230
|
return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
|
|
2288
3231
|
case 1:
|
|
2289
3232
|
promptbookSources = _a.sent();
|
|
2290
|
-
|
|
2291
|
-
case 2:
|
|
2292
|
-
library = _a.sent();
|
|
3233
|
+
library = createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookSources), false));
|
|
2293
3234
|
return [2 /*return*/];
|
|
2294
3235
|
}
|
|
2295
3236
|
});
|
|
@@ -2337,9 +3278,6 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
|
2337
3278
|
isResponsibleForPrompt: isResponsibleForPrompt,
|
|
2338
3279
|
};
|
|
2339
3280
|
}
|
|
2340
|
-
/**
|
|
2341
|
-
* TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
|
|
2342
|
-
*/
|
|
2343
3281
|
|
|
2344
3282
|
/**
|
|
2345
3283
|
* Constructs Promptbook from given directory
|
|
@@ -2350,32 +3288,42 @@ function createPromptbookLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
|
|
|
2350
3288
|
* @param options - Misc options for the library
|
|
2351
3289
|
* @returns PromptbookLibrary
|
|
2352
3290
|
*/
|
|
2353
|
-
function
|
|
3291
|
+
function createLibraryFromDirectory(path, options) {
|
|
2354
3292
|
return __awaiter(this, void 0, void 0, function () {
|
|
2355
|
-
var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
|
|
3293
|
+
var makedLibraryFilePath, makedLibraryFileExists, _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
|
|
2356
3294
|
var _this = this;
|
|
2357
3295
|
return __generator(this, function (_f) {
|
|
2358
3296
|
switch (_f.label) {
|
|
2359
3297
|
case 0:
|
|
2360
3298
|
if (!isRunningInNode()) {
|
|
2361
|
-
throw new Error('Function `
|
|
3299
|
+
throw new Error('Function `createLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
|
|
3300
|
+
}
|
|
3301
|
+
makedLibraryFilePath = join(path, "".concat(PROMPTBOOK_MAKED_BASE_FILENAME, ".json"));
|
|
3302
|
+
return [4 /*yield*/, access(makedLibraryFilePath, constants.R_OK)
|
|
3303
|
+
.then(function () { return true; })
|
|
3304
|
+
.catch(function () { return false; })];
|
|
3305
|
+
case 1:
|
|
3306
|
+
makedLibraryFileExists = _f.sent();
|
|
3307
|
+
if (!makedLibraryFileExists) {
|
|
3308
|
+
console.info(colors.yellow("Tip: Prebuild your promptbook library (file with supposed prebuild ".concat(makedLibraryFilePath, " not found) with CLI util \"promptbook make\" to speed up the library creation.")));
|
|
3309
|
+
}
|
|
3310
|
+
else {
|
|
3311
|
+
colors.green("Using your prebuild promptbook library ".concat(makedLibraryFilePath));
|
|
3312
|
+
// TODO: !!!!! Implement;
|
|
2362
3313
|
}
|
|
2363
3314
|
_a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c, _d = _a.isLazyLoaded, isLazyLoaded = _d === void 0 ? false : _d, _e = _a.isCrashOnError, isCrashOnError = _e === void 0 ? true : _e;
|
|
2364
|
-
library =
|
|
3315
|
+
library = createLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
2365
3316
|
var fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
|
|
2366
3317
|
var e_1, _a;
|
|
2367
3318
|
return __generator(this, function (_b) {
|
|
2368
3319
|
switch (_b.label) {
|
|
2369
3320
|
case 0:
|
|
2370
3321
|
if (isVerbose) {
|
|
2371
|
-
console.info("Creating promptbook library from path ".concat(path));
|
|
3322
|
+
console.info("Creating promptbook library from path ".concat(path.split('\\').join('/')));
|
|
2372
3323
|
}
|
|
2373
3324
|
return [4 /*yield*/, listAllFiles(path, isRecursive)];
|
|
2374
3325
|
case 1:
|
|
2375
3326
|
fileNames = _b.sent();
|
|
2376
|
-
if (isVerbose) {
|
|
2377
|
-
console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive, fileNames: fileNames });
|
|
2378
|
-
}
|
|
2379
3327
|
promptbooks = [];
|
|
2380
3328
|
_loop_1 = function (fileName) {
|
|
2381
3329
|
var promptbook, promptbookString, _c, _d, error_1, wrappedErrorMessage;
|
|
@@ -2395,7 +3343,7 @@ function createPromptbookLibraryFromDirectory(path, options) {
|
|
|
2395
3343
|
case 3:
|
|
2396
3344
|
if (!fileName.endsWith('.ptbk.json')) return [3 /*break*/, 5];
|
|
2397
3345
|
if (isVerbose) {
|
|
2398
|
-
console.info("Loading ".concat(fileName));
|
|
3346
|
+
console.info("Loading ".concat(fileName.split('\\').join('/')));
|
|
2399
3347
|
}
|
|
2400
3348
|
_d = (_c = JSON).parse;
|
|
2401
3349
|
return [4 /*yield*/, readFile(fileName, 'utf8')];
|
|
@@ -2405,7 +3353,7 @@ function createPromptbookLibraryFromDirectory(path, options) {
|
|
|
2405
3353
|
return [3 /*break*/, 6];
|
|
2406
3354
|
case 5:
|
|
2407
3355
|
if (isVerbose) {
|
|
2408
|
-
console.info("Skipping file ".concat(fileName));
|
|
3356
|
+
console.info("Skipping file ".concat(fileName.split('\\').join('/')));
|
|
2409
3357
|
}
|
|
2410
3358
|
_e.label = 6;
|
|
2411
3359
|
case 6:
|
|
@@ -2413,17 +3361,17 @@ function createPromptbookLibraryFromDirectory(path, options) {
|
|
|
2413
3361
|
if (promptbook !== null) {
|
|
2414
3362
|
if (!promptbook.promptbookUrl) {
|
|
2415
3363
|
if (isVerbose) {
|
|
2416
|
-
console.info("Not loading ".concat(fileName, " - missing URL"));
|
|
3364
|
+
console.info("Not loading ".concat(fileName.split('\\').join('/'), " - missing URL"));
|
|
2417
3365
|
}
|
|
2418
3366
|
}
|
|
2419
3367
|
else {
|
|
2420
3368
|
if (isVerbose) {
|
|
2421
|
-
console.info("Loading ".concat(fileName));
|
|
3369
|
+
console.info("Loading ".concat(fileName.split('\\').join('/')));
|
|
2422
3370
|
}
|
|
2423
3371
|
if (!isCrashOnError) {
|
|
2424
3372
|
// Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
|
|
2425
3373
|
// But be handled in current try-catch block
|
|
2426
|
-
|
|
3374
|
+
validatePromptbook(promptbook);
|
|
2427
3375
|
}
|
|
2428
3376
|
// Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
|
|
2429
3377
|
promptbooks.push(promptbook);
|
|
@@ -2475,12 +3423,12 @@ function createPromptbookLibraryFromDirectory(path, options) {
|
|
|
2475
3423
|
}
|
|
2476
3424
|
});
|
|
2477
3425
|
}); });
|
|
2478
|
-
if (!(isLazyLoaded === false)) return [3 /*break*/,
|
|
3426
|
+
if (!(isLazyLoaded === false)) return [3 /*break*/, 3];
|
|
2479
3427
|
return [4 /*yield*/, library.listPromptbooks()];
|
|
2480
|
-
case
|
|
3428
|
+
case 2:
|
|
2481
3429
|
_f.sent();
|
|
2482
|
-
_f.label =
|
|
2483
|
-
case
|
|
3430
|
+
_f.label = 3;
|
|
3431
|
+
case 3: return [2 /*return*/, library];
|
|
2484
3432
|
}
|
|
2485
3433
|
});
|
|
2486
3434
|
});
|
|
@@ -2491,7 +3439,7 @@ function createPromptbookLibraryFromDirectory(path, options) {
|
|
|
2491
3439
|
* @param path
|
|
2492
3440
|
* @param isRecursive
|
|
2493
3441
|
* @returns List of all files in the directory
|
|
2494
|
-
* @private internal function for `
|
|
3442
|
+
* @private internal function for `createLibraryFromDirectory`
|
|
2495
3443
|
*/
|
|
2496
3444
|
function listAllFiles(path, isRecursive) {
|
|
2497
3445
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -2548,5 +3496,5 @@ function listAllFiles(path, isRecursive) {
|
|
|
2548
3496
|
* TODO: !!!! [🧠] Library precompilation and do not mix markdown and json promptbooks
|
|
2549
3497
|
*/
|
|
2550
3498
|
|
|
2551
|
-
export { PROMPTBOOK_VERSION,
|
|
3499
|
+
export { PROMPTBOOK_VERSION, createLibraryFromDirectory };
|
|
2552
3500
|
//# sourceMappingURL=index.es.js.map
|