@langchain/core 1.1.49-dev-1781048185730 → 1.1.49
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/CHANGELOG.md +8 -0
- package/agents.cjs +1 -0
- package/agents.d.cts +1 -0
- package/agents.d.ts +1 -0
- package/agents.js +1 -0
- package/caches.cjs +1 -0
- package/caches.d.cts +1 -0
- package/caches.d.ts +1 -0
- package/caches.js +1 -0
- package/callbacks/base.cjs +1 -0
- package/callbacks/base.d.cts +1 -0
- package/callbacks/base.d.ts +1 -0
- package/callbacks/base.js +1 -0
- package/callbacks/dispatch/web.cjs +1 -0
- package/callbacks/dispatch/web.d.cts +1 -0
- package/callbacks/dispatch/web.d.ts +1 -0
- package/callbacks/dispatch/web.js +1 -0
- package/callbacks/dispatch.cjs +1 -0
- package/callbacks/dispatch.d.cts +1 -0
- package/callbacks/dispatch.d.ts +1 -0
- package/callbacks/dispatch.js +1 -0
- package/callbacks/manager.cjs +1 -0
- package/callbacks/manager.d.cts +1 -0
- package/callbacks/manager.d.ts +1 -0
- package/callbacks/manager.js +1 -0
- package/callbacks/promises.cjs +1 -0
- package/callbacks/promises.d.cts +1 -0
- package/callbacks/promises.d.ts +1 -0
- package/callbacks/promises.js +1 -0
- package/chat_history.cjs +1 -0
- package/chat_history.d.cts +1 -0
- package/chat_history.d.ts +1 -0
- package/chat_history.js +1 -0
- package/context.cjs +1 -0
- package/context.d.cts +1 -0
- package/context.d.ts +1 -0
- package/context.js +1 -0
- package/dist/callbacks/dispatch/index.cjs.map +1 -1
- package/dist/callbacks/dispatch/index.js.map +1 -1
- package/dist/callbacks/manager.cjs.map +1 -1
- package/dist/callbacks/manager.js.map +1 -1
- package/dist/example_selectors/conditional.cjs.map +1 -1
- package/dist/example_selectors/conditional.js.map +1 -1
- package/dist/example_selectors/length_based.cjs.map +1 -1
- package/dist/example_selectors/length_based.js.map +1 -1
- package/dist/example_selectors/semantic_similarity.cjs.map +1 -1
- package/dist/example_selectors/semantic_similarity.js.map +1 -1
- package/dist/indexing/base.cjs.map +1 -1
- package/dist/indexing/base.js.map +1 -1
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.cjs.map +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/language_models/base.js.map +1 -1
- package/dist/language_models/chat_models.cjs.map +1 -1
- package/dist/language_models/chat_models.js.map +1 -1
- package/dist/language_models/compat.cjs.map +1 -1
- package/dist/language_models/compat.js.map +1 -1
- package/dist/language_models/llms.cjs.map +1 -1
- package/dist/language_models/llms.js.map +1 -1
- package/dist/language_models/stream.cjs.map +1 -1
- package/dist/language_models/stream.js.map +1 -1
- package/dist/load/index.cjs.map +1 -1
- package/dist/load/index.js.map +1 -1
- package/dist/memory.cjs.map +1 -1
- package/dist/memory.js.map +1 -1
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/block_translators/anthropic.cjs.map +1 -1
- package/dist/messages/block_translators/anthropic.js.map +1 -1
- package/dist/messages/block_translators/bedrock_converse.cjs.map +1 -1
- package/dist/messages/block_translators/bedrock_converse.js.map +1 -1
- package/dist/messages/block_translators/google.cjs.map +1 -1
- package/dist/messages/block_translators/google.js.map +1 -1
- package/dist/messages/block_translators/google_vertexai.cjs.map +1 -1
- package/dist/messages/block_translators/google_vertexai.js.map +1 -1
- package/dist/messages/block_translators/openai.cjs.map +1 -1
- package/dist/messages/block_translators/openai.js.map +1 -1
- package/dist/messages/transformers.cjs.map +1 -1
- package/dist/messages/transformers.js.map +1 -1
- package/dist/messages/utils.cjs.map +1 -1
- package/dist/messages/utils.js.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.cjs.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.js.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js.map +1 -1
- package/dist/output_parsers/structured.cjs.map +1 -1
- package/dist/output_parsers/structured.js.map +1 -1
- package/dist/output_parsers/xml.cjs.map +1 -1
- package/dist/output_parsers/xml.js.map +1 -1
- package/dist/prompts/base.cjs.map +1 -1
- package/dist/prompts/base.js.map +1 -1
- package/dist/prompts/chat.cjs.map +1 -1
- package/dist/prompts/chat.js.map +1 -1
- package/dist/prompts/few_shot.cjs.map +1 -1
- package/dist/prompts/few_shot.js.map +1 -1
- package/dist/prompts/image.cjs.map +1 -1
- package/dist/prompts/image.js.map +1 -1
- package/dist/prompts/prompt.cjs.map +1 -1
- package/dist/prompts/prompt.js.map +1 -1
- package/dist/prompts/string.cjs.map +1 -1
- package/dist/prompts/string.js.map +1 -1
- package/dist/prompts/template.cjs.map +1 -1
- package/dist/prompts/template.js.map +1 -1
- package/dist/retrievers/index.cjs.map +1 -1
- package/dist/retrievers/index.js.map +1 -1
- package/dist/runnables/base.cjs.map +1 -1
- package/dist/runnables/base.js.map +1 -1
- package/dist/runnables/branch.cjs.map +1 -1
- package/dist/runnables/branch.js.map +1 -1
- package/dist/runnables/graph.cjs.map +1 -1
- package/dist/runnables/graph.js.map +1 -1
- package/dist/runnables/graph_mermaid.cjs.map +1 -1
- package/dist/runnables/graph_mermaid.js.map +1 -1
- package/dist/runnables/history.cjs.map +1 -1
- package/dist/runnables/history.js.map +1 -1
- package/dist/runnables/router.cjs.map +1 -1
- package/dist/runnables/router.js.map +1 -1
- package/dist/runnables/utils.cjs.map +1 -1
- package/dist/runnables/utils.js.map +1 -1
- package/dist/singletons/async_local_storage/context.cjs.map +1 -1
- package/dist/singletons/async_local_storage/context.js.map +1 -1
- package/dist/singletons/async_local_storage/index.cjs.map +1 -1
- package/dist/singletons/async_local_storage/index.js.map +1 -1
- package/dist/testing/fake_model_builder.cjs.map +1 -1
- package/dist/testing/fake_model_builder.js.map +1 -1
- package/dist/testing/matchers.cjs.map +1 -1
- package/dist/testing/matchers.js.map +1 -1
- package/dist/tracers/console.cjs.map +1 -1
- package/dist/tracers/console.js.map +1 -1
- package/dist/tracers/event_stream.cjs.map +1 -1
- package/dist/tracers/event_stream.js.map +1 -1
- package/dist/tracers/tracer_langchain.cjs.map +1 -1
- package/dist/tracers/tracer_langchain.js.map +1 -1
- package/dist/utils/callbacks.cjs.map +1 -1
- package/dist/utils/callbacks.js.map +1 -1
- package/dist/utils/context.cjs.map +1 -1
- package/dist/utils/context.js.map +1 -1
- package/dist/utils/env.cjs.map +1 -1
- package/dist/utils/env.js.map +1 -1
- package/dist/utils/fast-json-patch/src/core.cjs.map +1 -1
- package/dist/utils/fast-json-patch/src/core.js.map +1 -1
- package/dist/utils/json_schema.cjs.map +1 -1
- package/dist/utils/json_schema.js.map +1 -1
- package/dist/utils/sax-js/sax.cjs.map +1 -1
- package/dist/utils/sax-js/sax.js.map +1 -1
- package/dist/utils/ssrf.cjs.map +1 -1
- package/dist/utils/ssrf.js.map +1 -1
- package/dist/utils/stream.d.cts +2 -2
- package/dist/utils/stream.d.cts.map +1 -1
- package/dist/utils/stream.d.ts +2 -2
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/testing/chat_models.cjs.map +1 -1
- package/dist/utils/testing/chat_models.js.map +1 -1
- package/dist/utils/testing/embeddings.cjs.map +1 -1
- package/dist/utils/testing/embeddings.js.map +1 -1
- package/dist/utils/testing/vectorstores.cjs.map +1 -1
- package/dist/utils/testing/vectorstores.js.map +1 -1
- package/dist/utils/types/zod.cjs.map +1 -1
- package/dist/utils/types/zod.js.map +1 -1
- package/dist/utils/uuid/v35.cjs.map +1 -1
- package/dist/utils/uuid/v35.js.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/nativeEnum.cjs.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/nativeEnum.js.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/pipeline.cjs.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/pipeline.js.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/set.cjs.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/set.js.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/string.cjs +26 -0
- package/dist/utils/zod-to-json-schema/parsers/string.cjs.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/string.js +26 -0
- package/dist/utils/zod-to-json-schema/parsers/string.js.map +1 -1
- package/dist/vectorstores.cjs.map +1 -1
- package/dist/vectorstores.js.map +1 -1
- package/document_loaders/base.cjs +1 -0
- package/document_loaders/base.d.cts +1 -0
- package/document_loaders/base.d.ts +1 -0
- package/document_loaders/base.js +1 -0
- package/document_loaders/langsmith.cjs +1 -0
- package/document_loaders/langsmith.d.cts +1 -0
- package/document_loaders/langsmith.d.ts +1 -0
- package/document_loaders/langsmith.js +1 -0
- package/documents.cjs +1 -0
- package/documents.d.cts +1 -0
- package/documents.d.ts +1 -0
- package/documents.js +1 -0
- package/embeddings.cjs +1 -0
- package/embeddings.d.cts +1 -0
- package/embeddings.d.ts +1 -0
- package/embeddings.js +1 -0
- package/errors.cjs +1 -0
- package/errors.d.cts +1 -0
- package/errors.d.ts +1 -0
- package/errors.js +1 -0
- package/example_selectors.cjs +1 -0
- package/example_selectors.d.cts +1 -0
- package/example_selectors.d.ts +1 -0
- package/example_selectors.js +1 -0
- package/indexing.cjs +1 -0
- package/indexing.d.cts +1 -0
- package/indexing.d.ts +1 -0
- package/indexing.js +1 -0
- package/language_models/base.cjs +1 -0
- package/language_models/base.d.cts +1 -0
- package/language_models/base.d.ts +1 -0
- package/language_models/base.js +1 -0
- package/language_models/chat_models.cjs +1 -0
- package/language_models/chat_models.d.cts +1 -0
- package/language_models/chat_models.d.ts +1 -0
- package/language_models/chat_models.js +1 -0
- package/language_models/compat.cjs +1 -0
- package/language_models/compat.d.cts +1 -0
- package/language_models/compat.d.ts +1 -0
- package/language_models/compat.js +1 -0
- package/language_models/event.cjs +1 -0
- package/language_models/event.d.cts +1 -0
- package/language_models/event.d.ts +1 -0
- package/language_models/event.js +1 -0
- package/language_models/llms.cjs +1 -0
- package/language_models/llms.d.cts +1 -0
- package/language_models/llms.d.ts +1 -0
- package/language_models/llms.js +1 -0
- package/language_models/profile.cjs +1 -0
- package/language_models/profile.d.cts +1 -0
- package/language_models/profile.d.ts +1 -0
- package/language_models/profile.js +1 -0
- package/language_models/stream.cjs +1 -0
- package/language_models/stream.d.cts +1 -0
- package/language_models/stream.d.ts +1 -0
- package/language_models/stream.js +1 -0
- package/language_models/structured_output.cjs +1 -0
- package/language_models/structured_output.d.cts +1 -0
- package/language_models/structured_output.d.ts +1 -0
- package/language_models/structured_output.js +1 -0
- package/load/serializable.cjs +1 -0
- package/load/serializable.d.cts +1 -0
- package/load/serializable.d.ts +1 -0
- package/load/serializable.js +1 -0
- package/load.cjs +1 -0
- package/load.d.cts +1 -0
- package/load.d.ts +1 -0
- package/load.js +1 -0
- package/memory.cjs +1 -0
- package/memory.d.cts +1 -0
- package/memory.d.ts +1 -0
- package/memory.js +1 -0
- package/messages/tool.cjs +1 -0
- package/messages/tool.d.cts +1 -0
- package/messages/tool.d.ts +1 -0
- package/messages/tool.js +1 -0
- package/messages.cjs +1 -0
- package/messages.d.cts +1 -0
- package/messages.d.ts +1 -0
- package/messages.js +1 -0
- package/output_parsers/openai_functions.cjs +1 -0
- package/output_parsers/openai_functions.d.cts +1 -0
- package/output_parsers/openai_functions.d.ts +1 -0
- package/output_parsers/openai_functions.js +1 -0
- package/output_parsers/openai_tools.cjs +1 -0
- package/output_parsers/openai_tools.d.cts +1 -0
- package/output_parsers/openai_tools.d.ts +1 -0
- package/output_parsers/openai_tools.js +1 -0
- package/output_parsers.cjs +1 -0
- package/output_parsers.d.cts +1 -0
- package/output_parsers.d.ts +1 -0
- package/output_parsers.js +1 -0
- package/outputs.cjs +1 -0
- package/outputs.d.cts +1 -0
- package/outputs.d.ts +1 -0
- package/outputs.js +1 -0
- package/package.json +5 -5
- package/prompt_values.cjs +1 -0
- package/prompt_values.d.cts +1 -0
- package/prompt_values.d.ts +1 -0
- package/prompt_values.js +1 -0
- package/prompts.cjs +1 -0
- package/prompts.d.cts +1 -0
- package/prompts.d.ts +1 -0
- package/prompts.js +1 -0
- package/retrievers/document_compressors.cjs +1 -0
- package/retrievers/document_compressors.d.cts +1 -0
- package/retrievers/document_compressors.d.ts +1 -0
- package/retrievers/document_compressors.js +1 -0
- package/retrievers.cjs +1 -0
- package/retrievers.d.cts +1 -0
- package/retrievers.d.ts +1 -0
- package/retrievers.js +1 -0
- package/runnables/graph.cjs +1 -0
- package/runnables/graph.d.cts +1 -0
- package/runnables/graph.d.ts +1 -0
- package/runnables/graph.js +1 -0
- package/runnables.cjs +1 -0
- package/runnables.d.cts +1 -0
- package/runnables.d.ts +1 -0
- package/runnables.js +1 -0
- package/singletons.cjs +1 -0
- package/singletons.d.cts +1 -0
- package/singletons.d.ts +1 -0
- package/singletons.js +1 -0
- package/stores.cjs +1 -0
- package/stores.d.cts +1 -0
- package/stores.d.ts +1 -0
- package/stores.js +1 -0
- package/structured_query.cjs +1 -0
- package/structured_query.d.cts +1 -0
- package/structured_query.d.ts +1 -0
- package/structured_query.js +1 -0
- package/tools.cjs +1 -0
- package/tools.d.cts +1 -0
- package/tools.d.ts +1 -0
- package/tools.js +1 -0
- package/tracers/base.cjs +1 -0
- package/tracers/base.d.cts +1 -0
- package/tracers/base.d.ts +1 -0
- package/tracers/base.js +1 -0
- package/tracers/console.cjs +1 -0
- package/tracers/console.d.cts +1 -0
- package/tracers/console.d.ts +1 -0
- package/tracers/console.js +1 -0
- package/tracers/log_stream.cjs +1 -0
- package/tracers/log_stream.d.cts +1 -0
- package/tracers/log_stream.d.ts +1 -0
- package/tracers/log_stream.js +1 -0
- package/tracers/run_collector.cjs +1 -0
- package/tracers/run_collector.d.cts +1 -0
- package/tracers/run_collector.d.ts +1 -0
- package/tracers/run_collector.js +1 -0
- package/tracers/tracer_langchain.cjs +1 -0
- package/tracers/tracer_langchain.d.cts +1 -0
- package/tracers/tracer_langchain.d.ts +1 -0
- package/tracers/tracer_langchain.js +1 -0
- package/types/stream.cjs +1 -0
- package/types/stream.d.cts +1 -0
- package/types/stream.d.ts +1 -0
- package/types/stream.js +1 -0
- package/utils/async_caller.cjs +1 -0
- package/utils/async_caller.d.cts +1 -0
- package/utils/async_caller.d.ts +1 -0
- package/utils/async_caller.js +1 -0
- package/utils/chunk_array.cjs +1 -0
- package/utils/chunk_array.d.cts +1 -0
- package/utils/chunk_array.d.ts +1 -0
- package/utils/chunk_array.js +1 -0
- package/utils/context.cjs +1 -0
- package/utils/context.d.cts +1 -0
- package/utils/context.d.ts +1 -0
- package/utils/context.js +1 -0
- package/utils/env.cjs +1 -0
- package/utils/env.d.cts +1 -0
- package/utils/env.d.ts +1 -0
- package/utils/env.js +1 -0
- package/utils/event_source_parse.cjs +1 -0
- package/utils/event_source_parse.d.cts +1 -0
- package/utils/event_source_parse.d.ts +1 -0
- package/utils/event_source_parse.js +1 -0
- package/utils/format.cjs +1 -0
- package/utils/format.d.cts +1 -0
- package/utils/format.d.ts +1 -0
- package/utils/format.js +1 -0
- package/utils/function_calling.cjs +1 -0
- package/utils/function_calling.d.cts +1 -0
- package/utils/function_calling.d.ts +1 -0
- package/utils/function_calling.js +1 -0
- package/utils/hash.cjs +1 -0
- package/utils/hash.d.cts +1 -0
- package/utils/hash.d.ts +1 -0
- package/utils/hash.js +1 -0
- package/utils/json_patch.cjs +1 -0
- package/utils/json_patch.d.cts +1 -0
- package/utils/json_patch.d.ts +1 -0
- package/utils/json_patch.js +1 -0
- package/utils/json_schema.cjs +1 -0
- package/utils/json_schema.d.cts +1 -0
- package/utils/json_schema.d.ts +1 -0
- package/utils/json_schema.js +1 -0
- package/utils/math.cjs +1 -0
- package/utils/math.d.cts +1 -0
- package/utils/math.d.ts +1 -0
- package/utils/math.js +1 -0
- package/utils/ssrf.cjs +1 -0
- package/utils/ssrf.d.cts +1 -0
- package/utils/ssrf.d.ts +1 -0
- package/utils/ssrf.js +1 -0
- package/utils/standard_schema.cjs +1 -0
- package/utils/standard_schema.d.cts +1 -0
- package/utils/standard_schema.d.ts +1 -0
- package/utils/standard_schema.js +1 -0
- package/utils/stream.cjs +1 -0
- package/utils/stream.d.cts +1 -0
- package/utils/stream.d.ts +1 -0
- package/utils/stream.js +1 -0
- package/utils/testing.cjs +1 -0
- package/utils/testing.d.cts +1 -0
- package/utils/testing.d.ts +1 -0
- package/utils/testing.js +1 -0
- package/utils/tiktoken.cjs +1 -0
- package/utils/tiktoken.d.cts +1 -0
- package/utils/tiktoken.d.ts +1 -0
- package/utils/tiktoken.js +1 -0
- package/utils/types.cjs +1 -0
- package/utils/types.d.cts +1 -0
- package/utils/types.d.ts +1 -0
- package/utils/types.js +1 -0
- package/utils/uuid.cjs +1 -0
- package/utils/uuid.d.cts +1 -0
- package/utils/uuid.d.ts +1 -0
- package/utils/uuid.js +1 -0
- package/vectorstores.cjs +1 -0
- package/vectorstores.d.cts +1 -0
- package/vectorstores.d.ts +1 -0
- package/vectorstores.js +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"branch.cjs","names":["Runnable","_coerceToRunnable","patchConfig","getCallbackManagerForConfig","_coerceToDict","concat"],"sources":["../../src/runnables/branch.ts"],"sourcesContent":["import {\n Runnable,\n RunnableLike,\n _coerceToDict,\n _coerceToRunnable,\n} from \"./base.js\";\nimport {\n RunnableConfig,\n getCallbackManagerForConfig,\n patchConfig,\n} from \"./config.js\";\nimport { CallbackManagerForChainRun } from \"../callbacks/manager.js\";\nimport { concat } from \"../utils/stream.js\";\n\n/**\n * Type for a branch in the RunnableBranch. It consists of a condition\n * runnable and a branch runnable. The condition runnable is used to\n * determine whether the branch should be executed, and the branch runnable\n * is executed if the condition is true.\n */\nexport type Branch<RunInput, RunOutput> = [\n Runnable<RunInput, boolean>,\n Runnable<RunInput, RunOutput>,\n];\n\nexport type BranchLike<RunInput, RunOutput> = [\n RunnableLike<RunInput, boolean>,\n RunnableLike<RunInput, RunOutput>,\n];\n\n/**\n * Class that represents a runnable branch. The RunnableBranch is\n * initialized with an array of branches and a default branch. When invoked,\n * it evaluates the condition of each branch in order and executes the\n * corresponding branch if the condition is true. If none of the conditions\n * are true, it executes the default branch.\n * @example\n * ```typescript\n * const branch = RunnableBranch.from([\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"anthropic\"),\n * anthropicChain,\n * ],\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"langchain\"),\n * langChainChain,\n * ],\n * generalChain,\n * ]);\n *\n * const fullChain = RunnableSequence.from([\n * {\n * topic: classificationChain,\n * question: (input: { question: string }) => input.question,\n * },\n * branch,\n * ]);\n *\n * const result = await fullChain.invoke({\n * question: \"how do I use LangChain?\",\n * });\n * ```\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<\n RunInput,\n RunOutput\n> {\n static lc_name() {\n return \"RunnableBranch\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n default: Runnable<RunInput, RunOutput>;\n\n branches: Branch<RunInput, RunOutput>[];\n\n constructor(fields: {\n branches: Branch<RunInput, RunOutput>[];\n default: Runnable<RunInput, RunOutput>;\n }) {\n super(fields);\n this.branches = fields.branches;\n this.default = fields.default;\n }\n\n /**\n * Convenience method for instantiating a RunnableBranch from\n * RunnableLikes (objects, functions, or Runnables).\n *\n * Each item in the input except for the last one should be a\n * tuple with two items. The first is a \"condition\" RunnableLike that\n * returns \"true\" if the second RunnableLike in the tuple should run.\n *\n * The final item in the input should be a RunnableLike that acts as a\n * default branch if no other branches match.\n *\n * @example\n * ```ts\n * import { RunnableBranch } from \"@langchain/core/runnables\";\n *\n * const branch = RunnableBranch.from([\n * [(x: number) => x > 0, (x: number) => x + 1],\n * [(x: number) => x < 0, (x: number) => x - 1],\n * (x: number) => x\n * ]);\n * ```\n * @param branches An array where the every item except the last is a tuple of [condition, runnable]\n * pairs. The last item is a default runnable which is invoked if no other condition matches.\n * @returns A new RunnableBranch.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static from<RunInput = any, RunOutput = any>(\n branches: [\n ...BranchLike<RunInput, RunOutput>[],\n RunnableLike<RunInput, RunOutput>,\n ]\n ) {\n if (branches.length < 1) {\n throw new Error(\"RunnableBranch requires at least one branch\");\n }\n const branchLikes = branches.slice(0, -1) as BranchLike<\n RunInput,\n RunOutput\n >[];\n const coercedBranches: Branch<RunInput, RunOutput>[] = branchLikes.map(\n ([condition, runnable]) => [\n _coerceToRunnable(condition),\n _coerceToRunnable(runnable),\n ]\n );\n const defaultBranch = _coerceToRunnable(\n branches[branches.length - 1] as RunnableLike<RunInput, RunOutput>\n );\n return new this({\n branches: coercedBranches,\n default: defaultBranch,\n });\n }\n\n async _invoke(\n input: RunInput,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<RunOutput> {\n let result;\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n result = await branchRunnable.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n break;\n }\n }\n if (!result) {\n result = await this.default.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n }\n return result;\n }\n\n async invoke(\n input: RunInput,\n config: RunnableConfig = {}\n ): Promise<RunOutput> {\n return this._callWithConfig(this._invoke, input, config);\n }\n\n async *_streamIterator(input: RunInput, config?: Partial<RunnableConfig>) {\n const callbackManager_ = await getCallbackManagerForConfig(config);\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n _coerceToDict(input, \"input\"),\n config?.runId,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let finalOutput;\n let finalOutputSupported = true;\n let stream;\n try {\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n stream = await branchRunnable.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n break;\n }\n }\n if (stream === undefined) {\n stream = await this.default.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk as RunOutput);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n await runManager?.handleChainEnd(finalOutput ?? {});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,iBAAb,cAAqEA,aAAAA,SAGnE;CACA,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA;CAEA,YAAY,QAGT;AACD,QAAM,OAAO;AACb,OAAK,WAAW,OAAO;AACvB,OAAK,UAAU,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BxB,OAAO,KACL,UAIA;AACA,MAAI,SAAS,SAAS,EACpB,OAAM,IAAI,MAAM,8CAA8C;EAMhE,MAAM,kBAJc,SAAS,MAAM,GAAG,GAAG,CAI0B,KAChE,CAAC,WAAW,cAAc,CACzBC,aAAAA,kBAAkB,UAAU,EAC5BA,aAAAA,kBAAkB,SAAS,CAC5B,CACF;EACD,M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EACtD,CAAC,CACH,EACmB;AAClB,cAAS,MAAM,eAAe,OAC5B,OACAA,eAAAA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,UAAU,IAAI,IAAI,EACnD,CAAC,CACH;AACD,gBAAW,MAAM,SAAS,QAAQ;AAChC,YAAM;AACN,UAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;UAEd,KAAI;AACF,qBAAcG,qBAAAA,OAAO,aAAa,MAAM;cAClC;AACN,qBAAc,KAAA;AACd,8BAAuB;;;AAK/B;;;AAGJ,OAAI,WAAW,KAAA,GAAW;AACxB,aAAS,MAAM,KAAK,QAAQ,OAC1B,OACAH,eAAAA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,iBAAiB,EAClD,CAAC,CACH;AACD,eAAW,MAAM,SAAS,QAAQ;AAChC,WAAM;AACN,SAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;SAEd,KAAI;AACF,oBAAcG,qBAAAA,OAAO,aAAa,MAAmB;aAC/C;AACN,oBAAc,KAAA;AACd,6BAAuB;;;;WAM1B,GAAG;AACV,SAAM,YAAY,iBAAiB,EAAE;AACrC,SAAM;;AAER,QAAM,YAAY,eAAe,eAAe,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"branch.cjs","names":["Runnable","_coerceToRunnable","patchConfig","getCallbackManagerForConfig","_coerceToDict","concat"],"sources":["../../src/runnables/branch.ts"],"sourcesContent":["import {\n Runnable,\n RunnableLike,\n _coerceToDict,\n _coerceToRunnable,\n} from \"./base.js\";\nimport {\n RunnableConfig,\n getCallbackManagerForConfig,\n patchConfig,\n} from \"./config.js\";\nimport { CallbackManagerForChainRun } from \"../callbacks/manager.js\";\nimport { concat } from \"../utils/stream.js\";\n\n/**\n * Type for a branch in the RunnableBranch. It consists of a condition\n * runnable and a branch runnable. The condition runnable is used to\n * determine whether the branch should be executed, and the branch runnable\n * is executed if the condition is true.\n */\nexport type Branch<RunInput, RunOutput> = [\n Runnable<RunInput, boolean>,\n Runnable<RunInput, RunOutput>,\n];\n\nexport type BranchLike<RunInput, RunOutput> = [\n RunnableLike<RunInput, boolean>,\n RunnableLike<RunInput, RunOutput>,\n];\n\n/**\n * Class that represents a runnable branch. The RunnableBranch is\n * initialized with an array of branches and a default branch. When invoked,\n * it evaluates the condition of each branch in order and executes the\n * corresponding branch if the condition is true. If none of the conditions\n * are true, it executes the default branch.\n * @example\n * ```typescript\n * const branch = RunnableBranch.from([\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"anthropic\"),\n * anthropicChain,\n * ],\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"langchain\"),\n * langChainChain,\n * ],\n * generalChain,\n * ]);\n *\n * const fullChain = RunnableSequence.from([\n * {\n * topic: classificationChain,\n * question: (input: { question: string }) => input.question,\n * },\n * branch,\n * ]);\n *\n * const result = await fullChain.invoke({\n * question: \"how do I use LangChain?\",\n * });\n * ```\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<\n RunInput,\n RunOutput\n> {\n static lc_name() {\n return \"RunnableBranch\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n default: Runnable<RunInput, RunOutput>;\n\n branches: Branch<RunInput, RunOutput>[];\n\n constructor(fields: {\n branches: Branch<RunInput, RunOutput>[];\n default: Runnable<RunInput, RunOutput>;\n }) {\n super(fields);\n this.branches = fields.branches;\n this.default = fields.default;\n }\n\n /**\n * Convenience method for instantiating a RunnableBranch from\n * RunnableLikes (objects, functions, or Runnables).\n *\n * Each item in the input except for the last one should be a\n * tuple with two items. The first is a \"condition\" RunnableLike that\n * returns \"true\" if the second RunnableLike in the tuple should run.\n *\n * The final item in the input should be a RunnableLike that acts as a\n * default branch if no other branches match.\n *\n * @example\n * ```ts\n * import { RunnableBranch } from \"@langchain/core/runnables\";\n *\n * const branch = RunnableBranch.from([\n * [(x: number) => x > 0, (x: number) => x + 1],\n * [(x: number) => x < 0, (x: number) => x - 1],\n * (x: number) => x\n * ]);\n * ```\n * @param branches An array where the every item except the last is a tuple of [condition, runnable]\n * pairs. The last item is a default runnable which is invoked if no other condition matches.\n * @returns A new RunnableBranch.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static from<RunInput = any, RunOutput = any>(\n branches: [\n ...BranchLike<RunInput, RunOutput>[],\n RunnableLike<RunInput, RunOutput>,\n ]\n ) {\n if (branches.length < 1) {\n throw new Error(\"RunnableBranch requires at least one branch\");\n }\n const branchLikes = branches.slice(0, -1) as BranchLike<\n RunInput,\n RunOutput\n >[];\n const coercedBranches: Branch<RunInput, RunOutput>[] = branchLikes.map(\n ([condition, runnable]) => [\n _coerceToRunnable(condition),\n _coerceToRunnable(runnable),\n ]\n );\n const defaultBranch = _coerceToRunnable(\n branches[branches.length - 1] as RunnableLike<RunInput, RunOutput>\n );\n return new this({\n branches: coercedBranches,\n default: defaultBranch,\n });\n }\n\n async _invoke(\n input: RunInput,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<RunOutput> {\n let result;\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n result = await branchRunnable.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n break;\n }\n }\n if (!result) {\n result = await this.default.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n }\n return result;\n }\n\n async invoke(\n input: RunInput,\n config: RunnableConfig = {}\n ): Promise<RunOutput> {\n return this._callWithConfig(this._invoke, input, config);\n }\n\n async *_streamIterator(input: RunInput, config?: Partial<RunnableConfig>) {\n const callbackManager_ = await getCallbackManagerForConfig(config);\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n _coerceToDict(input, \"input\"),\n config?.runId,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let finalOutput;\n let finalOutputSupported = true;\n let stream;\n try {\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n stream = await branchRunnable.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n break;\n }\n }\n if (stream === undefined) {\n stream = await this.default.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk as RunOutput);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n await runManager?.handleChainEnd(finalOutput ?? {});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,iBAAb,cAAqEA,aAAAA,SAGnE;CACA,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA;CAEA,YAAY,QAGT;AACD,QAAM,OAAO;AACb,OAAK,WAAW,OAAO;AACvB,OAAK,UAAU,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BxB,OAAO,KACL,UAIA;AACA,MAAI,SAAS,SAAS,EACpB,O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I,KAAK,SAAS,QAAQ,KAAK,GAAG;IAChD,MAAM,CAAC,WAAW,kBAAkB,KAAK,SAAS;AAOlD,QAAI,MANyB,UAAU,OACrC,OACAF,eAAAA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,aAAa,IAAI,IAAI,EACtD,CAAC,CACH,EACmB;AAClB,cAAS,MAAM,eAAe,OAC5B,OACAA,eAAAA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,UAAU,IAAI,IAAI,EACnD,CAAC,CACH;AACD,gBAAW,MAAM,SAAS,QAAQ;AAChC,YAAM;AACN,UAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;UAEd,KAAI;AACF,qBAAcG,qBAAAA,OAAO,aAAa,MAAM;cAClC;AACN,qBAAc,KAAA;AACd,8BAAuB;;;AAK/B;;;AAGJ,OAAI,WAAW,KAAA,GAAW;AACxB,aAAS,MAAM,KAAK,QAAQ,OAC1B,OACAH,eAAAA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,iBAAiB,EAClD,CAAC,CACH;AACD,eAAW,MAAM,SAAS,QAAQ;AAChC,WAAM;AACN,SAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;SAEd,KAAI;AACF,oBAAcG,qBAAAA,OAAO,aAAa,MAAmB;aAC/C;AACN,oBAAc,KAAA;AACd,6BAAuB;;;;WAM1B,GAAG;AACV,SAAM,YAAY,iBAAiB,EAAE;AACrC,SAAM;;AAER,QAAM,YAAY,eAAe,eAAe,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"branch.js","names":[],"sources":["../../src/runnables/branch.ts"],"sourcesContent":["import {\n Runnable,\n RunnableLike,\n _coerceToDict,\n _coerceToRunnable,\n} from \"./base.js\";\nimport {\n RunnableConfig,\n getCallbackManagerForConfig,\n patchConfig,\n} from \"./config.js\";\nimport { CallbackManagerForChainRun } from \"../callbacks/manager.js\";\nimport { concat } from \"../utils/stream.js\";\n\n/**\n * Type for a branch in the RunnableBranch. It consists of a condition\n * runnable and a branch runnable. The condition runnable is used to\n * determine whether the branch should be executed, and the branch runnable\n * is executed if the condition is true.\n */\nexport type Branch<RunInput, RunOutput> = [\n Runnable<RunInput, boolean>,\n Runnable<RunInput, RunOutput>,\n];\n\nexport type BranchLike<RunInput, RunOutput> = [\n RunnableLike<RunInput, boolean>,\n RunnableLike<RunInput, RunOutput>,\n];\n\n/**\n * Class that represents a runnable branch. The RunnableBranch is\n * initialized with an array of branches and a default branch. When invoked,\n * it evaluates the condition of each branch in order and executes the\n * corresponding branch if the condition is true. If none of the conditions\n * are true, it executes the default branch.\n * @example\n * ```typescript\n * const branch = RunnableBranch.from([\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"anthropic\"),\n * anthropicChain,\n * ],\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"langchain\"),\n * langChainChain,\n * ],\n * generalChain,\n * ]);\n *\n * const fullChain = RunnableSequence.from([\n * {\n * topic: classificationChain,\n * question: (input: { question: string }) => input.question,\n * },\n * branch,\n * ]);\n *\n * const result = await fullChain.invoke({\n * question: \"how do I use LangChain?\",\n * });\n * ```\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<\n RunInput,\n RunOutput\n> {\n static lc_name() {\n return \"RunnableBranch\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n default: Runnable<RunInput, RunOutput>;\n\n branches: Branch<RunInput, RunOutput>[];\n\n constructor(fields: {\n branches: Branch<RunInput, RunOutput>[];\n default: Runnable<RunInput, RunOutput>;\n }) {\n super(fields);\n this.branches = fields.branches;\n this.default = fields.default;\n }\n\n /**\n * Convenience method for instantiating a RunnableBranch from\n * RunnableLikes (objects, functions, or Runnables).\n *\n * Each item in the input except for the last one should be a\n * tuple with two items. The first is a \"condition\" RunnableLike that\n * returns \"true\" if the second RunnableLike in the tuple should run.\n *\n * The final item in the input should be a RunnableLike that acts as a\n * default branch if no other branches match.\n *\n * @example\n * ```ts\n * import { RunnableBranch } from \"@langchain/core/runnables\";\n *\n * const branch = RunnableBranch.from([\n * [(x: number) => x > 0, (x: number) => x + 1],\n * [(x: number) => x < 0, (x: number) => x - 1],\n * (x: number) => x\n * ]);\n * ```\n * @param branches An array where the every item except the last is a tuple of [condition, runnable]\n * pairs. The last item is a default runnable which is invoked if no other condition matches.\n * @returns A new RunnableBranch.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static from<RunInput = any, RunOutput = any>(\n branches: [\n ...BranchLike<RunInput, RunOutput>[],\n RunnableLike<RunInput, RunOutput>,\n ]\n ) {\n if (branches.length < 1) {\n throw new Error(\"RunnableBranch requires at least one branch\");\n }\n const branchLikes = branches.slice(0, -1) as BranchLike<\n RunInput,\n RunOutput\n >[];\n const coercedBranches: Branch<RunInput, RunOutput>[] = branchLikes.map(\n ([condition, runnable]) => [\n _coerceToRunnable(condition),\n _coerceToRunnable(runnable),\n ]\n );\n const defaultBranch = _coerceToRunnable(\n branches[branches.length - 1] as RunnableLike<RunInput, RunOutput>\n );\n return new this({\n branches: coercedBranches,\n default: defaultBranch,\n });\n }\n\n async _invoke(\n input: RunInput,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<RunOutput> {\n let result;\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n result = await branchRunnable.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n break;\n }\n }\n if (!result) {\n result = await this.default.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n }\n return result;\n }\n\n async invoke(\n input: RunInput,\n config: RunnableConfig = {}\n ): Promise<RunOutput> {\n return this._callWithConfig(this._invoke, input, config);\n }\n\n async *_streamIterator(input: RunInput, config?: Partial<RunnableConfig>) {\n const callbackManager_ = await getCallbackManagerForConfig(config);\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n _coerceToDict(input, \"input\"),\n config?.runId,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let finalOutput;\n let finalOutputSupported = true;\n let stream;\n try {\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n stream = await branchRunnable.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n break;\n }\n }\n if (stream === undefined) {\n stream = await this.default.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk as RunOutput);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n await runManager?.handleChainEnd(finalOutput ?? {});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,iBAAb,cAAqE,SAGnE;CACA,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA;CAEA,YAAY,QAGT;AACD,QAAM,OAAO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,uBAAuB;EAC3B,IAAI;AACJ,MAAI;AACF,QAAK,IAAI,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK,GAAG;IAChD,MAAM,CAAC,WAAW,kBAAkB,KAAK,SAAS;AAOlD,QANuB,MAAM,UAAU,OACrC,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,aAAa,IAAI,IAAI,EACtD,CAAC,CACH,EACmB;AAClB,cAAS,MAAM,eAAe,OAC5B,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,UAAU,IAAI,IAAI,EACnD,CAAC,CACH;AACD,gBAAW,MAAM,SAAS,QAAQ;AAChC,YAAM;AACN,UAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;UAEd,KAAI;AACF,qBAAc,OAAO,aAAa,MAAM;cAClC;AACN,qBAAc,KAAA;AACd,8BAAuB;;;AAK/B;;;AAGJ,OAAI,WAAW,KAAA,GAAW;AACxB,aAAS,MAAM,KAAK,QAAQ,OAC1B,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,iBAAiB,EAClD,CAAC,CACH;AACD,eAAW,MAAM,SAAS,QAAQ;AAChC,WAAM;AACN,SAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;SAEd,KAAI;AACF,oBAAc,OAAO,aAAa,MAAmB;aAC/C;AACN,oBAAc,KAAA;AACd,6BAAuB;;;;WAM1B,GAAG;AACV,SAAM,YAAY,iBAAiB,EAAE;AACrC,SAAM;;AAER,QAAM,YAAY,eAAe,eAAe,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"branch.js","names":[],"sources":["../../src/runnables/branch.ts"],"sourcesContent":["import {\n Runnable,\n RunnableLike,\n _coerceToDict,\n _coerceToRunnable,\n} from \"./base.js\";\nimport {\n RunnableConfig,\n getCallbackManagerForConfig,\n patchConfig,\n} from \"./config.js\";\nimport { CallbackManagerForChainRun } from \"../callbacks/manager.js\";\nimport { concat } from \"../utils/stream.js\";\n\n/**\n * Type for a branch in the RunnableBranch. It consists of a condition\n * runnable and a branch runnable. The condition runnable is used to\n * determine whether the branch should be executed, and the branch runnable\n * is executed if the condition is true.\n */\nexport type Branch<RunInput, RunOutput> = [\n Runnable<RunInput, boolean>,\n Runnable<RunInput, RunOutput>,\n];\n\nexport type BranchLike<RunInput, RunOutput> = [\n RunnableLike<RunInput, boolean>,\n RunnableLike<RunInput, RunOutput>,\n];\n\n/**\n * Class that represents a runnable branch. The RunnableBranch is\n * initialized with an array of branches and a default branch. When invoked,\n * it evaluates the condition of each branch in order and executes the\n * corresponding branch if the condition is true. If none of the conditions\n * are true, it executes the default branch.\n * @example\n * ```typescript\n * const branch = RunnableBranch.from([\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"anthropic\"),\n * anthropicChain,\n * ],\n * [\n * (x: { topic: string; question: string }) =>\n * x.topic.toLowerCase().includes(\"langchain\"),\n * langChainChain,\n * ],\n * generalChain,\n * ]);\n *\n * const fullChain = RunnableSequence.from([\n * {\n * topic: classificationChain,\n * question: (input: { question: string }) => input.question,\n * },\n * branch,\n * ]);\n *\n * const result = await fullChain.invoke({\n * question: \"how do I use LangChain?\",\n * });\n * ```\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<\n RunInput,\n RunOutput\n> {\n static lc_name() {\n return \"RunnableBranch\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n default: Runnable<RunInput, RunOutput>;\n\n branches: Branch<RunInput, RunOutput>[];\n\n constructor(fields: {\n branches: Branch<RunInput, RunOutput>[];\n default: Runnable<RunInput, RunOutput>;\n }) {\n super(fields);\n this.branches = fields.branches;\n this.default = fields.default;\n }\n\n /**\n * Convenience method for instantiating a RunnableBranch from\n * RunnableLikes (objects, functions, or Runnables).\n *\n * Each item in the input except for the last one should be a\n * tuple with two items. The first is a \"condition\" RunnableLike that\n * returns \"true\" if the second RunnableLike in the tuple should run.\n *\n * The final item in the input should be a RunnableLike that acts as a\n * default branch if no other branches match.\n *\n * @example\n * ```ts\n * import { RunnableBranch } from \"@langchain/core/runnables\";\n *\n * const branch = RunnableBranch.from([\n * [(x: number) => x > 0, (x: number) => x + 1],\n * [(x: number) => x < 0, (x: number) => x - 1],\n * (x: number) => x\n * ]);\n * ```\n * @param branches An array where the every item except the last is a tuple of [condition, runnable]\n * pairs. The last item is a default runnable which is invoked if no other condition matches.\n * @returns A new RunnableBranch.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static from<RunInput = any, RunOutput = any>(\n branches: [\n ...BranchLike<RunInput, RunOutput>[],\n RunnableLike<RunInput, RunOutput>,\n ]\n ) {\n if (branches.length < 1) {\n throw new Error(\"RunnableBranch requires at least one branch\");\n }\n const branchLikes = branches.slice(0, -1) as BranchLike<\n RunInput,\n RunOutput\n >[];\n const coercedBranches: Branch<RunInput, RunOutput>[] = branchLikes.map(\n ([condition, runnable]) => [\n _coerceToRunnable(condition),\n _coerceToRunnable(runnable),\n ]\n );\n const defaultBranch = _coerceToRunnable(\n branches[branches.length - 1] as RunnableLike<RunInput, RunOutput>\n );\n return new this({\n branches: coercedBranches,\n default: defaultBranch,\n });\n }\n\n async _invoke(\n input: RunInput,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<RunOutput> {\n let result;\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n result = await branchRunnable.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n break;\n }\n }\n if (!result) {\n result = await this.default.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n }\n return result;\n }\n\n async invoke(\n input: RunInput,\n config: RunnableConfig = {}\n ): Promise<RunOutput> {\n return this._callWithConfig(this._invoke, input, config);\n }\n\n async *_streamIterator(input: RunInput, config?: Partial<RunnableConfig>) {\n const callbackManager_ = await getCallbackManagerForConfig(config);\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n _coerceToDict(input, \"input\"),\n config?.runId,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let finalOutput;\n let finalOutputSupported = true;\n let stream;\n try {\n for (let i = 0; i < this.branches.length; i += 1) {\n const [condition, branchRunnable] = this.branches[i];\n const conditionValue = await condition.invoke(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`condition:${i + 1}`),\n })\n );\n if (conditionValue) {\n stream = await branchRunnable.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(`branch:${i + 1}`),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n break;\n }\n }\n if (stream === undefined) {\n stream = await this.default.stream(\n input,\n patchConfig(config, {\n callbacks: runManager?.getChild(\"branch:default\"),\n })\n );\n for await (const chunk of stream) {\n yield chunk;\n if (finalOutputSupported) {\n if (finalOutput === undefined) {\n finalOutput = chunk;\n } else {\n try {\n finalOutput = concat(finalOutput, chunk as RunOutput);\n } catch {\n finalOutput = undefined;\n finalOutputSupported = false;\n }\n }\n }\n }\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n await runManager?.handleChainEnd(finalOutput ?? {});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,iBAAb,cAAqE,SAGnE;CACA,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA;CAEA,YAAY,QAGT;AACD,QAAM,OAA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I,uBAAuB;EAC3B,IAAI;AACJ,MAAI;AACF,QAAK,IAAI,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK,GAAG;IAChD,MAAM,CAAC,WAAW,kBAAkB,KAAK,SAAS;AAOlD,QAAI,MANyB,UAAU,OACrC,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,aAAa,IAAI,IAAI,EACtD,CAAC,CACH,EACmB;AAClB,cAAS,MAAM,eAAe,OAC5B,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,UAAU,IAAI,IAAI,EACnD,CAAC,CACH;AACD,gBAAW,MAAM,SAAS,QAAQ;AAChC,YAAM;AACN,UAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;UAEd,KAAI;AACF,qBAAc,OAAO,aAAa,MAAM;cAClC;AACN,qBAAc,KAAA;AACd,8BAAuB;;;AAK/B;;;AAGJ,OAAI,WAAW,KAAA,GAAW;AACxB,aAAS,MAAM,KAAK,QAAQ,OAC1B,OACA,YAAY,QAAQ,EAClB,WAAW,YAAY,SAAS,iBAAiB,EAClD,CAAC,CACH;AACD,eAAW,MAAM,SAAS,QAAQ;AAChC,WAAM;AACN,SAAI,qBACF,KAAI,gBAAgB,KAAA,EAClB,eAAc;SAEd,KAAI;AACF,oBAAc,OAAO,aAAa,MAAmB;aAC/C;AACN,oBAAc,KAAA;AACd,6BAAuB;;;;WAM1B,GAAG;AACV,SAAM,YAAY,iBAAiB,EAAE;AACrC,SAAM;;AAER,QAAM,YAAY,eAAe,eAAe,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.cjs","names":["isUuid","isRunnableInterface","toJsonSchema","uuidv4","drawMermaid","drawMermaidImage"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"../utils/uuid/index.js\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;;AAaA,SAAS,YACP,IACA,MACQ;AACR,KAAI,OAAO,KAAA,KAAa,CAACA,yBAAAA,SAAO,GAAG,CACjC,QAAO;UACEC,cAAAA,oBAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;AAC5B,YAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;SACD;AACN,SAAO,KAAK,SAAS;;KAGvB,QAAO,KAAK,QAAQ;;AAIxB,SAAS,aAAa,MAAY;AAEhC,KAAIA,cAAAA,oBAAoB,KAAK,KAAK,CAChC,QAAO;EACL,MAAM;EACN,MAAM;GACJ,IAAI,KAAK,KAAK;GACd,MAAM,KAAK,KAAK,S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AAM,UAAU;AAEjC,SAAOI,sBAAAA,YAAY,MAAM,OAAO,MAAM,OAAO;GAC3C,WAAW,WAAW;GACtB,UAAU,UAAU;GACpB;GACA;GACA;GACA;GACD,CAAC;;CAGJ,MAAM,eAAe,QAMH;AAEhB,SAAOC,sBAAAA,iBADe,KAAK,YAAY,OAAO,EACP,EACrC,iBAAiB,QAAQ,iBAC1B,CAAC;;;;;;;;;AASN,SAAS,WAAW,OAAc,UAAoB,EAAE,EAAoB;CAC1E,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,QAAQ,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,KAAK,SAAS,KAAK,OAAO,CAC9B;CAED,MAAM,QAAgB,EAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,CACrD,OAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK,KAAA;;;;;;;;AASzC,SAAS,UAAU,OAAc,UAAoB,EAAE,EAAoB;CACzE,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,QAAQ,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,KAAK,SAAS,KAAK,OAAO,CAC9B;CAED,MAAM,QAAgB,EAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,CACrD,OAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK,KAAA"}
|
|
1
|
+
{"version":3,"file":"graph.cjs","names":["isUuid","isRunnableInterface","toJsonSchema","uuidv4","drawMermaid","drawMermaidImage"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"../utils/uuid/index.js\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;;AAaA,SAAS,YACP,IACA,MACQ;AACR,KAAI,OAAO,KAAA,KAAa,CAACA,yBAAAA,SAAO,GAAG,CACjC,QAAO;UACEC,cAAAA,oBAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;AAC5B,YAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;SACD;AACN,SAAO,KAAK,SAAS;;KAGvB,QAAO,KAAK,QAAQ;;AAIxB,SAAS,aAAa,MAAY;AAEhC,KAAIA,cAAAA,oBAAoB,KAAK,KAAK,C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runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"../utils/uuid/index.js\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;AAaA,SAAS,YACP,IACA,MACQ;AACR,KAAI,OAAO,KAAA,KAAa,CAACA,SAAO,GAAG,CACjC,QAAO;UACE,oBAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;AAC5B,YAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;SACD;AACN,SAAO,KAAK,SAAS;;KAGvB,QAAO,KAAK,QAAQ;;AAIxB,SAAS,aAAa,MAAY;A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"../utils/uuid/index.js\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;AAaA,SAAS,YACP,IACA,MACQ;AACR,KAAI,OAAO,KAAA,KAAa,CAACA,SAAO,GAAG,CACjC,QAAO;UACE,oBAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;AAC5B,YAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;SACD;AACN,SAAO,KAAK,SAAS;;KAGvB,QAAO,KAAK,QAAQ;;AAIxB,SAAS,aAAa,MAAY;AAEhC,KAAI,oBAAoB,KAAK,KAAK,CAChC,QAAO;EACL,M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EAEnC,MAAM,WAAW,MAAM,UAAU;AAEjC,SAAO,YAAY,MAAM,OAAO,MAAM,OAAO;GAC3C,WAAW,WAAW;GACtB,UAAU,UAAU;GACpB;GACA;GACA;GACA;GACD,CAAC;;CAGJ,MAAM,eAAe,QAMH;AAEhB,SAAO,iBADe,KAAK,YAAY,OACF,EAAE,EACrC,iBAAiB,QAAQ,iBAC1B,CAAC;;;;;;;;;AASN,SAAS,WAAW,OAAc,UAAoB,EAAE,EAAoB;CAC1E,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,QAAQ,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,KAAK,SAAS,KAAK,OAAO,CAC9B;CAED,MAAM,QAAgB,EAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,CACrD,OAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK,KAAA;;;;;;;;AASzC,SAAS,UAAU,OAAc,UAAoB,EAAE,EAAoB;CACzE,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,QAAQ,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,KAAK,SAAS,KAAK,OAAO,CAC9B;CAED,MAAM,QAAgB,EAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,CACrD,OAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK,KAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph_mermaid.cjs","names":["toBase64Url"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\nimport { toBase64Url } from \"./utils.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n // sort prefixes by path length for correct nesting\n function sortPrefixesByDepth(prefixes: string[]): string[] {\n return [...prefixes].sort((a, b) => {\n return a.split(\":\").length - b.split(\":\").length;\n });\n }\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n\n if (seenSubgraphs.has(prefix)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' at '${prefix} -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(prefix);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n // all nested prefixes for this level, sorted by depth\n const nestedPrefixes = sortPrefixesByDepth(\n Object.keys(edgeGroups).filter(\n (nestedPrefix) =>\n nestedPrefix.startsWith(`${prefix}:`) &&\n nestedPrefix !== prefix &&\n nestedPrefix.split(\":\").length === prefix.split(\":\").length + 1\n )\n );\n\n for (const nestedPrefix of nestedPrefixes) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining top-level subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n const mermaidSyntaxEncoded = toBase64Url(mermaidSyntax);\n\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAGA,SAAS,iBAAiB,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;;AAGlD,MAAM,yBAAyB;CAAC;CAAK;CAAK;CAAI;AAE9C,SAAS,4BACP,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,WAAW,CACzD,WAAU,cAAc,UAAU,GAAG,MAAM;AAE7C,QAAO;;;;;AAMT,SAAgB,YACd,OACA,OACA,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,MAChB,UAAU,EAAE;CAEhB,IAAI,eAAe,aACf,qCAAqC,WAAW,uBAChD;AACJ,KAAI,YAAY;EAEd,MAAM,oBAAoB;EAC1B,MAAM,aAAqC,GACxC,oBAAoB,YACtB;AACD,MAAI,cAAc,KAAA,EAChB,YAAW,aAAa;AAE1B,MAAI,aAAa,KAAA,EACf,YAAW,YAAY;AAIzB,OAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,MAAM,EAAE;GAC/C,MAAM,WAAW,KAAK,KAAK,MAAM,IAAI,CAAC,KAAK,IAAI;GAO/C,IAAI,aANU,uBAAuB,MAClC,SAAS,SAAS,WAAW,KAAK,IAAI,SAAS,SAAS,KAAK,CAC/D,GACG,MAAM,SAAS,QACf;AAGJ,OAAI,OAAO,KAAK,KAAK,YAAY,EAAE,CAAC,CAAC,OACnC,eAAc,mBAAmB,OAAO,QAAQ,KAAK,YAAY,EAAE,CAAC,CACjE,KAAK,CAAC,GAAG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rB,CAAC,KAAK,KAAK,CACb;AAGH,QADgB,MAAM,IAAI,MAAM"}
|
|
1
|
+
{"version":3,"file":"graph_mermaid.cjs","names":["toBase64Url"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\nimport { toBase64Url } from \"./utils.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n // sort prefixes by path length for correct nesting\n function sortPrefixesByDepth(prefixes: string[]): string[] {\n return [...prefixes].sort((a, b) => {\n return a.split(\":\").length - b.split(\":\").length;\n });\n }\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n\n if (seenSubgraphs.has(prefix)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' at '${prefix} -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(prefix);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n // all nested prefixes for this level, sorted by depth\n const nestedPrefixes = sortPrefixesByDepth(\n Object.keys(edgeGroups).filter(\n (nestedPrefix) =>\n nestedPrefix.startsWith(`${prefix}:`) &&\n nestedPrefix !== prefix &&\n nestedPrefix.split(\":\").length === prefix.split(\":\").length + 1\n )\n );\n\n for (const nestedPrefix of nestedPrefixes) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining top-level subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n const mermaidSyntaxEncoded = toBase64Url(mermaidSyntax);\n\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAGA,SAAS,iBAAiB,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;;AAGlD,MAAM,yBAAyB;CAAC;CAAK;CAAK;CAAI;AAE9C,SAAS,4BACP,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,WAAW,CACzD,WAAU,cAAc,UAAU,GAAG,MAAM;AAE7C,QAAO;;;;;AAMT,SAAgB,YACd,OACA,OACA,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,MAChB,UAAU,EAAE;CAEhB,IAAI,eAAe,aACf,qCAAqC,WAAW,uBAChD;AACJ,KAAI,YAAY;EAEd,MAAM,oBAAoB;EAC1B,MAAM,aAAqC,GACxC,oBAAoB,YACtB;AACD,MAAI,cAAc,KAAA,EAChB,YAAW,aAAa;AAE1B,MAAI,aAAa,KAAA,EACf,YAAW,YAAY;AAIzB,OAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,MAAM,EAAE;GAC/C,MAAM,WAAW,KAAK,KAAK,MAAM,IAAI,CAAC,KAAK,IAAI;GAO/C,IAAI,aANU,uBAAuB,MAClC,SAAS,SAAS,WAA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AAkB,IAAI;;CAG1B,MAAM,WAAW,2BAA2B,qBAAqB,WAAW,gBAAgB,QAAQ;CACpG,MAAM,MAAM,MAAM,MAAM,SAAS;AACjC,KAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR;EACE;EACA,gBAAgB,IAAI;EACpB,gBAAgB,IAAI;EACrB,CAAC,KAAK,KAAK,CACb;AAGH,QAAO,MADe,IAAI,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph_mermaid.js","names":[],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\nimport { toBase64Url } from \"./utils.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n // sort prefixes by path length for correct nesting\n function sortPrefixesByDepth(prefixes: string[]): string[] {\n return [...prefixes].sort((a, b) => {\n return a.split(\":\").length - b.split(\":\").length;\n });\n }\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n\n if (seenSubgraphs.has(prefix)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' at '${prefix} -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(prefix);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n // all nested prefixes for this level, sorted by depth\n const nestedPrefixes = sortPrefixesByDepth(\n Object.keys(edgeGroups).filter(\n (nestedPrefix) =>\n nestedPrefix.startsWith(`${prefix}:`) &&\n nestedPrefix !== prefix &&\n nestedPrefix.split(\":\").length === prefix.split(\":\").length + 1\n )\n );\n\n for (const nestedPrefix of nestedPrefixes) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining top-level subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n const mermaidSyntaxEncoded = toBase64Url(mermaidSyntax);\n\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAGA,SAAS,iBAAiB,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;;AAGlD,MAAM,yBAAyB;CAAC;CAAK;CAAK;CAAI;AAE9C,SAAS,4BACP,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,WAAW,CACzD,WAAU,cAAc,UAAU,GAAG,MAAM;AAE7C,QAAO;;;;;AAMT,SAAgB,YACd,OACA,OACA,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,MAChB,UAAU,EAAE;CAEhB,IAAI,eAAe,aACf,qCAAqC,WAAW,uBAChD;AACJ,KAAI,YAAY;EAEd,MAAM,oBAAoB;EAC1B,MAAM,aAAqC,GACxC,oBAAoB,YACtB;AACD,MAAI,cAAc,KAAA,EAChB,YAAW,aAAa;AAE1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mBT,eAAsB,iBACpB,eACA,QAQA;CACA,IAAI,kBAAkB,QAAQ,mBAAmB;CACjD,MAAM,YAAY,QAAQ,aAAa;CAEvC,MAAM,uBAAuB,YAAY,cAAc;AAGvD,KAAI,oBAAoB,KAAA;MAElB,CADoB,6BACH,KAAK,gBAAgB,CACxC,mBAAkB,IAAI;;CAG1B,MAAM,WAAW,2BAA2B,qBAAqB,WAAW,gBAAgB,QAAQ;CACpG,MAAM,MAAM,MAAM,MAAM,SAAS;AACjC,KAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR;EACE;EACA,gBAAgB,IAAI;EACpB,gBAAgB,IAAI;EACrB,CAAC,KAAK,KAAK,CACb;AAGH,QADgB,MAAM,IAAI,MAAM"}
|
|
1
|
+
{"version":3,"file":"graph_mermaid.js","names":[],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\nimport { toBase64Url } from \"./utils.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n // sort prefixes by path length for correct nesting\n function sortPrefixesByDepth(prefixes: string[]): string[] {\n return [...prefixes].sort((a, b) => {\n return a.split(\":\").length - b.split(\":\").length;\n });\n }\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n\n if (seenSubgraphs.has(prefix)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' at '${prefix} -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(prefix);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n // all nested prefixes for this level, sorted by depth\n const nestedPrefixes = sortPrefixesByDepth(\n Object.keys(edgeGroups).filter(\n (nestedPrefix) =>\n nestedPrefix.startsWith(`${prefix}:`) &&\n nestedPrefix !== prefix &&\n nestedPrefix.split(\":\").length === prefix.split(\":\").length + 1\n )\n );\n\n for (const nestedPrefix of nestedPrefixes) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining top-level subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n const mermaidSyntaxEncoded = toBase64Url(mermaidSyntax);\n\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAGA,SAAS,iBAAiB,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;;AAGlD,MAAM,yBAAyB;CAAC;CAAK;CAAK;CAAI;AAE9C,SAAS,4BACP,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,WAAW,CACzD,WAAU,cAAc,UAAU,GAAG,MAAM;AAE7C,QAAO;;;;;AAMT,SAAgB,YACd,OACA,OACA,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,MAChB,UAAU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AAM,UAAU,WACnB,KAAI,CAAC,OAAO,SAAS,IAAI,IAAI,WAAW,GACtC,aAAY,WAAW,SAAS,OAAO;AAK3C,KAAI,WACF,iBAAgB,4BAA4B,cAAc,EAAE,CAAC;AAG/D,QAAO;;;;;;;;;;;;;;;;;;AAmBT,eAAsB,iBACpB,eACA,QAQA;CACA,IAAI,kBAAkB,QAAQ,mBAAmB;CACjD,MAAM,YAAY,QAAQ,aAAa;CAEvC,MAAM,uBAAuB,YAAY,cAAc;AAGvD,KAAI,oBAAoB,KAAA;MAElB,CAAC,6BAAgB,KAAK,gBAAgB,CACxC,mBAAkB,IAAI;;CAG1B,MAAM,WAAW,2BAA2B,qBAAqB,WAAW,gBAAgB,QAAQ;CACpG,MAAM,MAAM,MAAM,MAAM,SAAS;AACjC,KAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR;EACE;EACA,gBAAgB,IAAI;EACpB,gBAAgB,IAAI;EACrB,CAAC,KAAK,KAAK,CACb;AAGH,QAAO,MADe,IAAI,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.cjs","names":["RunnableBinding","RunnableLambda","RunnablePassthrough","isBaseMessage","HumanMessage","AIMessage"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<\n RunInput,\n RunOutput,\n> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n *\n * @deprecated Use LangGraph's built-in persistence instead.\n *\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/classic\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput,\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory =\n await this.getMessageHistory(sessionId);\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,IAAa,6BAAb,cAGUA,eAAAA,gBAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAA+D;EACzE,IAAI,eAAyBC,eAAAA,eAAe,MAAM,OAAO,YACvD,KAAK,cAAc,OAAO,WAAW,EAAE,CAAC,CACzC,CAAC,WAAW,EAAE,SAAS,eAAe,CAAC;EAExC,MAAM,cAAc,OAAO,sBAAsB,OAAO;AACxD,MAAI,YACF,gBAAeC,oBAAAA,oBAAoB,OAAO,GACvC,cAAc,cAChB,CAAC,CAAC,WAAW,EAAE,SAAS,iBAAiB,CAAC;EAG7C,MAAM,QAAQ,aACX,KACC,OAAO,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,CAAC,OAAO,aAAa,WAAW;GAC1D,MAAM,eAAe,GAClB,KAAK,oBAAoB,UAAU,OACrC;AAED,SAAM,IAAI,MACR,gHACsB,KAAK,UAAU,aAAa,CAAC,IAAI,KAAK,UAHxC,EAAE,cAAc,EAAE,WAAW,OAAO,EAAE,CAKvD,CAAC,GACL;;EAGH,MAAM,EAAE,cAAc,OAAO;AAC7B,SAAO,aAAa,iBAClB,MAAM,KAAK,kBAAkB,UAAU;AACzC,SAAO"}
|
|
1
|
+
{"version":3,"file":"history.cjs","names":["RunnableBinding","RunnableLambda","RunnablePassthrough","isBaseMessage","HumanMessage","AIMessage"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<\n RunInput,\n RunOutput,\n> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n *\n * @deprecated Use LangGraph's built-in persistence instead.\n *\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/classic\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput,\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory =\n await this.getMessageHistory(sessionId);\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,IAAa,6BAAb,cAGUA,eAAAA,gBAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAA+D;EACzE,IAAI,eAAyBC,eAAAA,eAAe,MAAM,OAAO,YACvD,KAAK,cAAc,OAAO,WAAW,EAAE,CAAC,CACzC,CAAC,WAAW,EAAE,SAAS,eAAe,CAAC;EAExC,MAAM,cAAc,OAAO,sBAAsB,OAAO;AACxD,MAAI,YACF,gBAAeC,oBAAAA,oBAAoB,OAAO,GACvC,cAAc,cAChB,CAAC,CAAC,WAAW,EAAE,SAAS,iBAAiB,CAAC;EAG7C,MAAM,QAAQ,aACX,KACC,OAAO,SAAS,cAAc,EAC5B,QAAQ,KAAK,WAAW,KAAK,aAAa,KAAK,UAAU,EAAE,CAAC,EAC7D,CAAC,CACH,CACA,WAAW,EAAE,SAAS,8BAA8B,CAAC;EAExD,MAAM,SAAS,OAAO,UAAU,EAAE;AAElC,QAAM;GACJ,GAAG;GACH;GACA;GACD,CAAC;AACF,OAAK,WAAW,OAAO;AACvB,OAAK,oBAAoB,OAAO;AAChC,OAAK,mBAAmB,OAAO;AAC/B,OAAK,oBAAoB,OAAO;AAChC,OAAK,qBAAqB,OAAO;;CAGnC,kBAEE,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"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<\n RunInput,\n RunOutput,\n> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n *\n * @deprecated Use LangGraph's built-in persistence instead.\n *\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/classic\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput,\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory =\n await this.getMessageHistory(sessionId);\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;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;GACzC,MAAM,mBAAmB,MAAM,QAAQ,aAAa;AACpD,mBAAgB,cAAc,MAAM,iBAAiB,OAAO;;EAG9D,MAAM,cAAc,IAAI;AACxB,MAAI,CAAC,YACH,OAAM,IAAI,MACR,4CAA4C,KAAK,UAC/C,KACA,MACA,EACD,GACF;EAEH,MAAM,iBAAiB,KAAK,mBAAmB,YAAY;AAC3D,QAAM,QAAQ,YAAY,CAAC,GAAG,eAAe,GAAG,eAAe,CAAC;;CAGlE,MAAM,aAAa,GAAG,SAA4C;EAChE,MAAM,SAAS,MAAM,MAAM,aAAa,GAAG,QAAQ;AAEnD,MAAI,CAAC,OAAO,gBAAgB,CAAC,OAAO,aAAa,WAAW;GAC1D,MAAM,eAAe,GAClB,KAAK,oBAAoB,UAAU,OACrC;AAED,SAAM,IAAI,MACR,gHACsB,KAAK,UAAU,aAAa,CAAC,IAAI,KAAK,UAHxC,EAAE,cAAc,EAAE,WAAW,OAAO,EAAE,CAKvD,CAAC,GACL;;EAGH,MAAM,EAAE,cAAc,OAAO;AAC7B,SAAO,aAAa,iBAClB,MAAM,KAAK,kBAAkB,UAAU;AACzC,SAAO"}
|
|
1
|
+
{"version":3,"file":"history.js","names":[],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<\n RunInput,\n RunOutput,\n> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n *\n * @deprecated Use LangGraph's built-in persistence instead.\n *\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/classic\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput,\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory =\n await this.getMessageHistory(sessionId);\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,IAAa,6BAAb,cAGU,gBAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAA+D;EACzE,IAAI,eAAyB,eAAe,MAAM,OAAO,YACvD,KAAK,cAAc,OAAO,WAAW,EAAE,CAAC,CACzC,CAAC,WAAW,EAAE,SAAS,eAAe,CAAC;EAExC,MAAM,cAAc,OAAO,sBAAsB,OAAO;AACxD,MAAI,YACF,gBAAe,oBAAoB,OAAO,GACvC,cAAc,cAChB,CAAC,CAAC,WAAW,EAAE,SAAS,iBAAiB,CAAC;EAG7C,MAAM,QAAQ,aACX,KACC,OAAO,SAAS,cAAc,EAC5B,QAAQ,KAAK,W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,SAAM,IAAI,MACR,gHACsB,KAAK,UAAU,aAAa,CAAC,IAAI,KAAK,UACxD,EAJkB,cAAc,EAAE,WAAW,OAAO,EAIvC,CACd,CAAC,GACL;;EAGH,MAAM,EAAE,cAAc,OAAO;AAC7B,SAAO,aAAa,iBAClB,MAAM,KAAK,kBAAkB,UAAU;AACzC,SAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.cjs","names":["Runnable","ensureConfig"],"sources":["../../src/runnables/router.ts"],"sourcesContent":["import { Runnable, type RunnableBatchOptions } from \"./base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ensureConfig, type RunnableConfig } from \"./config.js\";\n\nexport type RouterInput = {\n key: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any;\n};\n\n/**\n * A runnable that routes to a set of runnables based on Input['key'].\n * Returns the output of the selected runnable.\n * @example\n * ```typescript\n * import { RouterRunnable, RunnableLambda } from \"@langchain/core/runnables\";\n *\n * const router = new RouterRunnable({\n * runnables: {\n * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),\n * reverseText: RunnableLambda.from((text: string) =>\n * text.split(\"\").reverse().join(\"\")\n * ),\n * },\n * });\n *\n * // Invoke the 'reverseText' runnable\n * const result1 = router.invoke({ key: \"reverseText\", input: \"Hello World\" });\n *\n * // \"dlroW olleH\"\n *\n * // Invoke the 'toUpperCase' runnable\n * const result2 = router.invoke({ key: \"toUpperCase\", input: \"Hello World\" });\n *\n * // \"HELLO WORLD\"\n * ```\n */\nexport class RouterRunnable<\n RunInput extends RouterInput,\n RunnableInput,\n RunOutput,\n> extends Runnable<RunInput, RunOutput> {\n static lc_name() {\n return \"RouterRunnable\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n\n constructor(fields: {\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n }) {\n super(fields);\n this.runnables = fields.runnables;\n }\n\n async invoke(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<RunOutput> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.invoke(actualInput, ensureConfig(options));\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<RunOutput[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]> {\n const keys = inputs.map((input) => input.key);\n const actualInputs = inputs.map((input) => input.input);\n const missingKey = keys.find((key) => this.runnables[key] === undefined);\n if (missingKey !== undefined) {\n throw new Error(`One or more keys do not have a corresponding runnable.`);\n }\n const runnables = keys.map((key) => this.runnables[key]);\n const optionsList = this._getOptionsList(options ?? {}, inputs.length);\n const maxConcurrency =\n optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;\n const batchSize =\n maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;\n const batchResults = [];\n for (let i = 0; i < actualInputs.length; i += batchSize) {\n const batchPromises = actualInputs\n .slice(i, i + batchSize)\n .map((actualInput, i) =>\n runnables[i].invoke(actualInput, optionsList[i])\n );\n const batchResult = await Promise.all(batchPromises);\n batchResults.push(batchResult);\n }\n return batchResults.flat();\n }\n\n async stream(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<IterableReadableStream<RunOutput>> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.stream(actualInput, options);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,IAAa,iBAAb,cAIUA,aAAAA,SAA8B;CACtC,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA,YAAY,QAET;AACD,QAAM,OAAO;AACb,OAAK,YAAY,OAAO;;CAG1B,MAAM,OACJ,OACA,SACoB;EACpB,MAAM,EAAE,KAAK,OAAO,gBAAgB;EACpC,MAAM,WAAW,KAAK,UAAU;AAChC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,oCAAoC,IAAI,IAAI;AAE9D,SAAO,SAAS,OAAO,aAAaC,eAAAA,aAAa,QAAQ,CAAC;;CAqB5D,MAAM,MACJ,QACA,SACA,cACgC;EAChC,MAAM,OAAO,OAAO,KAAK,UAAU,MAAM,IAAI;EAC7C,MAAM,eAAe,OAAO,KAAK,UAAU,MAAM,MAAM;AAEvD,MADmB,KAAK,MAAM,QAAQ,KAAK,UAAU,SAAS,KAAA,
|
|
1
|
+
{"version":3,"file":"router.cjs","names":["Runnable","ensureConfig"],"sources":["../../src/runnables/router.ts"],"sourcesContent":["import { Runnable, type RunnableBatchOptions } from \"./base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ensureConfig, type RunnableConfig } from \"./config.js\";\n\nexport type RouterInput = {\n key: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any;\n};\n\n/**\n * A runnable that routes to a set of runnables based on Input['key'].\n * Returns the output of the selected runnable.\n * @example\n * ```typescript\n * import { RouterRunnable, RunnableLambda } from \"@langchain/core/runnables\";\n *\n * const router = new RouterRunnable({\n * runnables: {\n * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),\n * reverseText: RunnableLambda.from((text: string) =>\n * text.split(\"\").reverse().join(\"\")\n * ),\n * },\n * });\n *\n * // Invoke the 'reverseText' runnable\n * const result1 = router.invoke({ key: \"reverseText\", input: \"Hello World\" });\n *\n * // \"dlroW olleH\"\n *\n * // Invoke the 'toUpperCase' runnable\n * const result2 = router.invoke({ key: \"toUpperCase\", input: \"Hello World\" });\n *\n * // \"HELLO WORLD\"\n * ```\n */\nexport class RouterRunnable<\n RunInput extends RouterInput,\n RunnableInput,\n RunOutput,\n> extends Runnable<RunInput, RunOutput> {\n static lc_name() {\n return \"RouterRunnable\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n\n constructor(fields: {\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n }) {\n super(fields);\n this.runnables = fields.runnables;\n }\n\n async invoke(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<RunOutput> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.invoke(actualInput, ensureConfig(options));\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<RunOutput[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]> {\n const keys = inputs.map((input) => input.key);\n const actualInputs = inputs.map((input) => input.input);\n const missingKey = keys.find((key) => this.runnables[key] === undefined);\n if (missingKey !== undefined) {\n throw new Error(`One or more keys do not have a corresponding runnable.`);\n }\n const runnables = keys.map((key) => this.runnables[key]);\n const optionsList = this._getOptionsList(options ?? {}, inputs.length);\n const maxConcurrency =\n optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;\n const batchSize =\n maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;\n const batchResults = [];\n for (let i = 0; i < actualInputs.length; i += batchSize) {\n const batchPromises = actualInputs\n .slice(i, i + batchSize)\n .map((actualInput, i) =>\n runnables[i].invoke(actualInput, optionsList[i])\n );\n const batchResult = await Promise.all(batchPromises);\n batchResults.push(batchResult);\n }\n return batchResults.flat();\n }\n\n async stream(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<IterableReadableStream<RunOutput>> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.stream(actualInput, options);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,IAAa,iBAAb,cAIUA,aAAAA,SAA8B;CACtC,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA,YAAY,QAET;AACD,QAAM,OAAO;AACb,OAAK,YAAY,OAAO;;CAG1B,MAAM,OACJ,OACA,SACoB;EACpB,MAAM,EAAE,KAAK,OAAO,gBAAgB;EACpC,MAAM,WAAW,KAAK,UAAU;AAChC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,oCAAoC,IAAI,IAAI;AAE9D,SAAO,SAAS,OAAO,aAAaC,eAAAA,aAAa,QAAQ,CAAC;;CAqB5D,MAAM,MACJ,QACA,SACA,cACgC;EAChC,MAAM,OAAO,OAAO,KAAK,UAAU,MAAM,IAAI;EAC7C,MAAM,eAAe,OAAO,KAAK,UAAU,MAAM,MAAM;AAEvD,MADmB,KAAK,MAAM,QAAQ,KAAK,UAAU,SAAS,KAAA,EAChD,KAAK,KAAA,EACjB,OAAM,IAAI,MAAM,yDAAyD;EAE3E,MAAM,YAAY,KAAK,KAAK,QAAQ,KAAK,UAAU,KAAK;EACxD,MAAM,cAAc,KAAK,gBAAgB,WAAW,EAAE,EAAE,OAAO,OAAO;EACtE,MAAM,iBACJ,YAAY,IAAI,kBAAkB,cAAc;EAClD,MAAM,YACJ,kBAAkB,iBAAiB,IAAI,iBAAiB,OAAO;EACjE,MAAM,eAAe,EAAE;AACvB,OAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KAAK,WAAW;GACvD,MAAM,gBAAgB,aACnB,MAAM,GAAG,IAAI,UAAU,CACvB,KAAK,aAAa,MACjB,UAAU,GAAG,OAAO,aAAa,YAAY,GAAG,CACjD;GACH,MAAM,cAAc,MAAM,QAAQ,IAAI,cAAc;AACpD,gBAAa,KAAK,YAAY;;AAEhC,SAAO,aAAa,MAAM;;CAG5B,MAAM,OACJ,OACA,SAC4C;EAC5C,MAAM,EAAE,KAAK,OAAO,gBAAgB;EACpC,MAAM,WAAW,KAAK,UAAU;AAChC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,oCAAoC,IAAI,IAAI;AAE9D,SAAO,SAAS,OAAO,aAAa,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","names":[],"sources":["../../src/runnables/router.ts"],"sourcesContent":["import { Runnable, type RunnableBatchOptions } from \"./base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ensureConfig, type RunnableConfig } from \"./config.js\";\n\nexport type RouterInput = {\n key: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any;\n};\n\n/**\n * A runnable that routes to a set of runnables based on Input['key'].\n * Returns the output of the selected runnable.\n * @example\n * ```typescript\n * import { RouterRunnable, RunnableLambda } from \"@langchain/core/runnables\";\n *\n * const router = new RouterRunnable({\n * runnables: {\n * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),\n * reverseText: RunnableLambda.from((text: string) =>\n * text.split(\"\").reverse().join(\"\")\n * ),\n * },\n * });\n *\n * // Invoke the 'reverseText' runnable\n * const result1 = router.invoke({ key: \"reverseText\", input: \"Hello World\" });\n *\n * // \"dlroW olleH\"\n *\n * // Invoke the 'toUpperCase' runnable\n * const result2 = router.invoke({ key: \"toUpperCase\", input: \"Hello World\" });\n *\n * // \"HELLO WORLD\"\n * ```\n */\nexport class RouterRunnable<\n RunInput extends RouterInput,\n RunnableInput,\n RunOutput,\n> extends Runnable<RunInput, RunOutput> {\n static lc_name() {\n return \"RouterRunnable\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n\n constructor(fields: {\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n }) {\n super(fields);\n this.runnables = fields.runnables;\n }\n\n async invoke(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<RunOutput> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.invoke(actualInput, ensureConfig(options));\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<RunOutput[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]> {\n const keys = inputs.map((input) => input.key);\n const actualInputs = inputs.map((input) => input.input);\n const missingKey = keys.find((key) => this.runnables[key] === undefined);\n if (missingKey !== undefined) {\n throw new Error(`One or more keys do not have a corresponding runnable.`);\n }\n const runnables = keys.map((key) => this.runnables[key]);\n const optionsList = this._getOptionsList(options ?? {}, inputs.length);\n const maxConcurrency =\n optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;\n const batchSize =\n maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;\n const batchResults = [];\n for (let i = 0; i < actualInputs.length; i += batchSize) {\n const batchPromises = actualInputs\n .slice(i, i + batchSize)\n .map((actualInput, i) =>\n runnables[i].invoke(actualInput, optionsList[i])\n );\n const batchResult = await Promise.all(batchPromises);\n batchResults.push(batchResult);\n }\n return batchResults.flat();\n }\n\n async stream(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<IterableReadableStream<RunOutput>> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.stream(actualInput, options);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,IAAa,iBAAb,cAIU,SAA8B;CACtC,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe,CAAC,kBAAkB,YAAY;CAE9C,kBAAkB;CAElB;CAEA,YAAY,QAET;AACD,QAAM,OAAO;AACb,OAAK,YAAY,OAAO;;CAG1B,MAAM,OACJ,OACA,SACoB;EACpB,MAAM,EAAE,KAAK,OAAO,gBAAgB;EACpC,MAAM,WAAW,KAAK,UAAU;AAChC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,oCAAoC,IAAI,IAAI;AAE9D,SAAO,SAAS,OAAO,aAAa,aAAa,QAAQ,CAAC;;CAqB5D,MAAM,MACJ,QACA,SACA,cACgC;EAChC,MAAM,OAAO,OAAO,KAAK,UAAU,MAAM,IAAI;EAC7C,MAAM,eAAe,OAAO,KAAK,UAAU,MAAM,MAAM;AAEvD,MADmB,KAAK,MAAM,QAAQ,KAAK,UAAU,SAAS,KAAA,
|
|
1
|
+
{"version":3,"file":"router.js","names":[],"sources":["../../src/runnables/router.ts"],"sourcesContent":["import { Runnable, type RunnableBatchOptions } from \"./base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ensureConfig, type RunnableConfig } from \"./config.js\";\n\nexport type RouterInput = {\n key: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any;\n};\n\n/**\n * A runnable that routes to a set of runnables based on Input['key'].\n * Returns the output of the selected runnable.\n * @example\n * ```typescript\n * import { RouterRunnable, RunnableLambda } from \"@langchain/core/runnables\";\n *\n * const router = new RouterRunnable({\n * runnables: {\n * toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),\n * reverseText: RunnableLambda.from((text: string) =>\n * text.split(\"\").reverse().join(\"\")\n * ),\n * },\n * });\n *\n * // Invoke the 'reverseText' runnable\n * const result1 = router.invoke({ key: \"reverseText\", input: \"Hello World\" });\n *\n * // \"dlroW olleH\"\n *\n * // Invoke the 'toUpperCase' runnable\n * const result2 = router.invoke({ key: \"toUpperCase\", input: \"Hello World\" });\n *\n * // \"HELLO WORLD\"\n * ```\n */\nexport class RouterRunnable<\n RunInput extends RouterInput,\n RunnableInput,\n RunOutput,\n> extends Runnable<RunInput, RunOutput> {\n static lc_name() {\n return \"RouterRunnable\";\n }\n\n lc_namespace = [\"langchain_core\", \"runnables\"];\n\n lc_serializable = true;\n\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n\n constructor(fields: {\n runnables: Record<string, Runnable<RunnableInput, RunOutput>>;\n }) {\n super(fields);\n this.runnables = fields.runnables;\n }\n\n async invoke(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<RunOutput> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.invoke(actualInput, ensureConfig(options));\n }\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions?: false }\n ): Promise<RunOutput[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions & { returnExceptions: true }\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]>;\n\n async batch(\n inputs: RunInput[],\n options?: Partial<RunnableConfig> | Partial<RunnableConfig>[],\n batchOptions?: RunnableBatchOptions\n ): Promise<(RunOutput | Error)[]> {\n const keys = inputs.map((input) => input.key);\n const actualInputs = inputs.map((input) => input.input);\n const missingKey = keys.find((key) => this.runnables[key] === undefined);\n if (missingKey !== undefined) {\n throw new Error(`One or more keys do not have a corresponding runnable.`);\n }\n const runnables = keys.map((key) => this.runnables[key]);\n const optionsList = this._getOptionsList(options ?? {}, inputs.length);\n const maxConcurrency =\n optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;\n const batchSize =\n maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;\n const batchResults = [];\n for (let i = 0; i < actualInputs.length; i += batchSize) {\n const batchPromises = actualInputs\n .slice(i, i + batchSize)\n .map((actualInput, i) =>\n runnables[i].invoke(actualInput, optionsList[i])\n );\n const batchResult = await Promise.all(batchPromises);\n batchResults.push(batchResult);\n }\n return batchResults.flat();\n }\n\n async stream(\n input: RunInput,\n options?: Partial<RunnableConfig>\n ): Promise<IterableReadableStream<RunOutput>> {\n const { key, input: actualInput } = input;\n const runnable = this.runnables[key];\n if (runnable === undefined) {\n throw new Error(`No runnable associated with key \"${key}\".`);\n }\n return runnable.stream(actualInput, options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AC4C;EAC5C,MAAM,EAAE,KAAK,OAAO,gBAAgB;EACpC,MAAM,WAAW,KAAK,UAAU;AAChC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,oCAAoC,IAAI,IAAI;AAE9D,SAAO,SAAS,OAAO,aAAa,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/runnables/utils.ts"],"sourcesContent":["import { StreamEvent } from \"../tracers/event_stream.js\";\nimport type { RunnableInterface } from \"./types.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isRunnableInterface(thing: any): thing is RunnableInterface {\n return thing ? thing.lc_runnable : false;\n}\n\n/**\n * Utility to filter the root event in the streamEvents implementation.\n * This is simply binding the arguments to the namespace to make save on\n * a bit of typing in the streamEvents implementation.\n *\n * TODO: Refactor and remove.\n */\nexport class _RootEventFilter {\n includeNames?: string[];\n\n includeTypes?: string[];\n\n includeTags?: string[];\n\n excludeNames?: string[];\n\n excludeTypes?: string[];\n\n excludeTags?: string[];\n\n constructor(fields: {\n includeNames?: string[];\n includeTypes?: string[];\n includeTags?: string[];\n excludeNames?: string[];\n excludeTypes?: string[];\n excludeTags?: string[];\n }) {\n this.includeNames = fields.includeNames;\n this.includeTypes = fields.includeTypes;\n this.includeTags = fields.includeTags;\n this.excludeNames = fields.excludeNames;\n this.excludeTypes = fields.excludeTypes;\n this.excludeTags = fields.excludeTags;\n }\n\n includeEvent(event: StreamEvent, rootType: string): boolean {\n let include =\n this.includeNames === undefined &&\n this.includeTypes === undefined &&\n this.includeTags === undefined;\n const eventTags = event.tags ?? [];\n\n if (this.includeNames !== undefined) {\n include = include || this.includeNames.includes(event.name);\n }\n if (this.includeTypes !== undefined) {\n include = include || this.includeTypes.includes(rootType);\n }\n if (this.includeTags !== undefined) {\n include =\n include || eventTags.some((tag) => this.includeTags?.includes(tag));\n }\n\n if (this.excludeNames !== undefined) {\n include = include && !this.excludeNames.includes(event.name);\n }\n if (this.excludeTypes !== undefined) {\n include = include && !this.excludeTypes.includes(rootType);\n }\n if (this.excludeTags !== undefined) {\n include =\n include && eventTags.every((tag) => !this.excludeTags?.includes(tag));\n }\n\n return include;\n }\n}\n\nexport const toBase64Url = (str: string): string => {\n // Use btoa for compatibility, assume ASCII\n const encoded = btoa(str);\n return encoded.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n};\n"],"mappings":";AAIA,SAAgB,oBAAoB,OAAwC;AAC1E,QAAO,QAAQ,MAAM,cAAc;;;;;;;;;AAUrC,IAAa,mBAAb,MAA8B;CAC5B;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAOT;AACD,OAAK,eAAe,OAAO;AAC3B,OAAK,eAAe,OAAO;AAC3B,OAAK,cAAc,OAAO;AAC1B,OAAK,eAAe,OAAO;AAC3B,OAAK,eAAe,OAAO;AAC3B,OAAK,cAAc,OAAO;;CAG5B,aAAa,OAAoB,UAA2B;EAC1D,IAAI,UACF,KAAK,iBAAiB,KAAA,KACtB,KAAK,iBAAiB,KAAA,KACtB,KAAK,gBAAgB,KAAA;EACvB,MAAM,YAAY,MAAM,QAAQ,EAAE;AAElC,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,KAAK,aAAa,SAAS,MAAM,KAAK;AAE7D,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,KAAK,aAAa,SAAS,SAAS;AAE3D,MAAI,KAAK,gBAAgB,KAAA,EACvB,WACE,WAAW,UAAU,MAAM,QAAQ,KAAK,aAAa,SAAS,IAAI,CAAC;AAGvE,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,CAAC,KAAK,aAAa,SAAS,MAAM,KAAK;AAE9D,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,CAAC,KAAK,aAAa,SAAS,SAAS;AAE5D,MAAI,KAAK,gBAAgB,KAAA,EACvB,WACE,WAAW,UAAU,OAAO,QAAQ,CAAC,KAAK,aAAa,SAAS,IAAI,CAAC;AAGzE,SAAO;;;AAIX,MAAa,eAAe,QAAwB;AAGlD,QADgB,KAAK,
|
|
1
|
+
{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/runnables/utils.ts"],"sourcesContent":["import { StreamEvent } from \"../tracers/event_stream.js\";\nimport type { RunnableInterface } from \"./types.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isRunnableInterface(thing: any): thing is RunnableInterface {\n return thing ? thing.lc_runnable : false;\n}\n\n/**\n * Utility to filter the root event in the streamEvents implementation.\n * This is simply binding the arguments to the namespace to make save on\n * a bit of typing in the streamEvents implementation.\n *\n * TODO: Refactor and remove.\n */\nexport class _RootEventFilter {\n includeNames?: string[];\n\n includeTypes?: string[];\n\n includeTags?: string[];\n\n excludeNames?: string[];\n\n excludeTypes?: string[];\n\n excludeTags?: string[];\n\n constructor(fields: {\n includeNames?: string[];\n includeTypes?: string[];\n includeTags?: string[];\n excludeNames?: string[];\n excludeTypes?: string[];\n excludeTags?: string[];\n }) {\n this.includeNames = fields.includeNames;\n this.includeTypes = fields.includeTypes;\n this.includeTags = fields.includeTags;\n this.excludeNames = fields.excludeNames;\n this.excludeTypes = fields.excludeTypes;\n this.excludeTags = fields.excludeTags;\n }\n\n includeEvent(event: StreamEvent, rootType: string): boolean {\n let include =\n this.includeNames === undefined &&\n this.includeTypes === undefined &&\n this.includeTags === undefined;\n const eventTags = event.tags ?? [];\n\n if (this.includeNames !== undefined) {\n include = include || this.includeNames.includes(event.name);\n }\n if (this.includeTypes !== undefined) {\n include = include || this.includeTypes.includes(rootType);\n }\n if (this.includeTags !== undefined) {\n include =\n include || eventTags.some((tag) => this.includeTags?.includes(tag));\n }\n\n if (this.excludeNames !== undefined) {\n include = include && !this.excludeNames.includes(event.name);\n }\n if (this.excludeTypes !== undefined) {\n include = include && !this.excludeTypes.includes(rootType);\n }\n if (this.excludeTags !== undefined) {\n include =\n include && eventTags.every((tag) => !this.excludeTags?.includes(tag));\n }\n\n return include;\n }\n}\n\nexport const toBase64Url = (str: string): string => {\n // Use btoa for compatibility, assume ASCII\n const encoded = btoa(str);\n return encoded.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n};\n"],"mappings":";AAIA,SAAgB,oBAAoB,OAAwC;AAC1E,QAAO,QAAQ,MAAM,cAAc;;;;;;;;;AAUrC,IAAa,mBAAb,MAA8B;CAC5B;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAOT;AACD,OAAK,eAAe,OAAO;AAC3B,OAAK,eAAe,OAAO;AAC3B,OAAK,cAAc,OAAO;AAC1B,OAAK,eAAe,OAAO;AAC3B,OAAK,eAAe,OAAO;AAC3B,OAAK,cAAc,OAAO;;CAG5B,aAAa,OAAoB,UAA2B;EAC1D,IAAI,UACF,KAAK,iBAAiB,KAAA,KACtB,KAAK,iBAAiB,KAAA,KACtB,KAAK,gBAAgB,KAAA;EACvB,MAAM,YAAY,MAAM,QAAQ,EAAE;AAElC,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,KAAK,aAAa,SAAS,MAAM,KAAK;AAE7D,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,KAAK,aAAa,SAAS,SAAS;AAE3D,MAAI,KAAK,gBAAgB,KAAA,EACvB,WACE,WAAW,UAAU,MAAM,QAAQ,KAAK,aAAa,SAAS,IAAI,CAAC;AAGvE,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,CAAC,KAAK,aAAa,SAAS,MAAM,KAAK;AAE9D,MAAI,KAAK,iBAAiB,KAAA,EACxB,WAAU,WAAW,CAAC,KAAK,aAAa,SAAS,SAAS;AAE5D,MAAI,KAAK,gBAAgB,KAAA,EACvB,WACE,WAAW,UAAU,OAAO,QAAQ,CAAC,KAAK,aAAa,SAAS,IAAI,CAAC;AAGzE,SAAO;;;AAIX,MAAa,eAAe,QAAwB;AAGlD,QADgB,KAA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