@langchain/core 0.3.72 → 1.0.0-alpha.2
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/README.md +3 -3
- package/dist/_virtual/rolldown_runtime.cjs +32 -0
- package/dist/_virtual/rolldown_runtime.js +11 -0
- package/dist/agents.cjs +12 -2
- package/dist/agents.cjs.map +1 -0
- package/dist/agents.d.cts +19 -0
- package/dist/agents.d.cts.map +1 -0
- package/dist/agents.d.ts +16 -10
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +6 -1
- package/dist/agents.js.map +1 -0
- package/dist/caches/base.cjs +99 -104
- package/dist/caches/base.cjs.map +1 -0
- package/dist/caches/base.d.cts +83 -0
- package/dist/caches/base.d.cts.map +1 -0
- package/dist/caches/base.d.ts +60 -45
- package/dist/caches/base.d.ts.map +1 -0
- package/dist/caches/base.js +91 -98
- package/dist/caches/base.js.map +1 -0
- package/dist/callbacks/base.cjs +112 -185
- package/dist/callbacks/base.cjs.map +1 -0
- package/dist/callbacks/base.d.cts +318 -0
- package/dist/callbacks/base.d.cts.map +1 -0
- package/dist/callbacks/base.d.ts +234 -222
- package/dist/callbacks/base.d.ts.map +1 -0
- package/dist/callbacks/base.js +107 -149
- package/dist/callbacks/base.js.map +1 -0
- package/dist/callbacks/dispatch/index.cjs +56 -46
- package/dist/callbacks/dispatch/index.cjs.map +1 -0
- package/dist/callbacks/dispatch/index.d.cts +43 -0
- package/dist/callbacks/dispatch/index.d.cts.map +1 -0
- package/dist/callbacks/dispatch/index.d.ts +10 -2
- package/dist/callbacks/dispatch/index.d.ts.map +1 -0
- package/dist/callbacks/dispatch/index.js +48 -41
- package/dist/callbacks/dispatch/index.js.map +1 -0
- package/dist/callbacks/dispatch/web.cjs +60 -56
- package/dist/callbacks/dispatch/web.cjs.map +1 -0
- package/dist/callbacks/dispatch/web.d.cts +40 -0
- package/dist/callbacks/dispatch/web.d.cts.map +1 -0
- package/dist/callbacks/dispatch/web.d.ts +10 -2
- package/dist/callbacks/dispatch/web.d.ts.map +1 -0
- package/dist/callbacks/dispatch/web.js +54 -53
- package/dist/callbacks/dispatch/web.js.map +1 -0
- package/dist/callbacks/manager.cjs +580 -877
- package/dist/callbacks/manager.cjs.map +1 -0
- package/dist/callbacks/manager.d.cts +192 -0
- package/dist/callbacks/manager.d.cts.map +1 -0
- package/dist/callbacks/manager.d.ts +146 -133
- package/dist/callbacks/manager.d.ts.map +1 -0
- package/dist/callbacks/manager.js +564 -863
- package/dist/callbacks/manager.js.map +1 -0
- package/dist/callbacks/promises.cjs +20 -6
- package/dist/callbacks/promises.cjs.map +1 -0
- package/dist/callbacks/promises.d.cts +2 -0
- package/dist/callbacks/promises.d.ts +1 -1
- package/dist/callbacks/promises.js +12 -1
- package/dist/callbacks/promises.js.map +1 -0
- package/dist/chat_history.cjs +126 -119
- package/dist/chat_history.cjs.map +1 -0
- package/dist/chat_history.d.cts +99 -0
- package/dist/chat_history.d.cts.map +1 -0
- package/dist/chat_history.d.ts +82 -75
- package/dist/chat_history.d.ts.map +1 -0
- package/dist/chat_history.js +118 -113
- package/dist/chat_history.js.map +1 -0
- package/dist/context.cjs +29 -18
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +7 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.ts +7 -2
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +18 -11
- package/dist/context.js.map +1 -0
- package/dist/document_loaders/base.cjs +31 -22
- package/dist/document_loaders/base.cjs.map +1 -0
- package/dist/document_loaders/base.d.cts +34 -0
- package/dist/document_loaders/base.d.cts.map +1 -0
- package/dist/document_loaders/base.d.ts +22 -16
- package/dist/document_loaders/base.d.ts.map +1 -0
- package/dist/document_loaders/base.js +26 -19
- package/dist/document_loaders/base.js.map +1 -0
- package/dist/document_loaders/langsmith.cjs +128 -195
- package/dist/document_loaders/langsmith.cjs.map +1 -0
- package/dist/document_loaders/langsmith.d.cts +103 -0
- package/dist/document_loaders/langsmith.d.cts.map +1 -0
- package/dist/document_loaders/langsmith.d.ts +52 -45
- package/dist/document_loaders/langsmith.d.ts.map +1 -0
- package/dist/document_loaders/langsmith.js +121 -190
- package/dist/document_loaders/langsmith.js.map +1 -0
- package/dist/documents/document.cjs +23 -40
- package/dist/documents/document.cjs.map +1 -0
- package/dist/documents/document.d.cts +50 -0
- package/dist/documents/document.d.cts.map +1 -0
- package/dist/documents/document.d.ts +44 -31
- package/dist/documents/document.d.ts.map +1 -0
- package/dist/documents/document.js +23 -37
- package/dist/documents/document.js.map +1 -0
- package/dist/documents/index.cjs +23 -18
- package/dist/documents/index.cjs.map +1 -0
- package/dist/documents/index.d.cts +3 -0
- package/dist/documents/index.d.ts +3 -2
- package/dist/documents/index.js +15 -2
- package/dist/documents/index.js.map +1 -0
- package/dist/documents/transformers.cjs +46 -48
- package/dist/documents/transformers.cjs.map +1 -0
- package/dist/documents/transformers.d.cts +44 -0
- package/dist/documents/transformers.d.cts.map +1 -0
- package/dist/documents/transformers.d.ts +27 -21
- package/dist/documents/transformers.d.ts.map +1 -0
- package/dist/documents/transformers.js +45 -43
- package/dist/documents/transformers.js.map +1 -0
- package/dist/embeddings.cjs +28 -22
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +56 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +45 -39
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +22 -18
- package/dist/embeddings.js.map +1 -0
- package/dist/errors/index.cjs +9 -8
- package/dist/errors/index.cjs.map +1 -0
- package/dist/errors/index.js +9 -6
- package/dist/errors/index.js.map +1 -0
- package/dist/example_selectors/base.cjs +15 -17
- package/dist/example_selectors/base.cjs.map +1 -0
- package/dist/example_selectors/base.d.cts +26 -0
- package/dist/example_selectors/base.d.cts.map +1 -0
- package/dist/example_selectors/base.d.ts +21 -15
- package/dist/example_selectors/base.d.ts.map +1 -0
- package/dist/example_selectors/base.js +15 -13
- package/dist/example_selectors/base.js.map +1 -0
- package/dist/example_selectors/conditional.cjs +56 -68
- package/dist/example_selectors/conditional.cjs.map +1 -0
- package/dist/example_selectors/conditional.d.cts +62 -0
- package/dist/example_selectors/conditional.d.cts.map +1 -0
- package/dist/example_selectors/conditional.d.ts +42 -43
- package/dist/example_selectors/conditional.d.ts.map +1 -0
- package/dist/example_selectors/conditional.js +54 -63
- package/dist/example_selectors/conditional.js.map +1 -0
- package/dist/example_selectors/index.cjs +33 -20
- package/dist/example_selectors/index.cjs.map +1 -0
- package/dist/example_selectors/index.d.cts +5 -0
- package/dist/example_selectors/index.d.ts +5 -4
- package/dist/example_selectors/index.js +21 -4
- package/dist/example_selectors/index.js.map +1 -0
- package/dist/example_selectors/length_based.cjs +118 -145
- package/dist/example_selectors/length_based.cjs.map +1 -0
- package/dist/example_selectors/length_based.d.cts +95 -0
- package/dist/example_selectors/length_based.d.cts.map +1 -0
- package/dist/example_selectors/length_based.d.ts +45 -39
- package/dist/example_selectors/length_based.d.ts.map +1 -0
- package/dist/example_selectors/length_based.js +118 -141
- package/dist/example_selectors/length_based.js.map +1 -0
- package/dist/example_selectors/semantic_similarity.cjs +118 -135
- package/dist/example_selectors/semantic_similarity.cjs.map +1 -0
- package/dist/example_selectors/semantic_similarity.d.cts +97 -0
- package/dist/example_selectors/semantic_similarity.d.cts.map +1 -0
- package/dist/example_selectors/semantic_similarity.d.ts +59 -53
- package/dist/example_selectors/semantic_similarity.d.ts.map +1 -0
- package/dist/example_selectors/semantic_similarity.js +117 -130
- package/dist/example_selectors/semantic_similarity.js.map +1 -0
- package/dist/index.cjs +12 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/indexing/base.cjs +207 -272
- package/dist/indexing/base.cjs.map +1 -0
- package/dist/indexing/base.d.cts +125 -0
- package/dist/indexing/base.d.cts.map +1 -0
- package/dist/indexing/base.d.ts +88 -80
- package/dist/indexing/base.d.ts.map +1 -0
- package/dist/indexing/base.js +205 -267
- package/dist/indexing/base.js.map +1 -0
- package/dist/indexing/index.cjs +33 -18
- package/dist/indexing/index.cjs.map +1 -0
- package/dist/indexing/index.d.cts +3 -0
- package/dist/indexing/index.d.ts +3 -2
- package/dist/indexing/index.js +20 -2
- package/dist/indexing/index.js.map +1 -0
- package/dist/indexing/record_manager.cjs +11 -17
- package/dist/indexing/record_manager.cjs.map +1 -0
- package/dist/indexing/record_manager.d.cts +70 -0
- package/dist/indexing/record_manager.d.cts.map +1 -0
- package/dist/indexing/record_manager.d.ts +65 -59
- package/dist/indexing/record_manager.d.ts.map +1 -0
- package/dist/indexing/record_manager.js +10 -13
- package/dist/indexing/record_manager.js.map +1 -0
- package/dist/language_models/base.cjs +212 -293
- package/dist/language_models/base.cjs.map +1 -0
- package/dist/language_models/base.d.cts +301 -0
- package/dist/language_models/base.d.cts.map +1 -0
- package/dist/language_models/base.d.ts +256 -212
- package/dist/language_models/base.d.ts.map +1 -0
- package/dist/language_models/base.js +202 -285
- package/dist/language_models/base.js.map +1 -0
- package/dist/language_models/chat_models.cjs +500 -622
- package/dist/language_models/chat_models.cjs.map +1 -0
- package/dist/language_models/chat_models.d.cts +261 -0
- package/dist/language_models/chat_models.d.cts.map +1 -0
- package/dist/language_models/chat_models.d.ts +228 -172
- package/dist/language_models/chat_models.d.ts.map +1 -0
- package/dist/language_models/chat_models.js +492 -616
- package/dist/language_models/chat_models.js.map +1 -0
- package/dist/language_models/llms.cjs +328 -376
- package/dist/language_models/llms.cjs.map +1 -0
- package/dist/language_models/llms.d.cts +149 -0
- package/dist/language_models/llms.d.cts.map +1 -0
- package/dist/language_models/llms.d.ts +135 -115
- package/dist/language_models/llms.d.ts.map +1 -0
- package/dist/language_models/llms.js +321 -371
- package/dist/language_models/llms.js.map +1 -0
- package/dist/language_models/utils.cjs +19 -0
- package/dist/language_models/utils.cjs.map +1 -0
- package/dist/language_models/utils.js +17 -0
- package/dist/language_models/utils.js.map +1 -0
- package/dist/load/import_constants.cjs +7 -5
- package/dist/load/import_constants.cjs.map +1 -0
- package/dist/load/import_constants.js +6 -2
- package/dist/load/import_constants.js.map +1 -0
- package/dist/load/import_map.cjs +138 -79
- package/dist/load/import_map.cjs.map +1 -0
- package/dist/load/import_map.js +134 -44
- package/dist/load/import_map.js.map +1 -0
- package/dist/load/import_type.d.cts +7 -0
- package/dist/load/import_type.d.cts.map +1 -0
- package/dist/load/import_type.d.ts +7 -4
- package/dist/load/import_type.d.ts.map +1 -0
- package/dist/load/index.cjs +85 -192
- package/dist/load/index.cjs.map +1 -0
- package/dist/load/index.d.cts +12 -0
- package/dist/load/index.d.cts.map +1 -0
- package/dist/load/index.d.ts +11 -6
- package/dist/load/index.d.ts.map +1 -0
- package/dist/load/index.js +84 -155
- package/dist/load/index.js.map +1 -0
- package/dist/load/map_keys.cjs +16 -19
- package/dist/load/map_keys.cjs.map +1 -0
- package/dist/load/map_keys.d.cts +8 -0
- package/dist/load/map_keys.d.cts.map +1 -0
- package/dist/load/map_keys.d.ts +7 -8
- package/dist/load/map_keys.d.ts.map +1 -0
- package/dist/load/map_keys.js +14 -12
- package/dist/load/map_keys.js.map +1 -0
- package/dist/load/serializable.cjs +143 -185
- package/dist/load/serializable.cjs.map +1 -0
- package/dist/load/serializable.d.cts +82 -0
- package/dist/load/serializable.d.cts.map +1 -0
- package/dist/load/serializable.d.ts +74 -68
- package/dist/load/serializable.d.ts.map +1 -0
- package/dist/load/serializable.js +136 -180
- package/dist/load/serializable.js.map +1 -0
- package/dist/memory.cjs +60 -51
- package/dist/memory.cjs.map +1 -0
- package/dist/memory.d.cts +69 -0
- package/dist/memory.d.cts.map +1 -0
- package/dist/memory.d.ts +34 -27
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +54 -47
- package/dist/memory.js.map +1 -0
- package/dist/messages/ai.cjs +280 -333
- package/dist/messages/ai.cjs.map +1 -0
- package/dist/messages/ai.d.cts +148 -0
- package/dist/messages/ai.d.cts.map +1 -0
- package/dist/messages/ai.d.ts +119 -107
- package/dist/messages/ai.d.ts.map +1 -0
- package/dist/messages/ai.js +279 -328
- package/dist/messages/ai.js.map +1 -0
- package/dist/messages/base.cjs +225 -364
- package/dist/messages/base.cjs.map +1 -0
- package/dist/messages/base.d.cts +188 -0
- package/dist/messages/base.d.cts.map +1 -0
- package/dist/messages/base.d.ts +147 -147
- package/dist/messages/base.d.ts.map +1 -0
- package/dist/messages/base.js +222 -357
- package/dist/messages/base.js.map +1 -0
- package/dist/messages/block_translators/anthropic.cjs +320 -0
- package/dist/messages/block_translators/anthropic.cjs.map +1 -0
- package/dist/messages/block_translators/anthropic.js +319 -0
- package/dist/messages/block_translators/anthropic.js.map +1 -0
- package/dist/messages/block_translators/data.cjs +71 -0
- package/dist/messages/block_translators/data.cjs.map +1 -0
- package/dist/messages/block_translators/data.js +69 -0
- package/dist/messages/block_translators/data.js.map +1 -0
- package/dist/messages/block_translators/index.cjs +12 -0
- package/dist/messages/block_translators/index.cjs.map +1 -0
- package/dist/messages/block_translators/index.js +12 -0
- package/dist/messages/block_translators/index.js.map +1 -0
- package/dist/messages/block_translators/openai.cjs +324 -0
- package/dist/messages/block_translators/openai.cjs.map +1 -0
- package/dist/messages/block_translators/openai.js +323 -0
- package/dist/messages/block_translators/openai.js.map +1 -0
- package/dist/messages/block_translators/utils.cjs +35 -0
- package/dist/messages/block_translators/utils.cjs.map +1 -0
- package/dist/messages/block_translators/utils.js +28 -0
- package/dist/messages/block_translators/utils.js.map +1 -0
- package/dist/messages/chat.cjs +78 -86
- package/dist/messages/chat.cjs.map +1 -0
- package/dist/messages/chat.d.cts +37 -0
- package/dist/messages/chat.d.cts.map +1 -0
- package/dist/messages/chat.d.ts +27 -22
- package/dist/messages/chat.d.ts.map +1 -0
- package/dist/messages/chat.js +77 -81
- package/dist/messages/chat.js.map +1 -0
- package/dist/messages/content/base.d.cts +20 -0
- package/dist/messages/content/base.d.cts.map +1 -0
- package/dist/messages/content/base.d.ts +20 -0
- package/dist/messages/content/base.d.ts.map +1 -0
- package/dist/messages/content/data.cjs +146 -0
- package/dist/messages/content/data.cjs.map +1 -0
- package/dist/messages/content/data.d.cts +280 -0
- package/dist/messages/content/data.d.cts.map +1 -0
- package/dist/messages/content/data.d.ts +280 -0
- package/dist/messages/content/data.d.ts.map +1 -0
- package/dist/messages/content/data.js +137 -0
- package/dist/messages/content/data.js.map +1 -0
- package/dist/messages/content/index.cjs +14 -0
- package/dist/messages/content/index.cjs.map +1 -0
- package/dist/messages/content/index.d.cts +101 -0
- package/dist/messages/content/index.d.cts.map +1 -0
- package/dist/messages/content/index.d.ts +101 -0
- package/dist/messages/content/index.d.ts.map +1 -0
- package/dist/messages/content/index.js +14 -0
- package/dist/messages/content/index.js.map +1 -0
- package/dist/messages/content/multimodal.cjs +13 -0
- package/dist/messages/content/multimodal.cjs.map +1 -0
- package/dist/messages/content/multimodal.d.cts +108 -0
- package/dist/messages/content/multimodal.d.cts.map +1 -0
- package/dist/messages/content/multimodal.d.ts +108 -0
- package/dist/messages/content/multimodal.d.ts.map +1 -0
- package/dist/messages/content/multimodal.js +12 -0
- package/dist/messages/content/multimodal.js.map +1 -0
- package/dist/messages/content/tools.cjs +15 -0
- package/dist/messages/content/tools.cjs.map +1 -0
- package/dist/messages/content/tools.d.cts +151 -0
- package/dist/messages/content/tools.d.cts.map +1 -0
- package/dist/messages/content/tools.d.ts +151 -0
- package/dist/messages/content/tools.d.ts.map +1 -0
- package/dist/messages/content/tools.js +14 -0
- package/dist/messages/content/tools.js.map +1 -0
- package/dist/messages/function.cjs +49 -49
- package/dist/messages/function.cjs.map +1 -0
- package/dist/messages/function.d.cts +30 -0
- package/dist/messages/function.d.cts.map +1 -0
- package/dist/messages/function.d.ts +20 -16
- package/dist/messages/function.d.ts.map +1 -0
- package/dist/messages/function.js +48 -44
- package/dist/messages/function.js.map +1 -0
- package/dist/messages/human.cjs +47 -49
- package/dist/messages/human.cjs.map +1 -0
- package/dist/messages/human.d.cts +31 -0
- package/dist/messages/human.d.cts.map +1 -0
- package/dist/messages/human.d.ts +22 -22
- package/dist/messages/human.d.ts.map +1 -0
- package/dist/messages/human.js +46 -44
- package/dist/messages/human.js.map +1 -0
- package/dist/messages/index.cjs +139 -34
- package/dist/messages/index.cjs.map +1 -0
- package/dist/messages/index.d.cts +13 -0
- package/dist/messages/index.d.ts +13 -11
- package/dist/messages/index.js +78 -13
- package/dist/messages/index.js.map +1 -0
- package/dist/messages/modifier.cjs +30 -33
- package/dist/messages/modifier.cjs.map +1 -0
- package/dist/messages/modifier.d.cts +24 -0
- package/dist/messages/modifier.d.cts.map +1 -0
- package/dist/messages/modifier.d.ts +18 -13
- package/dist/messages/modifier.d.ts.map +1 -0
- package/dist/messages/modifier.js +30 -29
- package/dist/messages/modifier.js.map +1 -0
- package/dist/messages/system.cjs +47 -49
- package/dist/messages/system.cjs.map +1 -0
- package/dist/messages/system.d.cts +31 -0
- package/dist/messages/system.d.cts.map +1 -0
- package/dist/messages/system.d.ts +22 -22
- package/dist/messages/system.d.ts.map +1 -0
- package/dist/messages/system.js +46 -44
- package/dist/messages/system.js.map +1 -0
- package/dist/messages/tool.cjs +153 -189
- package/dist/messages/tool.cjs.map +1 -0
- package/dist/messages/tool.d.cts +173 -0
- package/dist/messages/tool.d.cts.map +1 -0
- package/dist/messages/tool.d.ts +96 -90
- package/dist/messages/tool.d.ts.map +1 -0
- package/dist/messages/tool.js +145 -183
- package/dist/messages/tool.js.map +1 -0
- package/dist/messages/transformers.cjs +287 -431
- package/dist/messages/transformers.cjs.map +1 -0
- package/dist/messages/transformers.d.cts +472 -0
- package/dist/messages/transformers.d.cts.map +1 -0
- package/dist/messages/transformers.d.ts +111 -105
- package/dist/messages/transformers.d.ts.map +1 -0
- package/dist/messages/transformers.js +284 -425
- package/dist/messages/transformers.js.map +1 -0
- package/dist/messages/utils.cjs +172 -267
- package/dist/messages/utils.cjs.map +1 -0
- package/dist/messages/utils.d.cts +36 -0
- package/dist/messages/utils.d.cts.map +1 -0
- package/dist/messages/utils.d.ts +13 -8
- package/dist/messages/utils.d.ts.map +1 -0
- package/dist/messages/utils.js +170 -262
- package/dist/messages/utils.js.map +1 -0
- package/dist/output_parsers/base.cjs +104 -120
- package/dist/output_parsers/base.cjs.map +1 -0
- package/dist/output_parsers/base.d.cts +108 -0
- package/dist/output_parsers/base.d.cts.map +1 -0
- package/dist/output_parsers/base.d.ts +76 -71
- package/dist/output_parsers/base.d.ts.map +1 -0
- package/dist/output_parsers/base.js +102 -114
- package/dist/output_parsers/base.js.map +1 -0
- package/dist/output_parsers/bytes.cjs +27 -39
- package/dist/output_parsers/bytes.cjs.map +1 -0
- package/dist/output_parsers/bytes.d.cts +19 -0
- package/dist/output_parsers/bytes.d.cts.map +1 -0
- package/dist/output_parsers/bytes.d.ts +13 -7
- package/dist/output_parsers/bytes.d.ts.map +1 -0
- package/dist/output_parsers/bytes.js +27 -35
- package/dist/output_parsers/bytes.js.map +1 -0
- package/dist/output_parsers/index.cjs +66 -24
- package/dist/output_parsers/index.cjs.map +1 -0
- package/dist/output_parsers/index.d.cts +10 -0
- package/dist/output_parsers/index.d.ts +10 -8
- package/dist/output_parsers/index.js +40 -8
- package/dist/output_parsers/index.js.map +1 -0
- package/dist/output_parsers/json.cjs +36 -57
- package/dist/output_parsers/json.cjs.map +1 -0
- package/dist/output_parsers/json.d.cts +26 -0
- package/dist/output_parsers/json.d.cts.map +1 -0
- package/dist/output_parsers/json.d.ts +20 -13
- package/dist/output_parsers/json.d.ts.map +1 -0
- package/dist/output_parsers/json.js +36 -52
- package/dist/output_parsers/json.js.map +1 -0
- package/dist/output_parsers/list.cjs +155 -239
- package/dist/output_parsers/list.cjs.map +1 -0
- package/dist/output_parsers/list.d.cts +85 -0
- package/dist/output_parsers/list.d.cts.map +1 -0
- package/dist/output_parsers/list.d.ts +67 -58
- package/dist/output_parsers/list.d.ts.map +1 -0
- package/dist/output_parsers/list.js +151 -231
- package/dist/output_parsers/list.js.map +1 -0
- package/dist/output_parsers/openai_functions/index.cjs +22 -17
- package/dist/output_parsers/openai_functions/index.cjs.map +1 -0
- package/dist/output_parsers/openai_functions/index.d.cts +2 -0
- package/dist/output_parsers/openai_functions/index.d.ts +2 -1
- package/dist/output_parsers/openai_functions/index.js +14 -1
- package/dist/output_parsers/openai_functions/index.js.map +1 -0
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.cjs +140 -203
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.cjs.map +1 -0
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.cts +90 -0
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.cts.map +1 -0
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts +68 -58
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts.map +1 -0
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.js +138 -197
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.js.map +1 -0
- package/dist/output_parsers/openai_tools/index.cjs +26 -17
- package/dist/output_parsers/openai_tools/index.cjs.map +1 -0
- package/dist/output_parsers/openai_tools/index.d.cts +2 -0
- package/dist/output_parsers/openai_tools/index.d.ts +2 -1
- package/dist/output_parsers/openai_tools/index.js +16 -1
- package/dist/output_parsers/openai_tools/index.js.map +1 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +176 -256
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs.map +1 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.cts +120 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.cts.map +1 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +98 -70
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts.map +1 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +173 -249
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js.map +1 -0
- package/dist/output_parsers/string.cjs +70 -84
- package/dist/output_parsers/string.cjs.map +1 -0
- package/dist/output_parsers/string.d.cts +45 -0
- package/dist/output_parsers/string.d.cts.map +1 -0
- package/dist/output_parsers/string.d.ts +25 -19
- package/dist/output_parsers/string.d.ts.map +1 -0
- package/dist/output_parsers/string.js +70 -80
- package/dist/output_parsers/string.js.map +1 -0
- package/dist/output_parsers/structured.cjs +148 -181
- package/dist/output_parsers/structured.cjs.map +1 -0
- package/dist/output_parsers/structured.d.cts +97 -0
- package/dist/output_parsers/structured.d.cts.map +1 -0
- package/dist/output_parsers/structured.d.ts +82 -74
- package/dist/output_parsers/structured.d.ts.map +1 -0
- package/dist/output_parsers/structured.js +144 -174
- package/dist/output_parsers/structured.js.map +1 -0
- package/dist/output_parsers/transform.cjs +79 -109
- package/dist/output_parsers/transform.cjs.map +1 -0
- package/dist/output_parsers/transform.d.cts +41 -0
- package/dist/output_parsers/transform.d.cts.map +1 -0
- package/dist/output_parsers/transform.d.ts +29 -22
- package/dist/output_parsers/transform.d.ts.map +1 -0
- package/dist/output_parsers/transform.js +77 -104
- package/dist/output_parsers/transform.js.map +1 -0
- package/dist/output_parsers/xml.cjs +95 -129
- package/dist/output_parsers/xml.cjs.map +1 -0
- package/dist/output_parsers/xml.d.cts +34 -0
- package/dist/output_parsers/xml.d.cts.map +1 -0
- package/dist/output_parsers/xml.d.ts +29 -24
- package/dist/output_parsers/xml.d.ts.map +1 -0
- package/dist/output_parsers/xml.js +93 -124
- package/dist/output_parsers/xml.js.map +1 -0
- package/dist/outputs.cjs +57 -57
- package/dist/outputs.cjs.map +1 -0
- package/dist/outputs.d.cts +72 -0
- package/dist/outputs.d.cts.map +1 -0
- package/dist/outputs.d.ts +55 -44
- package/dist/outputs.d.ts.map +1 -0
- package/dist/outputs.js +50 -53
- package/dist/outputs.js.map +1 -0
- package/dist/prompt_values.cjs +101 -148
- package/dist/prompt_values.cjs.map +1 -0
- package/dist/prompt_values.d.cts +83 -0
- package/dist/prompt_values.d.cts.map +1 -0
- package/dist/prompt_values.d.ts +53 -48
- package/dist/prompt_values.d.ts.map +1 -0
- package/dist/prompt_values.js +90 -139
- package/dist/prompt_values.js.map +1 -0
- package/dist/prompts/base.cjs +106 -173
- package/dist/prompts/base.cjs.map +1 -0
- package/dist/prompts/base.d.cts +108 -0
- package/dist/prompts/base.d.cts.map +1 -0
- package/dist/prompts/base.d.ts +98 -85
- package/dist/prompts/base.d.ts.map +1 -0
- package/dist/prompts/base.js +106 -136
- package/dist/prompts/base.js.map +1 -0
- package/dist/prompts/chat.cjs +533 -786
- package/dist/prompts/chat.cjs.map +1 -0
- package/dist/prompts/chat.d.cts +296 -0
- package/dist/prompts/chat.d.cts.map +1 -0
- package/dist/prompts/chat.d.ts +191 -136
- package/dist/prompts/chat.d.ts.map +1 -0
- package/dist/prompts/chat.js +522 -771
- package/dist/prompts/chat.js.map +1 -0
- package/dist/prompts/dict.cjs +60 -110
- package/dist/prompts/dict.cjs.map +1 -0
- package/dist/prompts/dict.d.cts +23 -0
- package/dist/prompts/dict.d.cts.map +1 -0
- package/dist/prompts/dict.d.ts +19 -14
- package/dist/prompts/dict.d.ts.map +1 -0
- package/dist/prompts/dict.js +58 -104
- package/dist/prompts/dict.js.map +1 -0
- package/dist/prompts/few_shot.cjs +239 -352
- package/dist/prompts/few_shot.cjs.map +1 -0
- package/dist/prompts/few_shot.d.cts +204 -0
- package/dist/prompts/few_shot.d.cts.map +1 -0
- package/dist/prompts/few_shot.d.ts +157 -148
- package/dist/prompts/few_shot.d.ts.map +1 -0
- package/dist/prompts/few_shot.js +237 -346
- package/dist/prompts/few_shot.js.map +1 -0
- package/dist/prompts/image.cjs +92 -126
- package/dist/prompts/image.cjs.map +1 -0
- package/dist/prompts/image.d.cts +86 -0
- package/dist/prompts/image.d.cts.map +1 -0
- package/dist/prompts/image.d.ts +76 -60
- package/dist/prompts/image.d.ts.map +1 -0
- package/dist/prompts/image.js +92 -122
- package/dist/prompts/image.js.map +1 -0
- package/dist/prompts/index.cjs +79 -27
- package/dist/prompts/index.cjs.map +1 -0
- package/dist/prompts/index.d.cts +12 -0
- package/dist/prompts/index.d.ts +12 -11
- package/dist/prompts/index.js +47 -11
- package/dist/prompts/index.js.map +1 -0
- package/dist/prompts/pipeline.cjs +128 -142
- package/dist/prompts/pipeline.cjs.map +1 -0
- package/dist/prompts/pipeline.d.cts +104 -0
- package/dist/prompts/pipeline.d.cts.map +1 -0
- package/dist/prompts/pipeline.d.ts +48 -42
- package/dist/prompts/pipeline.d.ts.map +1 -0
- package/dist/prompts/pipeline.js +127 -137
- package/dist/prompts/pipeline.js.map +1 -0
- package/dist/prompts/prompt.cjs +131 -164
- package/dist/prompts/prompt.cjs.map +1 -0
- package/dist/prompts/prompt.d.cts +134 -0
- package/dist/prompts/prompt.d.cts.map +1 -0
- package/dist/prompts/prompt.d.ts +106 -79
- package/dist/prompts/prompt.d.ts.map +1 -0
- package/dist/prompts/prompt.js +131 -160
- package/dist/prompts/prompt.js.map +1 -0
- package/dist/prompts/serde.d.cts +50 -0
- package/dist/prompts/serde.d.cts.map +1 -0
- package/dist/prompts/serde.d.ts +24 -17
- package/dist/prompts/serde.d.ts.map +1 -0
- package/dist/prompts/string.cjs +23 -23
- package/dist/prompts/string.cjs.map +1 -0
- package/dist/prompts/string.d.cts +27 -0
- package/dist/prompts/string.d.cts.map +1 -0
- package/dist/prompts/string.d.ts +21 -11
- package/dist/prompts/string.d.ts.map +1 -0
- package/dist/prompts/string.js +23 -19
- package/dist/prompts/string.js.map +1 -0
- package/dist/prompts/structured.cjs +46 -70
- package/dist/prompts/structured.cjs.map +1 -0
- package/dist/prompts/structured.d.cts +40 -0
- package/dist/prompts/structured.d.cts.map +1 -0
- package/dist/prompts/structured.d.ts +33 -14
- package/dist/prompts/structured.d.ts.map +1 -0
- package/dist/prompts/structured.js +46 -66
- package/dist/prompts/structured.js.map +1 -0
- package/dist/prompts/template.cjs +138 -157
- package/dist/prompts/template.cjs.map +1 -0
- package/dist/prompts/template.d.cts +48 -0
- package/dist/prompts/template.d.cts.map +1 -0
- package/dist/prompts/template.d.ts +24 -19
- package/dist/prompts/template.d.ts.map +1 -0
- package/dist/prompts/template.js +135 -150
- package/dist/prompts/template.js.map +1 -0
- package/dist/retrievers/document_compressors/base.cjs +21 -11
- package/dist/retrievers/document_compressors/base.cjs.map +1 -0
- package/dist/retrievers/document_compressors/base.d.cts +25 -0
- package/dist/retrievers/document_compressors/base.d.cts.map +1 -0
- package/dist/retrievers/document_compressors/base.d.ts +20 -13
- package/dist/retrievers/document_compressors/base.d.ts.map +1 -0
- package/dist/retrievers/document_compressors/base.js +16 -8
- package/dist/retrievers/document_compressors/base.js.map +1 -0
- package/dist/retrievers/index.cjs +119 -129
- package/dist/retrievers/index.cjs.map +1 -0
- package/dist/retrievers/index.d.cts +144 -0
- package/dist/retrievers/index.d.cts.map +1 -0
- package/dist/retrievers/index.d.ts +98 -88
- package/dist/retrievers/index.d.ts.map +1 -0
- package/dist/retrievers/index.js +113 -125
- package/dist/retrievers/index.js.map +1 -0
- package/dist/runnables/base.cjs +1808 -2421
- package/dist/runnables/base.cjs.map +1 -0
- package/dist/runnables/base.d.cts +990 -0
- package/dist/runnables/base.d.cts.map +1 -0
- package/dist/runnables/base.d.ts +633 -592
- package/dist/runnables/base.d.ts.map +1 -0
- package/dist/runnables/base.js +1791 -2396
- package/dist/runnables/base.js.map +1 -0
- package/dist/runnables/branch.cjs +151 -205
- package/dist/runnables/branch.cjs.map +1 -0
- package/dist/runnables/branch.d.cts +94 -0
- package/dist/runnables/branch.d.cts.map +1 -0
- package/dist/runnables/branch.d.ts +50 -51
- package/dist/runnables/branch.d.ts.map +1 -0
- package/dist/runnables/branch.js +151 -201
- package/dist/runnables/branch.js.map +1 -0
- package/dist/runnables/config.cjs +136 -212
- package/dist/runnables/config.cjs.map +1 -0
- package/dist/runnables/config.d.cts +26 -0
- package/dist/runnables/config.d.cts.map +1 -0
- package/dist/runnables/config.d.ts +18 -7
- package/dist/runnables/config.d.ts.map +1 -0
- package/dist/runnables/config.js +134 -207
- package/dist/runnables/config.js.map +1 -0
- package/dist/runnables/graph.cjs +215 -260
- package/dist/runnables/graph.cjs.map +1 -0
- package/dist/runnables/graph.d.cts +52 -0
- package/dist/runnables/graph.d.cts.map +1 -0
- package/dist/runnables/graph.d.ts +49 -41
- package/dist/runnables/graph.d.ts.map +1 -0
- package/dist/runnables/graph.js +206 -253
- package/dist/runnables/graph.js.map +1 -0
- package/dist/runnables/graph_mermaid.cjs +86 -141
- package/dist/runnables/graph_mermaid.cjs.map +1 -0
- package/dist/runnables/graph_mermaid.js +86 -139
- package/dist/runnables/graph_mermaid.js.map +1 -0
- package/dist/runnables/history.cjs +160 -260
- package/dist/runnables/history.cjs.map +1 -0
- package/dist/runnables/history.d.cts +104 -0
- package/dist/runnables/history.d.cts.map +1 -0
- package/dist/runnables/history.d.ts +38 -26
- package/dist/runnables/history.d.ts.map +1 -0
- package/dist/runnables/history.js +160 -256
- package/dist/runnables/history.js.map +1 -0
- package/dist/runnables/index.cjs +65 -31
- package/dist/runnables/index.cjs.map +1 -0
- package/dist/runnables/index.d.cts +8 -0
- package/dist/runnables/index.d.ts +8 -7
- package/dist/runnables/index.js +38 -6
- package/dist/runnables/index.js.map +1 -0
- package/dist/runnables/iter.cjs +27 -42
- package/dist/runnables/iter.cjs.map +1 -0
- package/dist/runnables/iter.js +26 -37
- package/dist/runnables/iter.js.map +1 -0
- package/dist/runnables/passthrough.cjs +97 -125
- package/dist/runnables/passthrough.cjs.map +1 -0
- package/dist/runnables/passthrough.d.cts +76 -0
- package/dist/runnables/passthrough.d.cts.map +1 -0
- package/dist/runnables/passthrough.d.ts +46 -40
- package/dist/runnables/passthrough.d.ts.map +1 -0
- package/dist/runnables/passthrough.js +97 -121
- package/dist/runnables/passthrough.js.map +1 -0
- package/dist/runnables/remote.cjs +387 -508
- package/dist/runnables/remote.cjs.map +1 -0
- package/dist/runnables/remote.d.cts +73 -0
- package/dist/runnables/remote.d.cts.map +1 -0
- package/dist/runnables/remote.d.ts +52 -40
- package/dist/runnables/remote.d.ts.map +1 -0
- package/dist/runnables/remote.js +379 -502
- package/dist/runnables/remote.js.map +1 -0
- package/dist/runnables/router.cjs +74 -95
- package/dist/runnables/router.cjs.map +1 -0
- package/dist/runnables/router.d.cts +58 -0
- package/dist/runnables/router.d.cts.map +1 -0
- package/dist/runnables/router.d.ts +27 -21
- package/dist/runnables/router.d.ts.map +1 -0
- package/dist/runnables/router.js +74 -91
- package/dist/runnables/router.js.map +1 -0
- package/dist/runnables/types.d.cts +81 -0
- package/dist/runnables/types.d.cts.map +1 -0
- package/dist/runnables/types.d.ts +68 -55
- package/dist/runnables/types.d.ts.map +1 -0
- package/dist/runnables/utils.cjs +40 -85
- package/dist/runnables/utils.cjs.map +1 -0
- package/dist/runnables/utils.js +40 -82
- package/dist/runnables/utils.js.map +1 -0
- package/dist/runnables/wrappers.cjs +14 -15
- package/dist/runnables/wrappers.cjs.map +1 -0
- package/dist/runnables/wrappers.js +14 -12
- package/dist/runnables/wrappers.js.map +1 -0
- package/dist/singletons/async_local_storage/context.cjs +167 -177
- package/dist/singletons/async_local_storage/context.cjs.map +1 -0
- package/dist/singletons/async_local_storage/context.d.cts +163 -0
- package/dist/singletons/async_local_storage/context.d.cts.map +1 -0
- package/dist/singletons/async_local_storage/context.d.ts +17 -9
- package/dist/singletons/async_local_storage/context.d.ts.map +1 -0
- package/dist/singletons/async_local_storage/context.js +168 -175
- package/dist/singletons/async_local_storage/context.js.map +1 -0
- package/dist/singletons/async_local_storage/globals.cjs +11 -8
- package/dist/singletons/async_local_storage/globals.cjs.map +1 -0
- package/dist/singletons/async_local_storage/globals.d.cts +11 -0
- package/dist/singletons/async_local_storage/globals.d.cts.map +1 -0
- package/dist/singletons/async_local_storage/globals.d.ts +10 -8
- package/dist/singletons/async_local_storage/globals.d.ts.map +1 -0
- package/dist/singletons/async_local_storage/globals.js +11 -6
- package/dist/singletons/async_local_storage/globals.js.map +1 -0
- package/dist/singletons/async_local_storage/index.cjs +55 -65
- package/dist/singletons/async_local_storage/index.cjs.map +1 -0
- package/dist/singletons/async_local_storage/index.d.cts +18 -0
- package/dist/singletons/async_local_storage/index.d.cts.map +1 -0
- package/dist/singletons/async_local_storage/index.d.ts +13 -9
- package/dist/singletons/async_local_storage/index.d.ts.map +1 -0
- package/dist/singletons/async_local_storage/index.js +53 -61
- package/dist/singletons/async_local_storage/index.js.map +1 -0
- package/dist/singletons/callbacks.cjs +43 -60
- package/dist/singletons/callbacks.cjs.map +1 -0
- package/dist/singletons/callbacks.d.cts +16 -0
- package/dist/singletons/callbacks.d.cts.map +1 -0
- package/dist/singletons/callbacks.d.ts +8 -3
- package/dist/singletons/callbacks.d.ts.map +1 -0
- package/dist/singletons/callbacks.js +43 -54
- package/dist/singletons/callbacks.js.map +1 -0
- package/dist/singletons/index.cjs +23 -8
- package/dist/singletons/index.cjs.map +1 -0
- package/dist/singletons/index.d.cts +3 -0
- package/dist/singletons/index.d.ts +3 -4
- package/dist/singletons/index.js +14 -3
- package/dist/singletons/index.js.map +1 -0
- package/dist/singletons/tracer.cjs +13 -19
- package/dist/singletons/tracer.cjs.map +1 -0
- package/dist/singletons/tracer.js +13 -15
- package/dist/singletons/tracer.js.map +1 -0
- package/dist/stores.cjs +78 -87
- package/dist/stores.cjs.map +1 -0
- package/dist/stores.d.cts +111 -0
- package/dist/stores.d.cts.map +1 -0
- package/dist/stores.d.ts +84 -78
- package/dist/stores.d.ts.map +1 -0
- package/dist/stores.js +71 -82
- package/dist/stores.js.map +1 -0
- package/dist/structured_query/base.cjs +86 -136
- package/dist/structured_query/base.cjs.map +1 -0
- package/dist/structured_query/base.d.cts +75 -0
- package/dist/structured_query/base.d.cts.map +1 -0
- package/dist/structured_query/base.d.ts +53 -47
- package/dist/structured_query/base.d.ts.map +1 -0
- package/dist/structured_query/base.js +86 -132
- package/dist/structured_query/base.js.map +1 -0
- package/dist/structured_query/functional.cjs +163 -234
- package/dist/structured_query/functional.cjs.map +1 -0
- package/dist/structured_query/functional.d.cts +98 -0
- package/dist/structured_query/functional.d.cts.map +1 -0
- package/dist/structured_query/functional.d.ts +72 -67
- package/dist/structured_query/functional.d.ts.map +1 -0
- package/dist/structured_query/functional.js +163 -230
- package/dist/structured_query/functional.js.map +1 -0
- package/dist/structured_query/index.cjs +55 -20
- package/dist/structured_query/index.cjs.map +1 -0
- package/dist/structured_query/index.d.cts +5 -0
- package/dist/structured_query/index.d.ts +5 -4
- package/dist/structured_query/index.js +32 -4
- package/dist/structured_query/index.js.map +1 -0
- package/dist/structured_query/ir.cjs +76 -132
- package/dist/structured_query/ir.cjs.map +1 -0
- package/dist/structured_query/ir.d.cts +143 -0
- package/dist/structured_query/ir.d.cts.map +1 -0
- package/dist/structured_query/ir.d.ts +61 -56
- package/dist/structured_query/ir.d.ts.map +1 -0
- package/dist/structured_query/ir.js +69 -124
- package/dist/structured_query/ir.js.map +1 -0
- package/dist/structured_query/utils.cjs +58 -83
- package/dist/structured_query/utils.cjs.map +1 -0
- package/dist/structured_query/utils.d.cts +40 -0
- package/dist/structured_query/utils.d.cts.map +1 -0
- package/dist/structured_query/utils.d.ts +14 -7
- package/dist/structured_query/utils.d.ts.map +1 -0
- package/dist/structured_query/utils.js +57 -80
- package/dist/structured_query/utils.js.map +1 -0
- package/dist/tools/index.cjs +321 -437
- package/dist/tools/index.cjs.map +1 -0
- package/dist/tools/index.d.cts +209 -0
- package/dist/tools/index.d.cts.map +1 -0
- package/dist/tools/index.d.ts +153 -149
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +305 -424
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/types.cjs +44 -52
- package/dist/tools/types.cjs.map +1 -0
- package/dist/tools/types.d.cts +283 -0
- package/dist/tools/types.d.cts.map +1 -0
- package/dist/tools/types.d.ts +149 -138
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +45 -50
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/utils.cjs +20 -32
- package/dist/tools/utils.cjs.map +1 -0
- package/dist/tools/utils.d.cts +14 -0
- package/dist/tools/utils.d.cts.map +1 -0
- package/dist/tools/utils.d.ts +8 -10
- package/dist/tools/utils.d.ts.map +1 -0
- package/dist/tools/utils.js +20 -29
- package/dist/tools/utils.js.map +1 -0
- package/dist/tracers/base.cjs +441 -529
- package/dist/tracers/base.cjs.map +1 -0
- package/dist/tracers/base.d.cts +304 -0
- package/dist/tracers/base.d.cts.map +1 -0
- package/dist/tracers/base.d.ts +296 -271
- package/dist/tracers/base.d.ts.map +1 -0
- package/dist/tracers/base.js +435 -525
- package/dist/tracers/base.js.map +1 -0
- package/dist/tracers/console.cjs +206 -223
- package/dist/tracers/console.cjs.map +1 -0
- package/dist/tracers/console.d.cts +125 -0
- package/dist/tracers/console.d.cts.map +1 -0
- package/dist/tracers/console.d.ts +109 -101
- package/dist/tracers/console.d.ts.map +1 -0
- package/dist/tracers/console.js +199 -215
- package/dist/tracers/console.js.map +1 -0
- package/dist/tracers/event_stream.cjs +357 -529
- package/dist/tracers/event_stream.cjs.map +1 -0
- package/dist/tracers/event_stream.d.cts +98 -0
- package/dist/tracers/event_stream.d.cts.map +1 -0
- package/dist/tracers/event_stream.d.ts +87 -129
- package/dist/tracers/event_stream.d.ts.map +1 -0
- package/dist/tracers/event_stream.js +355 -523
- package/dist/tracers/event_stream.js.map +1 -0
- package/dist/tracers/initialize.cjs +40 -29
- package/dist/tracers/initialize.cjs.map +1 -0
- package/dist/tracers/initialize.d.cts +26 -0
- package/dist/tracers/initialize.d.cts.map +1 -0
- package/dist/tracers/initialize.d.ts +8 -2
- package/dist/tracers/initialize.d.ts.map +1 -0
- package/dist/tracers/initialize.js +33 -25
- package/dist/tracers/initialize.js.map +1 -0
- package/dist/tracers/log_stream.cjs +264 -425
- package/dist/tracers/log_stream.cjs.map +1 -0
- package/dist/tracers/log_stream.d.cts +129 -0
- package/dist/tracers/log_stream.d.cts.map +1 -0
- package/dist/tracers/log_stream.d.ts +103 -92
- package/dist/tracers/log_stream.d.ts.map +1 -0
- package/dist/tracers/log_stream.js +255 -418
- package/dist/tracers/log_stream.js.map +1 -0
- package/dist/tracers/root_listener.cjs +41 -80
- package/dist/tracers/root_listener.cjs.map +1 -0
- package/dist/tracers/root_listener.js +41 -76
- package/dist/tracers/root_listener.js.map +1 -0
- package/dist/tracers/run_collector.cjs +44 -48
- package/dist/tracers/run_collector.cjs.map +1 -0
- package/dist/tracers/run_collector.d.cts +34 -0
- package/dist/tracers/run_collector.d.cts.map +1 -0
- package/dist/tracers/run_collector.d.ts +27 -19
- package/dist/tracers/run_collector.d.ts.map +1 -0
- package/dist/tracers/run_collector.js +38 -44
- package/dist/tracers/run_collector.js.map +1 -0
- package/dist/tracers/tracer_langchain.cjs +91 -123
- package/dist/tracers/tracer_langchain.cjs.map +1 -0
- package/dist/tracers/tracer_langchain.d.cts +49 -0
- package/dist/tracers/tracer_langchain.d.cts.map +1 -0
- package/dist/tracers/tracer_langchain.d.ts +43 -38
- package/dist/tracers/tracer_langchain.d.ts.map +1 -0
- package/dist/tracers/tracer_langchain.js +84 -118
- package/dist/tracers/tracer_langchain.js.map +1 -0
- package/dist/tracers/tracer_langchain_v1.cjs +166 -198
- package/dist/tracers/tracer_langchain_v1.cjs.map +1 -0
- package/dist/tracers/tracer_langchain_v1.d.cts +64 -0
- package/dist/tracers/tracer_langchain_v1.d.cts.map +1 -0
- package/dist/tracers/tracer_langchain_v1.d.ts +56 -51
- package/dist/tracers/tracer_langchain_v1.d.ts.map +1 -0
- package/dist/tracers/tracer_langchain_v1.js +158 -192
- package/dist/tracers/tracer_langchain_v1.js.map +1 -0
- package/dist/types/_internal.d.cts +8 -0
- package/dist/types/_internal.d.cts.map +1 -0
- package/dist/types/_internal.d.ts +8 -1
- package/dist/types/_internal.d.ts.map +1 -0
- package/dist/types/stream.cjs +12 -2
- package/dist/types/stream.cjs.map +1 -0
- package/dist/types/stream.d.cts +3 -0
- package/dist/types/stream.d.ts +3 -2
- package/dist/types/stream.js +6 -1
- package/dist/types/stream.js.map +1 -0
- package/dist/types/type-utils.d.cts +7 -0
- package/dist/types/type-utils.d.cts.map +1 -0
- package/dist/types/type-utils.d.ts +7 -1
- package/dist/types/type-utils.d.ts.map +1 -0
- package/dist/utils/async_caller.cjs +81 -122
- package/dist/utils/async_caller.cjs.map +1 -0
- package/dist/utils/async_caller.d.cts +52 -0
- package/dist/utils/async_caller.d.cts.map +1 -0
- package/dist/utils/async_caller.d.ts +36 -29
- package/dist/utils/async_caller.d.ts.map +1 -0
- package/dist/utils/async_caller.js +74 -114
- package/dist/utils/async_caller.js.map +1 -0
- package/dist/utils/callbacks.cjs +14 -14
- package/dist/utils/callbacks.cjs.map +1 -0
- package/dist/utils/callbacks.js +15 -11
- package/dist/utils/callbacks.js.map +1 -0
- package/dist/utils/chunk_array.cjs +18 -8
- package/dist/utils/chunk_array.cjs.map +1 -0
- package/dist/utils/chunk_array.d.cts +5 -0
- package/dist/utils/chunk_array.d.cts.map +1 -0
- package/dist/utils/chunk_array.d.ts +5 -1
- package/dist/utils/chunk_array.d.ts.map +1 -0
- package/dist/utils/chunk_array.js +14 -6
- package/dist/utils/chunk_array.js.map +1 -0
- package/dist/utils/env.cjs +62 -72
- package/dist/utils/env.cjs.map +1 -0
- package/dist/utils/env.d.cts +36 -0
- package/dist/utils/env.d.cts.map +1 -0
- package/dist/utils/env.d.ts +30 -22
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/env.js +54 -66
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/event_source_parse.cjs +172 -225
- package/dist/utils/event_source_parse.cjs.map +1 -0
- package/dist/utils/event_source_parse.d.cts +47 -0
- package/dist/utils/event_source_parse.d.cts.map +1 -0
- package/dist/utils/event_source_parse.d.ts +22 -14
- package/dist/utils/event_source_parse.d.ts.map +1 -0
- package/dist/utils/event_source_parse.js +164 -220
- package/dist/utils/event_source_parse.js.map +1 -0
- package/dist/utils/fast-json-patch/index.cjs +14 -58
- package/dist/utils/fast-json-patch/index.cjs.map +1 -0
- package/dist/utils/fast-json-patch/index.js +14 -15
- package/dist/utils/fast-json-patch/index.js.map +1 -0
- package/dist/utils/fast-json-patch/src/core.cjs +319 -445
- package/dist/utils/fast-json-patch/src/core.cjs.map +1 -0
- package/dist/utils/fast-json-patch/src/core.d.cts +80 -0
- package/dist/utils/fast-json-patch/src/core.d.cts.map +1 -0
- package/dist/utils/fast-json-patch/src/core.d.ts +38 -69
- package/dist/utils/fast-json-patch/src/core.d.ts.map +1 -0
- package/dist/utils/fast-json-patch/src/core.js +314 -442
- package/dist/utils/fast-json-patch/src/core.js.map +1 -0
- package/dist/utils/fast-json-patch/src/duplex.cjs +72 -231
- package/dist/utils/fast-json-patch/src/duplex.cjs.map +1 -0
- package/dist/utils/fast-json-patch/src/duplex.d.cts +11 -0
- package/dist/utils/fast-json-patch/src/duplex.d.cts.map +1 -0
- package/dist/utils/fast-json-patch/src/duplex.d.ts +7 -19
- package/dist/utils/fast-json-patch/src/duplex.d.ts.map +1 -0
- package/dist/utils/fast-json-patch/src/duplex.js +73 -226
- package/dist/utils/fast-json-patch/src/duplex.js.map +1 -0
- package/dist/utils/fast-json-patch/src/helpers.cjs +99 -171
- package/dist/utils/fast-json-patch/src/helpers.cjs.map +1 -0
- package/dist/utils/fast-json-patch/src/helpers.js +99 -166
- package/dist/utils/fast-json-patch/src/helpers.js.map +1 -0
- package/dist/utils/function_calling.cjs +62 -59
- package/dist/utils/function_calling.cjs.map +1 -0
- package/dist/utils/function_calling.d.cts +43 -0
- package/dist/utils/function_calling.d.cts.map +1 -0
- package/dist/utils/function_calling.d.ts +22 -15
- package/dist/utils/function_calling.d.ts.map +1 -0
- package/dist/utils/function_calling.js +51 -50
- package/dist/utils/function_calling.js.map +1 -0
- package/dist/utils/hash.cjs +21 -7
- package/dist/utils/hash.cjs.map +1 -0
- package/dist/utils/hash.d.cts +14 -0
- package/dist/utils/hash.d.cts.map +1 -0
- package/dist/utils/hash.d.ts +9 -3
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -2
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/js-sha1/hash.cjs +272 -356
- package/dist/utils/js-sha1/hash.cjs.map +1 -0
- package/dist/utils/js-sha1/hash.d.cts +9 -0
- package/dist/utils/js-sha1/hash.d.cts.map +1 -0
- package/dist/utils/js-sha1/hash.d.ts +5 -1
- package/dist/utils/js-sha1/hash.d.ts.map +1 -0
- package/dist/utils/js-sha1/hash.js +268 -356
- package/dist/utils/js-sha1/hash.js.map +1 -0
- package/dist/utils/js-sha256/hash.cjs +332 -430
- package/dist/utils/js-sha256/hash.cjs.map +1 -0
- package/dist/utils/js-sha256/hash.d.cts +5 -0
- package/dist/utils/js-sha256/hash.d.cts.map +1 -0
- package/dist/utils/js-sha256/hash.d.ts +5 -1
- package/dist/utils/js-sha256/hash.d.ts.map +1 -0
- package/dist/utils/js-sha256/hash.js +328 -430
- package/dist/utils/js-sha256/hash.js.map +1 -0
- package/dist/utils/json.cjs +47 -101
- package/dist/utils/json.cjs.map +1 -0
- package/dist/utils/json.d.cts +8 -0
- package/dist/utils/json.d.cts.map +1 -0
- package/dist/utils/json.d.ts +8 -2
- package/dist/utils/json.d.ts.map +1 -0
- package/dist/utils/json.js +47 -99
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/json_patch.cjs +22 -6
- package/dist/utils/json_patch.cjs.map +1 -0
- package/dist/utils/json_patch.d.cts +3 -0
- package/dist/utils/json_patch.d.ts +3 -1
- package/dist/utils/json_patch.js +15 -1
- package/dist/utils/json_patch.js.map +1 -0
- package/dist/utils/json_schema.cjs +77 -92
- package/dist/utils/json_schema.cjs.map +1 -0
- package/dist/utils/json_schema.d.cts +28 -0
- package/dist/utils/json_schema.d.cts.map +1 -0
- package/dist/utils/json_schema.d.ts +15 -6
- package/dist/utils/json_schema.d.ts.map +1 -0
- package/dist/utils/json_schema.js +58 -85
- package/dist/utils/json_schema.js.map +1 -0
- package/dist/utils/math.cjs +112 -108
- package/dist/utils/math.cjs.map +1 -0
- package/dist/utils/math.d.cts +43 -0
- package/dist/utils/math.d.cts.map +1 -0
- package/dist/utils/math.d.ts +10 -7
- package/dist/utils/math.d.ts.map +1 -0
- package/dist/utils/math.js +103 -102
- package/dist/utils/math.js.map +1 -0
- package/dist/utils/ml-distance/distances.cjs +15 -14
- package/dist/utils/ml-distance/distances.cjs.map +1 -0
- package/dist/utils/ml-distance/distances.js +15 -12
- package/dist/utils/ml-distance/distances.js.map +1 -0
- package/dist/utils/ml-distance/similarities.cjs +20 -17
- package/dist/utils/ml-distance/similarities.cjs.map +1 -0
- package/dist/utils/ml-distance/similarities.d.cts +11 -0
- package/dist/utils/ml-distance/similarities.d.cts.map +1 -0
- package/dist/utils/ml-distance/similarities.d.ts +5 -1
- package/dist/utils/ml-distance/similarities.d.ts.map +1 -0
- package/dist/utils/ml-distance/similarities.js +20 -15
- package/dist/utils/ml-distance/similarities.js.map +1 -0
- package/dist/utils/ml-distance-euclidean/euclidean.cjs +10 -10
- package/dist/utils/ml-distance-euclidean/euclidean.cjs.map +1 -0
- package/dist/utils/ml-distance-euclidean/euclidean.js +11 -8
- package/dist/utils/ml-distance-euclidean/euclidean.js.map +1 -0
- package/dist/utils/sax-js/sax.cjs +1206 -1556
- package/dist/utils/sax-js/sax.cjs.map +1 -0
- package/dist/utils/sax-js/sax.js +1205 -1553
- package/dist/utils/sax-js/sax.js.map +1 -0
- package/dist/utils/signal.cjs +39 -27
- package/dist/utils/signal.cjs.map +1 -0
- package/dist/utils/signal.js +38 -25
- package/dist/utils/signal.js.map +1 -0
- package/dist/utils/stream.cjs +186 -268
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.d.cts +54 -0
- package/dist/utils/stream.d.cts.map +1 -0
- package/dist/utils/stream.d.ts +49 -33
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +177 -261
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/testing/index.cjs +664 -888
- package/dist/utils/testing/index.cjs.map +1 -0
- package/dist/utils/testing/index.d.cts +383 -0
- package/dist/utils/testing/index.d.cts.map +1 -0
- package/dist/utils/testing/index.d.ts +280 -250
- package/dist/utils/testing/index.d.ts.map +1 -0
- package/dist/utils/testing/index.js +639 -865
- package/dist/utils/testing/index.js.map +1 -0
- package/dist/utils/tiktoken.cjs +28 -19
- package/dist/utils/tiktoken.cjs.map +1 -0
- package/dist/utils/tiktoken.d.cts +8 -0
- package/dist/utils/tiktoken.d.cts.map +1 -0
- package/dist/utils/tiktoken.d.ts +7 -2
- package/dist/utils/tiktoken.d.ts.map +1 -0
- package/dist/utils/tiktoken.js +22 -16
- package/dist/utils/tiktoken.js.map +1 -0
- package/dist/utils/types/index.cjs +60 -17
- package/dist/utils/types/index.cjs.map +1 -0
- package/dist/utils/types/index.d.cts +16 -0
- package/dist/utils/types/index.d.cts.map +1 -0
- package/dist/utils/types/index.d.ts +13 -5
- package/dist/utils/types/index.d.ts.map +1 -0
- package/dist/utils/types/index.js +33 -1
- package/dist/utils/types/index.js.map +1 -0
- package/dist/utils/types/zod.cjs +376 -554
- package/dist/utils/types/zod.cjs.map +1 -0
- package/dist/utils/types/zod.d.cts +213 -0
- package/dist/utils/types/zod.d.cts.map +1 -0
- package/dist/utils/types/zod.d.ts +49 -48
- package/dist/utils/types/zod.d.ts.map +1 -0
- package/dist/utils/types/zod.js +376 -552
- package/dist/utils/types/zod.js.map +1 -0
- package/dist/vectorstores.cjs +391 -424
- package/dist/vectorstores.cjs.map +1 -0
- package/dist/vectorstores.d.cts +661 -0
- package/dist/vectorstores.d.cts.map +1 -0
- package/dist/vectorstores.d.ts +488 -471
- package/dist/vectorstores.d.ts.map +1 -0
- package/dist/vectorstores.js +383 -418
- package/dist/vectorstores.js.map +1 -0
- package/package.json +536 -654
- package/agents.cjs +0 -1
- package/agents.d.cts +0 -1
- package/agents.d.ts +0 -1
- package/agents.js +0 -1
- package/caches.cjs +0 -1
- package/caches.d.cts +0 -1
- package/caches.d.ts +0 -1
- package/caches.js +0 -1
- package/callbacks/base.cjs +0 -1
- package/callbacks/base.d.cts +0 -1
- package/callbacks/base.d.ts +0 -1
- package/callbacks/base.js +0 -1
- package/callbacks/dispatch/web.cjs +0 -1
- package/callbacks/dispatch/web.d.cts +0 -1
- package/callbacks/dispatch/web.d.ts +0 -1
- package/callbacks/dispatch/web.js +0 -1
- package/callbacks/dispatch.cjs +0 -1
- package/callbacks/dispatch.d.cts +0 -1
- package/callbacks/dispatch.d.ts +0 -1
- package/callbacks/dispatch.js +0 -1
- package/callbacks/manager.cjs +0 -1
- package/callbacks/manager.d.cts +0 -1
- package/callbacks/manager.d.ts +0 -1
- package/callbacks/manager.js +0 -1
- package/callbacks/promises.cjs +0 -1
- package/callbacks/promises.d.cts +0 -1
- package/callbacks/promises.d.ts +0 -1
- package/callbacks/promises.js +0 -1
- package/chat_history.cjs +0 -1
- package/chat_history.d.cts +0 -1
- package/chat_history.d.ts +0 -1
- package/chat_history.js +0 -1
- package/context.cjs +0 -1
- package/context.d.cts +0 -1
- package/context.d.ts +0 -1
- package/context.js +0 -1
- package/dist/errors/index.d.ts +0 -2
- package/dist/load/import_constants.d.ts +0 -1
- package/dist/load/import_map.d.ts +0 -43
- package/dist/load/import_type.cjs +0 -3
- package/dist/load/import_type.js +0 -2
- package/dist/messages/content_blocks.cjs +0 -165
- package/dist/messages/content_blocks.d.ts +0 -151
- package/dist/messages/content_blocks.js +0 -154
- package/dist/prompts/serde.cjs +0 -2
- package/dist/prompts/serde.js +0 -1
- package/dist/runnables/graph_mermaid.d.ts +0 -18
- package/dist/runnables/iter.d.ts +0 -6
- package/dist/runnables/types.cjs +0 -2
- package/dist/runnables/types.js +0 -1
- package/dist/runnables/utils.d.ts +0 -27
- package/dist/runnables/wrappers.d.ts +0 -2
- package/dist/singletons/tracer.d.ts +0 -3
- package/dist/tracers/root_listener.d.ts +0 -25
- package/dist/types/_internal.cjs +0 -2
- package/dist/types/_internal.js +0 -1
- package/dist/types/type-utils.cjs +0 -2
- package/dist/types/type-utils.js +0 -1
- package/dist/utils/callbacks.d.ts +0 -1
- package/dist/utils/fast-json-patch/index.d.ts +0 -22
- package/dist/utils/fast-json-patch/src/helpers.d.ts +0 -36
- package/dist/utils/ml-distance/distances.d.ts +0 -8
- package/dist/utils/ml-distance-euclidean/euclidean.d.ts +0 -2
- package/dist/utils/sax-js/sax.d.ts +0 -2
- package/dist/utils/signal.d.ts +0 -1
- package/dist/utils/testing/helpers.cjs +0 -7
- package/dist/utils/testing/helpers.d.ts +0 -3
- package/dist/utils/testing/helpers.js +0 -3
- package/document_loaders/base.cjs +0 -1
- package/document_loaders/base.d.cts +0 -1
- package/document_loaders/base.d.ts +0 -1
- package/document_loaders/base.js +0 -1
- package/document_loaders/langsmith.cjs +0 -1
- package/document_loaders/langsmith.d.cts +0 -1
- package/document_loaders/langsmith.d.ts +0 -1
- package/document_loaders/langsmith.js +0 -1
- package/documents.cjs +0 -1
- package/documents.d.cts +0 -1
- package/documents.d.ts +0 -1
- package/documents.js +0 -1
- package/embeddings.cjs +0 -1
- package/embeddings.d.cts +0 -1
- package/embeddings.d.ts +0 -1
- package/embeddings.js +0 -1
- package/example_selectors.cjs +0 -1
- package/example_selectors.d.cts +0 -1
- package/example_selectors.d.ts +0 -1
- package/example_selectors.js +0 -1
- package/indexing.cjs +0 -1
- package/indexing.d.cts +0 -1
- package/indexing.d.ts +0 -1
- package/indexing.js +0 -1
- package/language_models/base.cjs +0 -1
- package/language_models/base.d.cts +0 -1
- package/language_models/base.d.ts +0 -1
- package/language_models/base.js +0 -1
- package/language_models/chat_models.cjs +0 -1
- package/language_models/chat_models.d.cts +0 -1
- package/language_models/chat_models.d.ts +0 -1
- package/language_models/chat_models.js +0 -1
- package/language_models/llms.cjs +0 -1
- package/language_models/llms.d.cts +0 -1
- package/language_models/llms.d.ts +0 -1
- package/language_models/llms.js +0 -1
- package/load/serializable.cjs +0 -1
- package/load/serializable.d.cts +0 -1
- package/load/serializable.d.ts +0 -1
- package/load/serializable.js +0 -1
- package/load.cjs +0 -1
- package/load.d.cts +0 -1
- package/load.d.ts +0 -1
- package/load.js +0 -1
- package/memory.cjs +0 -1
- package/memory.d.cts +0 -1
- package/memory.d.ts +0 -1
- package/memory.js +0 -1
- package/messages/tool.cjs +0 -1
- package/messages/tool.d.cts +0 -1
- package/messages/tool.d.ts +0 -1
- package/messages/tool.js +0 -1
- package/messages.cjs +0 -1
- package/messages.d.cts +0 -1
- package/messages.d.ts +0 -1
- package/messages.js +0 -1
- package/output_parsers/openai_functions.cjs +0 -1
- package/output_parsers/openai_functions.d.cts +0 -1
- package/output_parsers/openai_functions.d.ts +0 -1
- package/output_parsers/openai_functions.js +0 -1
- package/output_parsers/openai_tools.cjs +0 -1
- package/output_parsers/openai_tools.d.cts +0 -1
- package/output_parsers/openai_tools.d.ts +0 -1
- package/output_parsers/openai_tools.js +0 -1
- package/output_parsers.cjs +0 -1
- package/output_parsers.d.cts +0 -1
- package/output_parsers.d.ts +0 -1
- package/output_parsers.js +0 -1
- package/outputs.cjs +0 -1
- package/outputs.d.cts +0 -1
- package/outputs.d.ts +0 -1
- package/outputs.js +0 -1
- package/prompt_values.cjs +0 -1
- package/prompt_values.d.cts +0 -1
- package/prompt_values.d.ts +0 -1
- package/prompt_values.js +0 -1
- package/prompts.cjs +0 -1
- package/prompts.d.cts +0 -1
- package/prompts.d.ts +0 -1
- package/prompts.js +0 -1
- package/retrievers/document_compressors.cjs +0 -1
- package/retrievers/document_compressors.d.cts +0 -1
- package/retrievers/document_compressors.d.ts +0 -1
- package/retrievers/document_compressors.js +0 -1
- package/retrievers.cjs +0 -1
- package/retrievers.d.cts +0 -1
- package/retrievers.d.ts +0 -1
- package/retrievers.js +0 -1
- package/runnables/graph.cjs +0 -1
- package/runnables/graph.d.cts +0 -1
- package/runnables/graph.d.ts +0 -1
- package/runnables/graph.js +0 -1
- package/runnables/remote.cjs +0 -1
- package/runnables/remote.d.cts +0 -1
- package/runnables/remote.d.ts +0 -1
- package/runnables/remote.js +0 -1
- package/runnables.cjs +0 -1
- package/runnables.d.cts +0 -1
- package/runnables.d.ts +0 -1
- package/runnables.js +0 -1
- package/singletons.cjs +0 -1
- package/singletons.d.cts +0 -1
- package/singletons.d.ts +0 -1
- package/singletons.js +0 -1
- package/stores.cjs +0 -1
- package/stores.d.cts +0 -1
- package/stores.d.ts +0 -1
- package/stores.js +0 -1
- package/structured_query.cjs +0 -1
- package/structured_query.d.cts +0 -1
- package/structured_query.d.ts +0 -1
- package/structured_query.js +0 -1
- package/tools.cjs +0 -1
- package/tools.d.cts +0 -1
- package/tools.d.ts +0 -1
- package/tools.js +0 -1
- package/tracers/base.cjs +0 -1
- package/tracers/base.d.cts +0 -1
- package/tracers/base.d.ts +0 -1
- package/tracers/base.js +0 -1
- package/tracers/console.cjs +0 -1
- package/tracers/console.d.cts +0 -1
- package/tracers/console.d.ts +0 -1
- package/tracers/console.js +0 -1
- package/tracers/initialize.cjs +0 -1
- package/tracers/initialize.d.cts +0 -1
- package/tracers/initialize.d.ts +0 -1
- package/tracers/initialize.js +0 -1
- package/tracers/log_stream.cjs +0 -1
- package/tracers/log_stream.d.cts +0 -1
- package/tracers/log_stream.d.ts +0 -1
- package/tracers/log_stream.js +0 -1
- package/tracers/run_collector.cjs +0 -1
- package/tracers/run_collector.d.cts +0 -1
- package/tracers/run_collector.d.ts +0 -1
- package/tracers/run_collector.js +0 -1
- package/tracers/tracer_langchain.cjs +0 -1
- package/tracers/tracer_langchain.d.cts +0 -1
- package/tracers/tracer_langchain.d.ts +0 -1
- package/tracers/tracer_langchain.js +0 -1
- package/tracers/tracer_langchain_v1.cjs +0 -1
- package/tracers/tracer_langchain_v1.d.cts +0 -1
- package/tracers/tracer_langchain_v1.d.ts +0 -1
- package/tracers/tracer_langchain_v1.js +0 -1
- package/types/stream.cjs +0 -1
- package/types/stream.d.cts +0 -1
- package/types/stream.d.ts +0 -1
- package/types/stream.js +0 -1
- package/utils/async_caller.cjs +0 -1
- package/utils/async_caller.d.cts +0 -1
- package/utils/async_caller.d.ts +0 -1
- package/utils/async_caller.js +0 -1
- package/utils/chunk_array.cjs +0 -1
- package/utils/chunk_array.d.cts +0 -1
- package/utils/chunk_array.d.ts +0 -1
- package/utils/chunk_array.js +0 -1
- package/utils/env.cjs +0 -1
- package/utils/env.d.cts +0 -1
- package/utils/env.d.ts +0 -1
- package/utils/env.js +0 -1
- package/utils/event_source_parse.cjs +0 -1
- package/utils/event_source_parse.d.cts +0 -1
- package/utils/event_source_parse.d.ts +0 -1
- package/utils/event_source_parse.js +0 -1
- package/utils/function_calling.cjs +0 -1
- package/utils/function_calling.d.cts +0 -1
- package/utils/function_calling.d.ts +0 -1
- package/utils/function_calling.js +0 -1
- package/utils/hash/insecure.cjs +0 -1
- package/utils/hash/insecure.d.cts +0 -1
- package/utils/hash/insecure.d.ts +0 -1
- package/utils/hash/insecure.js +0 -1
- package/utils/hash/sha256.cjs +0 -1
- package/utils/hash/sha256.d.cts +0 -1
- package/utils/hash/sha256.d.ts +0 -1
- package/utils/hash/sha256.js +0 -1
- package/utils/hash.cjs +0 -1
- package/utils/hash.d.cts +0 -1
- package/utils/hash.d.ts +0 -1
- package/utils/hash.js +0 -1
- package/utils/json_patch.cjs +0 -1
- package/utils/json_patch.d.cts +0 -1
- package/utils/json_patch.d.ts +0 -1
- package/utils/json_patch.js +0 -1
- package/utils/json_schema.cjs +0 -1
- package/utils/json_schema.d.cts +0 -1
- package/utils/json_schema.d.ts +0 -1
- package/utils/json_schema.js +0 -1
- package/utils/math.cjs +0 -1
- package/utils/math.d.cts +0 -1
- package/utils/math.d.ts +0 -1
- package/utils/math.js +0 -1
- package/utils/stream.cjs +0 -1
- package/utils/stream.d.cts +0 -1
- package/utils/stream.d.ts +0 -1
- package/utils/stream.js +0 -1
- package/utils/testing.cjs +0 -1
- package/utils/testing.d.cts +0 -1
- package/utils/testing.d.ts +0 -1
- package/utils/testing.js +0 -1
- package/utils/tiktoken.cjs +0 -1
- package/utils/tiktoken.d.cts +0 -1
- package/utils/tiktoken.d.ts +0 -1
- package/utils/tiktoken.js +0 -1
- package/utils/types.cjs +0 -1
- package/utils/types.d.cts +0 -1
- package/utils/types.d.ts +0 -1
- package/utils/types.js +0 -1
- package/vectorstores.cjs +0 -1
- package/vectorstores.d.cts +0 -1
- package/vectorstores.d.ts +0 -1
- package/vectorstores.js +0 -1
package/dist/runnables/base.js
CHANGED
|
@@ -1,2420 +1,1815 @@
|
|
|
1
|
-
import { z } from "zod/v3";
|
|
2
|
-
import pRetry from "p-retry";
|
|
3
|
-
import { v4 as uuidv4 } from "uuid";
|
|
4
|
-
import { isTraceableFunction, } from "langsmith/singletons/traceable";
|
|
5
|
-
import { LogStreamCallbackHandler, RunLog, RunLogPatch, isLogStreamHandler, } from "../tracers/log_stream.js";
|
|
6
|
-
import { EventStreamCallbackHandler, isStreamEventsHandler, } from "../tracers/event_stream.js";
|
|
7
1
|
import { Serializable } from "../load/serializable.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
2
|
+
import { ToolInputParsingException, _isToolCall } from "../tools/utils.js";
|
|
3
|
+
import { AsyncLocalStorageProviderSingleton } from "../singletons/async_local_storage/index.js";
|
|
4
|
+
import "../singletons/index.js";
|
|
5
|
+
import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mergeConfigs, patchConfig, pickRunnableConfigKeys } from "./config.js";
|
|
6
|
+
import { getAbortSignalError, raceWithSignal } from "../utils/signal.js";
|
|
7
|
+
import { AsyncGeneratorWithSetup, IterableReadableStream, atee, concat, pipeGeneratorWithSetup } from "../utils/stream.js";
|
|
8
|
+
import { LogStreamCallbackHandler, RunLog, RunLogPatch, isLogStreamHandler } from "../tracers/log_stream.js";
|
|
9
|
+
import { EventStreamCallbackHandler, isStreamEventsHandler } from "../tracers/event_stream.js";
|
|
11
10
|
import { AsyncCaller } from "../utils/async_caller.js";
|
|
12
11
|
import { RootListenersTracer } from "../tracers/root_listener.js";
|
|
13
12
|
import { _RootEventFilter, isRunnableInterface } from "./utils.js";
|
|
14
|
-
import {
|
|
13
|
+
import { getSchemaDescription, interopParseAsync, isSimpleStringZodSchema } from "../utils/types/zod.js";
|
|
15
14
|
import { Graph } from "./graph.js";
|
|
16
15
|
import { convertToHttpEventStream } from "./wrappers.js";
|
|
17
|
-
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator
|
|
18
|
-
import {
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
typeof value === "object"
|
|
27
|
-
? value
|
|
28
|
-
: { [defaultKey]: value };
|
|
16
|
+
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator } from "./iter.js";
|
|
17
|
+
import { z } from "zod/v3";
|
|
18
|
+
import pRetry from "p-retry";
|
|
19
|
+
import { v4 } from "uuid";
|
|
20
|
+
import { isTraceableFunction } from "langsmith/singletons/traceable";
|
|
21
|
+
|
|
22
|
+
//#region src/runnables/base.ts
|
|
23
|
+
function _coerceToDict(value, defaultKey) {
|
|
24
|
+
return value && !Array.isArray(value) && !(value instanceof Date) && typeof value === "object" ? value : { [defaultKey]: value };
|
|
29
25
|
}
|
|
30
26
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
}
|
|
589
|
-
yield event;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
finally {
|
|
593
|
-
abortController.abort();
|
|
594
|
-
await runnableStreamConsumePromise;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
async *_streamEventsV1(input, options, streamOptions) {
|
|
598
|
-
let runLog;
|
|
599
|
-
let hasEncounteredStartEvent = false;
|
|
600
|
-
const config = ensureConfig(options);
|
|
601
|
-
const rootTags = config.tags ?? [];
|
|
602
|
-
const rootMetadata = config.metadata ?? {};
|
|
603
|
-
const rootName = config.runName ?? this.getName();
|
|
604
|
-
const logStreamCallbackHandler = new LogStreamCallbackHandler({
|
|
605
|
-
...streamOptions,
|
|
606
|
-
autoClose: false,
|
|
607
|
-
_schemaFormat: "streaming_events",
|
|
608
|
-
});
|
|
609
|
-
const rootEventFilter = new _RootEventFilter({
|
|
610
|
-
...streamOptions,
|
|
611
|
-
});
|
|
612
|
-
const logStream = this._streamLog(input, logStreamCallbackHandler, config);
|
|
613
|
-
for await (const log of logStream) {
|
|
614
|
-
if (!runLog) {
|
|
615
|
-
runLog = RunLog.fromRunLogPatch(log);
|
|
616
|
-
}
|
|
617
|
-
else {
|
|
618
|
-
runLog = runLog.concat(log);
|
|
619
|
-
}
|
|
620
|
-
if (runLog.state === undefined) {
|
|
621
|
-
throw new Error(`Internal error: "streamEvents" state is missing. Please open a bug report.`);
|
|
622
|
-
}
|
|
623
|
-
// Yield the start event for the root runnable if it hasn't been seen.
|
|
624
|
-
// The root run is never filtered out
|
|
625
|
-
if (!hasEncounteredStartEvent) {
|
|
626
|
-
hasEncounteredStartEvent = true;
|
|
627
|
-
const state = { ...runLog.state };
|
|
628
|
-
const event = {
|
|
629
|
-
run_id: state.id,
|
|
630
|
-
event: `on_${state.type}_start`,
|
|
631
|
-
name: rootName,
|
|
632
|
-
tags: rootTags,
|
|
633
|
-
metadata: rootMetadata,
|
|
634
|
-
data: {
|
|
635
|
-
input,
|
|
636
|
-
},
|
|
637
|
-
};
|
|
638
|
-
if (rootEventFilter.includeEvent(event, state.type)) {
|
|
639
|
-
yield event;
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
const paths = log.ops
|
|
643
|
-
.filter((op) => op.path.startsWith("/logs/"))
|
|
644
|
-
.map((op) => op.path.split("/")[2]);
|
|
645
|
-
const dedupedPaths = [...new Set(paths)];
|
|
646
|
-
for (const path of dedupedPaths) {
|
|
647
|
-
let eventType;
|
|
648
|
-
let data = {};
|
|
649
|
-
const logEntry = runLog.state.logs[path];
|
|
650
|
-
if (logEntry.end_time === undefined) {
|
|
651
|
-
if (logEntry.streamed_output.length > 0) {
|
|
652
|
-
eventType = "stream";
|
|
653
|
-
}
|
|
654
|
-
else {
|
|
655
|
-
eventType = "start";
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
else {
|
|
659
|
-
eventType = "end";
|
|
660
|
-
}
|
|
661
|
-
if (eventType === "start") {
|
|
662
|
-
// Include the inputs with the start event if they are available.
|
|
663
|
-
// Usually they will NOT be available for components that operate
|
|
664
|
-
// on streams, since those components stream the input and
|
|
665
|
-
// don't know its final value until the end of the stream.
|
|
666
|
-
if (logEntry.inputs !== undefined) {
|
|
667
|
-
data.input = logEntry.inputs;
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
else if (eventType === "end") {
|
|
671
|
-
if (logEntry.inputs !== undefined) {
|
|
672
|
-
data.input = logEntry.inputs;
|
|
673
|
-
}
|
|
674
|
-
data.output = logEntry.final_output;
|
|
675
|
-
}
|
|
676
|
-
else if (eventType === "stream") {
|
|
677
|
-
const chunkCount = logEntry.streamed_output.length;
|
|
678
|
-
if (chunkCount !== 1) {
|
|
679
|
-
throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${logEntry.name}"`);
|
|
680
|
-
}
|
|
681
|
-
data = { chunk: logEntry.streamed_output[0] };
|
|
682
|
-
// Clean up the stream, we don't need it anymore.
|
|
683
|
-
// And this avoids duplicates as well!
|
|
684
|
-
logEntry.streamed_output = [];
|
|
685
|
-
}
|
|
686
|
-
yield {
|
|
687
|
-
event: `on_${logEntry.type}_${eventType}`,
|
|
688
|
-
name: logEntry.name,
|
|
689
|
-
run_id: logEntry.id,
|
|
690
|
-
tags: logEntry.tags,
|
|
691
|
-
metadata: logEntry.metadata,
|
|
692
|
-
data,
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
// Finally, we take care of the streaming output from the root chain
|
|
696
|
-
// if there is any.
|
|
697
|
-
const { state } = runLog;
|
|
698
|
-
if (state.streamed_output.length > 0) {
|
|
699
|
-
const chunkCount = state.streamed_output.length;
|
|
700
|
-
if (chunkCount !== 1) {
|
|
701
|
-
throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${state.name}"`);
|
|
702
|
-
}
|
|
703
|
-
const data = { chunk: state.streamed_output[0] };
|
|
704
|
-
// Clean up the stream, we don't need it anymore.
|
|
705
|
-
state.streamed_output = [];
|
|
706
|
-
const event = {
|
|
707
|
-
event: `on_${state.type}_stream`,
|
|
708
|
-
run_id: state.id,
|
|
709
|
-
tags: rootTags,
|
|
710
|
-
metadata: rootMetadata,
|
|
711
|
-
name: rootName,
|
|
712
|
-
data,
|
|
713
|
-
};
|
|
714
|
-
if (rootEventFilter.includeEvent(event, state.type)) {
|
|
715
|
-
yield event;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
const state = runLog?.state;
|
|
720
|
-
if (state !== undefined) {
|
|
721
|
-
// Finally, yield the end event for the root runnable.
|
|
722
|
-
const event = {
|
|
723
|
-
event: `on_${state.type}_end`,
|
|
724
|
-
name: rootName,
|
|
725
|
-
run_id: state.id,
|
|
726
|
-
tags: rootTags,
|
|
727
|
-
metadata: rootMetadata,
|
|
728
|
-
data: {
|
|
729
|
-
output: state.final_output,
|
|
730
|
-
},
|
|
731
|
-
};
|
|
732
|
-
if (rootEventFilter.includeEvent(event, state.type))
|
|
733
|
-
yield event;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
737
|
-
static isRunnable(thing) {
|
|
738
|
-
return isRunnableInterface(thing);
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
742
|
-
* The Run object contains information about the run, including its id,
|
|
743
|
-
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
744
|
-
* added to the run.
|
|
745
|
-
*
|
|
746
|
-
* @param {Object} params - The object containing the callback functions.
|
|
747
|
-
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
748
|
-
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
749
|
-
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
750
|
-
*/
|
|
751
|
-
withListeners({ onStart, onEnd, onError, }) {
|
|
752
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
753
|
-
return new RunnableBinding({
|
|
754
|
-
bound: this,
|
|
755
|
-
config: {},
|
|
756
|
-
configFactories: [
|
|
757
|
-
(config) => ({
|
|
758
|
-
callbacks: [
|
|
759
|
-
new RootListenersTracer({
|
|
760
|
-
config,
|
|
761
|
-
onStart,
|
|
762
|
-
onEnd,
|
|
763
|
-
onError,
|
|
764
|
-
}),
|
|
765
|
-
],
|
|
766
|
-
}),
|
|
767
|
-
],
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
/**
|
|
771
|
-
* Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
|
|
772
|
-
* which contains the runnable, name, description and schema.
|
|
773
|
-
*
|
|
774
|
-
* @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
|
|
775
|
-
*
|
|
776
|
-
* @param fields
|
|
777
|
-
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
778
|
-
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
779
|
-
* @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
780
|
-
* @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
781
|
-
*/
|
|
782
|
-
asTool(fields) {
|
|
783
|
-
return convertRunnableToTool(this, fields);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
27
|
+
* A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
|
|
28
|
+
* transformed.
|
|
29
|
+
*/
|
|
30
|
+
var Runnable = class extends Serializable {
|
|
31
|
+
lc_runnable = true;
|
|
32
|
+
name;
|
|
33
|
+
getName(suffix) {
|
|
34
|
+
const name = this.name ?? this.constructor.lc_name() ?? this.constructor.name;
|
|
35
|
+
return suffix ? `${name}${suffix}` : name;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Bind arguments to a Runnable, returning a new Runnable.
|
|
39
|
+
* @param kwargs
|
|
40
|
+
* @returns A new RunnableBinding that, when invoked, will apply the bound args.
|
|
41
|
+
*
|
|
42
|
+
* @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
|
|
43
|
+
*/
|
|
44
|
+
bind(kwargs) {
|
|
45
|
+
return new RunnableBinding({
|
|
46
|
+
bound: this,
|
|
47
|
+
kwargs,
|
|
48
|
+
config: {}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Return a new Runnable that maps a list of inputs to a list of outputs,
|
|
53
|
+
* by calling invoke() with each input.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated This will be removed in the next breaking release.
|
|
56
|
+
*/
|
|
57
|
+
map() {
|
|
58
|
+
return new RunnableEach({ bound: this });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Add retry logic to an existing runnable.
|
|
62
|
+
* @param fields.stopAfterAttempt The number of attempts to retry.
|
|
63
|
+
* @param fields.onFailedAttempt A function that is called when a retry fails.
|
|
64
|
+
* @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
|
|
65
|
+
*/
|
|
66
|
+
withRetry(fields) {
|
|
67
|
+
return new RunnableRetry({
|
|
68
|
+
bound: this,
|
|
69
|
+
kwargs: {},
|
|
70
|
+
config: {},
|
|
71
|
+
maxAttemptNumber: fields?.stopAfterAttempt,
|
|
72
|
+
...fields
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Bind config to a Runnable, returning a new Runnable.
|
|
77
|
+
* @param config New configuration parameters to attach to the new runnable.
|
|
78
|
+
* @returns A new RunnableBinding with a config matching what's passed.
|
|
79
|
+
*/
|
|
80
|
+
withConfig(config) {
|
|
81
|
+
return new RunnableBinding({
|
|
82
|
+
bound: this,
|
|
83
|
+
config,
|
|
84
|
+
kwargs: {}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a new runnable from the current one that will try invoking
|
|
89
|
+
* other passed fallback runnables if the initial invocation fails.
|
|
90
|
+
* @param fields.fallbacks Other runnables to call if the runnable errors.
|
|
91
|
+
* @returns A new RunnableWithFallbacks.
|
|
92
|
+
*/
|
|
93
|
+
withFallbacks(fields) {
|
|
94
|
+
const fallbacks = Array.isArray(fields) ? fields : fields.fallbacks;
|
|
95
|
+
return new RunnableWithFallbacks({
|
|
96
|
+
runnable: this,
|
|
97
|
+
fallbacks
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
_getOptionsList(options, length = 0) {
|
|
101
|
+
if (Array.isArray(options) && options.length !== length) throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
|
|
102
|
+
if (Array.isArray(options)) return options.map(ensureConfig);
|
|
103
|
+
if (length > 1 && !Array.isArray(options) && options.runId) {
|
|
104
|
+
console.warn("Provided runId will be used only for the first element of the batch.");
|
|
105
|
+
const subsequent = Object.fromEntries(Object.entries(options).filter(([key]) => key !== "runId"));
|
|
106
|
+
return Array.from({ length }, (_, i) => ensureConfig(i === 0 ? options : subsequent));
|
|
107
|
+
}
|
|
108
|
+
return Array.from({ length }, () => ensureConfig(options));
|
|
109
|
+
}
|
|
110
|
+
async batch(inputs, options, batchOptions) {
|
|
111
|
+
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
112
|
+
const maxConcurrency = configList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
|
|
113
|
+
const caller = new AsyncCaller({
|
|
114
|
+
maxConcurrency,
|
|
115
|
+
onFailedAttempt: (e) => {
|
|
116
|
+
throw e;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
const batchCalls = inputs.map((input, i) => caller.call(async () => {
|
|
120
|
+
try {
|
|
121
|
+
const result = await this.invoke(input, configList[i]);
|
|
122
|
+
return result;
|
|
123
|
+
} catch (e) {
|
|
124
|
+
if (batchOptions?.returnExceptions) return e;
|
|
125
|
+
throw e;
|
|
126
|
+
}
|
|
127
|
+
}));
|
|
128
|
+
return Promise.all(batchCalls);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Default streaming implementation.
|
|
132
|
+
* Subclasses should override this method if they support streaming output.
|
|
133
|
+
* @param input
|
|
134
|
+
* @param options
|
|
135
|
+
*/
|
|
136
|
+
async *_streamIterator(input, options) {
|
|
137
|
+
yield this.invoke(input, options);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Stream output in chunks.
|
|
141
|
+
* @param input
|
|
142
|
+
* @param options
|
|
143
|
+
* @returns A readable stream that is also an iterable.
|
|
144
|
+
*/
|
|
145
|
+
async stream(input, options) {
|
|
146
|
+
const config = ensureConfig(options);
|
|
147
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
148
|
+
generator: this._streamIterator(input, config),
|
|
149
|
+
config
|
|
150
|
+
});
|
|
151
|
+
await wrappedGenerator.setup;
|
|
152
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
153
|
+
}
|
|
154
|
+
_separateRunnableConfigFromCallOptions(options) {
|
|
155
|
+
let runnableConfig;
|
|
156
|
+
if (options === void 0) runnableConfig = ensureConfig(options);
|
|
157
|
+
else runnableConfig = ensureConfig({
|
|
158
|
+
callbacks: options.callbacks,
|
|
159
|
+
tags: options.tags,
|
|
160
|
+
metadata: options.metadata,
|
|
161
|
+
runName: options.runName,
|
|
162
|
+
configurable: options.configurable,
|
|
163
|
+
recursionLimit: options.recursionLimit,
|
|
164
|
+
maxConcurrency: options.maxConcurrency,
|
|
165
|
+
runId: options.runId,
|
|
166
|
+
timeout: options.timeout,
|
|
167
|
+
signal: options.signal
|
|
168
|
+
});
|
|
169
|
+
const callOptions = { ...options };
|
|
170
|
+
delete callOptions.callbacks;
|
|
171
|
+
delete callOptions.tags;
|
|
172
|
+
delete callOptions.metadata;
|
|
173
|
+
delete callOptions.runName;
|
|
174
|
+
delete callOptions.configurable;
|
|
175
|
+
delete callOptions.recursionLimit;
|
|
176
|
+
delete callOptions.maxConcurrency;
|
|
177
|
+
delete callOptions.runId;
|
|
178
|
+
delete callOptions.timeout;
|
|
179
|
+
delete callOptions.signal;
|
|
180
|
+
return [runnableConfig, callOptions];
|
|
181
|
+
}
|
|
182
|
+
async _callWithConfig(func, input, options) {
|
|
183
|
+
const config = ensureConfig(options);
|
|
184
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
185
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, config?.runType, void 0, void 0, config?.runName ?? this.getName());
|
|
186
|
+
delete config.runId;
|
|
187
|
+
let output;
|
|
188
|
+
try {
|
|
189
|
+
const promise = func.call(this, input, config, runManager);
|
|
190
|
+
output = await raceWithSignal(promise, options?.signal);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
await runManager?.handleChainError(e);
|
|
193
|
+
throw e;
|
|
194
|
+
}
|
|
195
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
196
|
+
return output;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Internal method that handles batching and configuration for a runnable
|
|
200
|
+
* It takes a function, input values, and optional configuration, and
|
|
201
|
+
* returns a promise that resolves to the output values.
|
|
202
|
+
* @param func The function to be executed for each input value.
|
|
203
|
+
* @param input The input values to be processed.
|
|
204
|
+
* @param config Optional configuration for the function execution.
|
|
205
|
+
* @returns A promise that resolves to the output values.
|
|
206
|
+
*/
|
|
207
|
+
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
208
|
+
const optionsList = this._getOptionsList(options ?? {}, inputs.length);
|
|
209
|
+
const callbackManagers = await Promise.all(optionsList.map(getCallbackManagerForConfig));
|
|
210
|
+
const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
|
|
211
|
+
const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), optionsList[i].runId, optionsList[i].runType, void 0, void 0, optionsList[i].runName ?? this.getName());
|
|
212
|
+
delete optionsList[i].runId;
|
|
213
|
+
return handleStartRes;
|
|
214
|
+
}));
|
|
215
|
+
let outputs;
|
|
216
|
+
try {
|
|
217
|
+
const promise = func.call(this, inputs, optionsList, runManagers, batchOptions);
|
|
218
|
+
outputs = await raceWithSignal(promise, optionsList?.[0]?.signal);
|
|
219
|
+
} catch (e) {
|
|
220
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
221
|
+
throw e;
|
|
222
|
+
}
|
|
223
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(outputs, "output"))));
|
|
224
|
+
return outputs;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Helper method to transform an Iterator of Input values into an Iterator of
|
|
228
|
+
* Output values, with callbacks.
|
|
229
|
+
* Use this to implement `stream()` or `transform()` in Runnable subclasses.
|
|
230
|
+
*/
|
|
231
|
+
async *_transformStreamWithConfig(inputGenerator, transformer, options) {
|
|
232
|
+
let finalInput;
|
|
233
|
+
let finalInputSupported = true;
|
|
234
|
+
let finalOutput;
|
|
235
|
+
let finalOutputSupported = true;
|
|
236
|
+
const config = ensureConfig(options);
|
|
237
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
238
|
+
async function* wrapInputForTracing() {
|
|
239
|
+
for await (const chunk of inputGenerator) {
|
|
240
|
+
if (finalInputSupported) if (finalInput === void 0) finalInput = chunk;
|
|
241
|
+
else try {
|
|
242
|
+
finalInput = concat(finalInput, chunk);
|
|
243
|
+
} catch {
|
|
244
|
+
finalInput = void 0;
|
|
245
|
+
finalInputSupported = false;
|
|
246
|
+
}
|
|
247
|
+
yield chunk;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
let runManager;
|
|
251
|
+
try {
|
|
252
|
+
const pipe = await pipeGeneratorWithSetup(transformer.bind(this), wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, config.runId, config.runType, void 0, void 0, config.runName ?? this.getName()), options?.signal, config);
|
|
253
|
+
delete config.runId;
|
|
254
|
+
runManager = pipe.setup;
|
|
255
|
+
const streamEventsHandler = runManager?.handlers.find(isStreamEventsHandler);
|
|
256
|
+
let iterator = pipe.output;
|
|
257
|
+
if (streamEventsHandler !== void 0 && runManager !== void 0) iterator = streamEventsHandler.tapOutputIterable(runManager.runId, iterator);
|
|
258
|
+
const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
|
|
259
|
+
if (streamLogHandler !== void 0 && runManager !== void 0) iterator = streamLogHandler.tapOutputIterable(runManager.runId, iterator);
|
|
260
|
+
for await (const chunk of iterator) {
|
|
261
|
+
yield chunk;
|
|
262
|
+
if (finalOutputSupported) if (finalOutput === void 0) finalOutput = chunk;
|
|
263
|
+
else try {
|
|
264
|
+
finalOutput = concat(finalOutput, chunk);
|
|
265
|
+
} catch {
|
|
266
|
+
finalOutput = void 0;
|
|
267
|
+
finalOutputSupported = false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} catch (e) {
|
|
271
|
+
await runManager?.handleChainError(e, void 0, void 0, void 0, { inputs: _coerceToDict(finalInput, "input") });
|
|
272
|
+
throw e;
|
|
273
|
+
}
|
|
274
|
+
await runManager?.handleChainEnd(finalOutput ?? {}, void 0, void 0, void 0, { inputs: _coerceToDict(finalInput, "input") });
|
|
275
|
+
}
|
|
276
|
+
getGraph(_) {
|
|
277
|
+
const graph = new Graph();
|
|
278
|
+
const inputNode = graph.addNode({
|
|
279
|
+
name: `${this.getName()}Input`,
|
|
280
|
+
schema: z.any()
|
|
281
|
+
});
|
|
282
|
+
const runnableNode = graph.addNode(this);
|
|
283
|
+
const outputNode = graph.addNode({
|
|
284
|
+
name: `${this.getName()}Output`,
|
|
285
|
+
schema: z.any()
|
|
286
|
+
});
|
|
287
|
+
graph.addEdge(inputNode, runnableNode);
|
|
288
|
+
graph.addEdge(runnableNode, outputNode);
|
|
289
|
+
return graph;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Create a new runnable sequence that runs each individual runnable in series,
|
|
293
|
+
* piping the output of one runnable into another runnable or runnable-like.
|
|
294
|
+
* @param coerceable A runnable, function, or object whose values are functions or runnables.
|
|
295
|
+
* @returns A new runnable sequence.
|
|
296
|
+
*/
|
|
297
|
+
pipe(coerceable) {
|
|
298
|
+
return new RunnableSequence({
|
|
299
|
+
first: this,
|
|
300
|
+
last: _coerceToRunnable(coerceable)
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Pick keys from the dict output of this runnable. Returns a new runnable.
|
|
305
|
+
*/
|
|
306
|
+
pick(keys) {
|
|
307
|
+
return this.pipe(new RunnablePick(keys));
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Assigns new fields to the dict output of this runnable. Returns a new runnable.
|
|
311
|
+
*/
|
|
312
|
+
assign(mapping) {
|
|
313
|
+
return this.pipe(new RunnableAssign(new RunnableMap({ steps: mapping })));
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Default implementation of transform, which buffers input and then calls stream.
|
|
317
|
+
* Subclasses should override this method if they can start producing output while
|
|
318
|
+
* input is still being generated.
|
|
319
|
+
* @param generator
|
|
320
|
+
* @param options
|
|
321
|
+
*/
|
|
322
|
+
async *transform(generator, options) {
|
|
323
|
+
let finalChunk;
|
|
324
|
+
for await (const chunk of generator) if (finalChunk === void 0) finalChunk = chunk;
|
|
325
|
+
else finalChunk = concat(finalChunk, chunk);
|
|
326
|
+
yield* this._streamIterator(finalChunk, ensureConfig(options));
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Stream all output from a runnable, as reported to the callback system.
|
|
330
|
+
* This includes all inner runs of LLMs, Retrievers, Tools, etc.
|
|
331
|
+
* Output is streamed as Log objects, which include a list of
|
|
332
|
+
* jsonpatch ops that describe how the state of the run has changed in each
|
|
333
|
+
* step, and the final state of the run.
|
|
334
|
+
* The jsonpatch ops can be applied in order to construct state.
|
|
335
|
+
* @param input
|
|
336
|
+
* @param options
|
|
337
|
+
* @param streamOptions
|
|
338
|
+
*/
|
|
339
|
+
async *streamLog(input, options, streamOptions) {
|
|
340
|
+
const logStreamCallbackHandler = new LogStreamCallbackHandler({
|
|
341
|
+
...streamOptions,
|
|
342
|
+
autoClose: false,
|
|
343
|
+
_schemaFormat: "original"
|
|
344
|
+
});
|
|
345
|
+
const config = ensureConfig(options);
|
|
346
|
+
yield* this._streamLog(input, logStreamCallbackHandler, config);
|
|
347
|
+
}
|
|
348
|
+
async *_streamLog(input, logStreamCallbackHandler, config) {
|
|
349
|
+
const { callbacks } = config;
|
|
350
|
+
if (callbacks === void 0) config.callbacks = [logStreamCallbackHandler];
|
|
351
|
+
else if (Array.isArray(callbacks)) config.callbacks = callbacks.concat([logStreamCallbackHandler]);
|
|
352
|
+
else {
|
|
353
|
+
const copiedCallbacks = callbacks.copy();
|
|
354
|
+
copiedCallbacks.addHandler(logStreamCallbackHandler, true);
|
|
355
|
+
config.callbacks = copiedCallbacks;
|
|
356
|
+
}
|
|
357
|
+
const runnableStreamPromise = this.stream(input, config);
|
|
358
|
+
async function consumeRunnableStream() {
|
|
359
|
+
try {
|
|
360
|
+
const runnableStream = await runnableStreamPromise;
|
|
361
|
+
for await (const chunk of runnableStream) {
|
|
362
|
+
const patch = new RunLogPatch({ ops: [{
|
|
363
|
+
op: "add",
|
|
364
|
+
path: "/streamed_output/-",
|
|
365
|
+
value: chunk
|
|
366
|
+
}] });
|
|
367
|
+
await logStreamCallbackHandler.writer.write(patch);
|
|
368
|
+
}
|
|
369
|
+
} finally {
|
|
370
|
+
await logStreamCallbackHandler.writer.close();
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
const runnableStreamConsumePromise = consumeRunnableStream();
|
|
374
|
+
try {
|
|
375
|
+
for await (const log of logStreamCallbackHandler) yield log;
|
|
376
|
+
} finally {
|
|
377
|
+
await runnableStreamConsumePromise;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
streamEvents(input, options, streamOptions) {
|
|
381
|
+
let stream;
|
|
382
|
+
if (options.version === "v1") stream = this._streamEventsV1(input, options, streamOptions);
|
|
383
|
+
else if (options.version === "v2") stream = this._streamEventsV2(input, options, streamOptions);
|
|
384
|
+
else throw new Error(`Only versions "v1" and "v2" of the schema are currently supported.`);
|
|
385
|
+
if (options.encoding === "text/event-stream") return convertToHttpEventStream(stream);
|
|
386
|
+
else return IterableReadableStream.fromAsyncGenerator(stream);
|
|
387
|
+
}
|
|
388
|
+
async *_streamEventsV2(input, options, streamOptions) {
|
|
389
|
+
const eventStreamer = new EventStreamCallbackHandler({
|
|
390
|
+
...streamOptions,
|
|
391
|
+
autoClose: false
|
|
392
|
+
});
|
|
393
|
+
const config = ensureConfig(options);
|
|
394
|
+
const runId = config.runId ?? v4();
|
|
395
|
+
config.runId = runId;
|
|
396
|
+
const callbacks = config.callbacks;
|
|
397
|
+
if (callbacks === void 0) config.callbacks = [eventStreamer];
|
|
398
|
+
else if (Array.isArray(callbacks)) config.callbacks = callbacks.concat(eventStreamer);
|
|
399
|
+
else {
|
|
400
|
+
const copiedCallbacks = callbacks.copy();
|
|
401
|
+
copiedCallbacks.addHandler(eventStreamer, true);
|
|
402
|
+
config.callbacks = copiedCallbacks;
|
|
403
|
+
}
|
|
404
|
+
const abortController = new AbortController();
|
|
405
|
+
const outerThis = this;
|
|
406
|
+
async function consumeRunnableStream() {
|
|
407
|
+
let signal;
|
|
408
|
+
let listener = null;
|
|
409
|
+
try {
|
|
410
|
+
if (options?.signal) if ("any" in AbortSignal) signal = AbortSignal.any([abortController.signal, options.signal]);
|
|
411
|
+
else {
|
|
412
|
+
signal = options.signal;
|
|
413
|
+
listener = () => {
|
|
414
|
+
abortController.abort();
|
|
415
|
+
};
|
|
416
|
+
options.signal.addEventListener("abort", listener, { once: true });
|
|
417
|
+
}
|
|
418
|
+
else signal = abortController.signal;
|
|
419
|
+
const runnableStream = await outerThis.stream(input, {
|
|
420
|
+
...config,
|
|
421
|
+
signal
|
|
422
|
+
});
|
|
423
|
+
const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
|
|
424
|
+
for await (const _ of tappedStream) if (abortController.signal.aborted) break;
|
|
425
|
+
} finally {
|
|
426
|
+
await eventStreamer.finish();
|
|
427
|
+
if (signal && listener) signal.removeEventListener("abort", listener);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const runnableStreamConsumePromise = consumeRunnableStream();
|
|
431
|
+
let firstEventSent = false;
|
|
432
|
+
let firstEventRunId;
|
|
433
|
+
try {
|
|
434
|
+
for await (const event of eventStreamer) {
|
|
435
|
+
if (!firstEventSent) {
|
|
436
|
+
event.data.input = input;
|
|
437
|
+
firstEventSent = true;
|
|
438
|
+
firstEventRunId = event.run_id;
|
|
439
|
+
yield event;
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (event.run_id === firstEventRunId && event.event.endsWith("_end")) {
|
|
443
|
+
if (event.data?.input) delete event.data.input;
|
|
444
|
+
}
|
|
445
|
+
yield event;
|
|
446
|
+
}
|
|
447
|
+
} finally {
|
|
448
|
+
abortController.abort();
|
|
449
|
+
await runnableStreamConsumePromise;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
async *_streamEventsV1(input, options, streamOptions) {
|
|
453
|
+
let runLog;
|
|
454
|
+
let hasEncounteredStartEvent = false;
|
|
455
|
+
const config = ensureConfig(options);
|
|
456
|
+
const rootTags = config.tags ?? [];
|
|
457
|
+
const rootMetadata = config.metadata ?? {};
|
|
458
|
+
const rootName = config.runName ?? this.getName();
|
|
459
|
+
const logStreamCallbackHandler = new LogStreamCallbackHandler({
|
|
460
|
+
...streamOptions,
|
|
461
|
+
autoClose: false,
|
|
462
|
+
_schemaFormat: "streaming_events"
|
|
463
|
+
});
|
|
464
|
+
const rootEventFilter = new _RootEventFilter({ ...streamOptions });
|
|
465
|
+
const logStream = this._streamLog(input, logStreamCallbackHandler, config);
|
|
466
|
+
for await (const log of logStream) {
|
|
467
|
+
if (!runLog) runLog = RunLog.fromRunLogPatch(log);
|
|
468
|
+
else runLog = runLog.concat(log);
|
|
469
|
+
if (runLog.state === void 0) throw new Error(`Internal error: "streamEvents" state is missing. Please open a bug report.`);
|
|
470
|
+
if (!hasEncounteredStartEvent) {
|
|
471
|
+
hasEncounteredStartEvent = true;
|
|
472
|
+
const state$2 = { ...runLog.state };
|
|
473
|
+
const event = {
|
|
474
|
+
run_id: state$2.id,
|
|
475
|
+
event: `on_${state$2.type}_start`,
|
|
476
|
+
name: rootName,
|
|
477
|
+
tags: rootTags,
|
|
478
|
+
metadata: rootMetadata,
|
|
479
|
+
data: { input }
|
|
480
|
+
};
|
|
481
|
+
if (rootEventFilter.includeEvent(event, state$2.type)) yield event;
|
|
482
|
+
}
|
|
483
|
+
const paths = log.ops.filter((op) => op.path.startsWith("/logs/")).map((op) => op.path.split("/")[2]);
|
|
484
|
+
const dedupedPaths = [...new Set(paths)];
|
|
485
|
+
for (const path of dedupedPaths) {
|
|
486
|
+
let eventType;
|
|
487
|
+
let data = {};
|
|
488
|
+
const logEntry = runLog.state.logs[path];
|
|
489
|
+
if (logEntry.end_time === void 0) if (logEntry.streamed_output.length > 0) eventType = "stream";
|
|
490
|
+
else eventType = "start";
|
|
491
|
+
else eventType = "end";
|
|
492
|
+
if (eventType === "start") {
|
|
493
|
+
if (logEntry.inputs !== void 0) data.input = logEntry.inputs;
|
|
494
|
+
} else if (eventType === "end") {
|
|
495
|
+
if (logEntry.inputs !== void 0) data.input = logEntry.inputs;
|
|
496
|
+
data.output = logEntry.final_output;
|
|
497
|
+
} else if (eventType === "stream") {
|
|
498
|
+
const chunkCount = logEntry.streamed_output.length;
|
|
499
|
+
if (chunkCount !== 1) throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${logEntry.name}"`);
|
|
500
|
+
data = { chunk: logEntry.streamed_output[0] };
|
|
501
|
+
logEntry.streamed_output = [];
|
|
502
|
+
}
|
|
503
|
+
yield {
|
|
504
|
+
event: `on_${logEntry.type}_${eventType}`,
|
|
505
|
+
name: logEntry.name,
|
|
506
|
+
run_id: logEntry.id,
|
|
507
|
+
tags: logEntry.tags,
|
|
508
|
+
metadata: logEntry.metadata,
|
|
509
|
+
data
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
const { state: state$1 } = runLog;
|
|
513
|
+
if (state$1.streamed_output.length > 0) {
|
|
514
|
+
const chunkCount = state$1.streamed_output.length;
|
|
515
|
+
if (chunkCount !== 1) throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${state$1.name}"`);
|
|
516
|
+
const data = { chunk: state$1.streamed_output[0] };
|
|
517
|
+
state$1.streamed_output = [];
|
|
518
|
+
const event = {
|
|
519
|
+
event: `on_${state$1.type}_stream`,
|
|
520
|
+
run_id: state$1.id,
|
|
521
|
+
tags: rootTags,
|
|
522
|
+
metadata: rootMetadata,
|
|
523
|
+
name: rootName,
|
|
524
|
+
data
|
|
525
|
+
};
|
|
526
|
+
if (rootEventFilter.includeEvent(event, state$1.type)) yield event;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const state = runLog?.state;
|
|
530
|
+
if (state !== void 0) {
|
|
531
|
+
const event = {
|
|
532
|
+
event: `on_${state.type}_end`,
|
|
533
|
+
name: rootName,
|
|
534
|
+
run_id: state.id,
|
|
535
|
+
tags: rootTags,
|
|
536
|
+
metadata: rootMetadata,
|
|
537
|
+
data: { output: state.final_output }
|
|
538
|
+
};
|
|
539
|
+
if (rootEventFilter.includeEvent(event, state.type)) yield event;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
static isRunnable(thing) {
|
|
543
|
+
return isRunnableInterface(thing);
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
547
|
+
* The Run object contains information about the run, including its id,
|
|
548
|
+
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
549
|
+
* added to the run.
|
|
550
|
+
*
|
|
551
|
+
* @param {Object} params - The object containing the callback functions.
|
|
552
|
+
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
553
|
+
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
554
|
+
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
555
|
+
*/
|
|
556
|
+
withListeners({ onStart, onEnd, onError }) {
|
|
557
|
+
return new RunnableBinding({
|
|
558
|
+
bound: this,
|
|
559
|
+
config: {},
|
|
560
|
+
configFactories: [(config) => ({ callbacks: [new RootListenersTracer({
|
|
561
|
+
config,
|
|
562
|
+
onStart,
|
|
563
|
+
onEnd,
|
|
564
|
+
onError
|
|
565
|
+
})] })]
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
|
|
570
|
+
* which contains the runnable, name, description and schema.
|
|
571
|
+
*
|
|
572
|
+
* @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
|
|
573
|
+
*
|
|
574
|
+
* @param fields
|
|
575
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
576
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
577
|
+
* @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
578
|
+
* @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
579
|
+
*/
|
|
580
|
+
asTool(fields) {
|
|
581
|
+
return convertRunnableToTool(this, fields);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
786
584
|
/**
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
...(await outerThis._mergeConfig(ensureConfig(options), outerThis.kwargs)),
|
|
942
|
-
version: options.version,
|
|
943
|
-
}, streamOptions);
|
|
944
|
-
};
|
|
945
|
-
return IterableReadableStream.fromAsyncGenerator(generator());
|
|
946
|
-
}
|
|
947
|
-
static isRunnableBinding(
|
|
948
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
949
|
-
thing
|
|
950
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
951
|
-
) {
|
|
952
|
-
return thing.bound && Runnable.isRunnable(thing.bound);
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
956
|
-
* The Run object contains information about the run, including its id,
|
|
957
|
-
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
958
|
-
* added to the run.
|
|
959
|
-
*
|
|
960
|
-
* @param {Object} params - The object containing the callback functions.
|
|
961
|
-
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
962
|
-
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
963
|
-
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
964
|
-
*/
|
|
965
|
-
withListeners({ onStart, onEnd, onError, }) {
|
|
966
|
-
return new RunnableBinding({
|
|
967
|
-
bound: this.bound,
|
|
968
|
-
kwargs: this.kwargs,
|
|
969
|
-
config: this.config,
|
|
970
|
-
configFactories: [
|
|
971
|
-
(config) => ({
|
|
972
|
-
callbacks: [
|
|
973
|
-
new RootListenersTracer({
|
|
974
|
-
config,
|
|
975
|
-
onStart,
|
|
976
|
-
onEnd,
|
|
977
|
-
onError,
|
|
978
|
-
}),
|
|
979
|
-
],
|
|
980
|
-
}),
|
|
981
|
-
],
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
}
|
|
585
|
+
* Wraps a runnable and applies partial config upon invocation.
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```typescript
|
|
589
|
+
* import {
|
|
590
|
+
* type RunnableConfig,
|
|
591
|
+
* RunnableLambda,
|
|
592
|
+
* } from "@langchain/core/runnables";
|
|
593
|
+
*
|
|
594
|
+
* const enhanceProfile = (
|
|
595
|
+
* profile: Record<string, any>,
|
|
596
|
+
* config?: RunnableConfig
|
|
597
|
+
* ) => {
|
|
598
|
+
* if (config?.configurable?.role) {
|
|
599
|
+
* return { ...profile, role: config.configurable.role };
|
|
600
|
+
* }
|
|
601
|
+
* return profile;
|
|
602
|
+
* };
|
|
603
|
+
*
|
|
604
|
+
* const runnable = RunnableLambda.from(enhanceProfile);
|
|
605
|
+
*
|
|
606
|
+
* // Bind configuration to the runnable to set the user's role dynamically
|
|
607
|
+
* const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
|
|
608
|
+
* const userRunnable = runnable.bind({ configurable: { role: "User" } });
|
|
609
|
+
*
|
|
610
|
+
* const result1 = await adminRunnable.invoke({
|
|
611
|
+
* name: "Alice",
|
|
612
|
+
* email: "alice@example.com"
|
|
613
|
+
* });
|
|
614
|
+
*
|
|
615
|
+
* // { name: "Alice", email: "alice@example.com", role: "Admin" }
|
|
616
|
+
*
|
|
617
|
+
* const result2 = await userRunnable.invoke({
|
|
618
|
+
* name: "Bob",
|
|
619
|
+
* email: "bob@example.com"
|
|
620
|
+
* });
|
|
621
|
+
*
|
|
622
|
+
* // { name: "Bob", email: "bob@example.com", role: "User" }
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
var RunnableBinding = class RunnableBinding extends Runnable {
|
|
626
|
+
static lc_name() {
|
|
627
|
+
return "RunnableBinding";
|
|
628
|
+
}
|
|
629
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
630
|
+
lc_serializable = true;
|
|
631
|
+
bound;
|
|
632
|
+
config;
|
|
633
|
+
kwargs;
|
|
634
|
+
configFactories;
|
|
635
|
+
constructor(fields) {
|
|
636
|
+
super(fields);
|
|
637
|
+
this.bound = fields.bound;
|
|
638
|
+
this.kwargs = fields.kwargs;
|
|
639
|
+
this.config = fields.config;
|
|
640
|
+
this.configFactories = fields.configFactories;
|
|
641
|
+
}
|
|
642
|
+
getName(suffix) {
|
|
643
|
+
return this.bound.getName(suffix);
|
|
644
|
+
}
|
|
645
|
+
async _mergeConfig(...options) {
|
|
646
|
+
const config = mergeConfigs(this.config, ...options);
|
|
647
|
+
return mergeConfigs(config, ...this.configFactories ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config))) : []);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Binds the runnable with the specified arguments.
|
|
651
|
+
* @param kwargs The arguments to bind the runnable with.
|
|
652
|
+
* @returns A new instance of the `RunnableBinding` class that is bound with the specified arguments.
|
|
653
|
+
*
|
|
654
|
+
* @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
|
|
655
|
+
*/
|
|
656
|
+
bind(kwargs) {
|
|
657
|
+
return new this.constructor({
|
|
658
|
+
bound: this.bound,
|
|
659
|
+
kwargs: {
|
|
660
|
+
...this.kwargs,
|
|
661
|
+
...kwargs
|
|
662
|
+
},
|
|
663
|
+
config: this.config
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
withConfig(config) {
|
|
667
|
+
return new this.constructor({
|
|
668
|
+
bound: this.bound,
|
|
669
|
+
kwargs: this.kwargs,
|
|
670
|
+
config: {
|
|
671
|
+
...this.config,
|
|
672
|
+
...config
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
withRetry(fields) {
|
|
677
|
+
return new RunnableRetry({
|
|
678
|
+
bound: this.bound,
|
|
679
|
+
kwargs: this.kwargs,
|
|
680
|
+
config: this.config,
|
|
681
|
+
maxAttemptNumber: fields?.stopAfterAttempt,
|
|
682
|
+
...fields
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
async invoke(input, options) {
|
|
686
|
+
return this.bound.invoke(input, await this._mergeConfig(ensureConfig(options), this.kwargs));
|
|
687
|
+
}
|
|
688
|
+
async batch(inputs, options, batchOptions) {
|
|
689
|
+
const mergedOptions = Array.isArray(options) ? await Promise.all(options.map(async (individualOption) => this._mergeConfig(ensureConfig(individualOption), this.kwargs))) : await this._mergeConfig(ensureConfig(options), this.kwargs);
|
|
690
|
+
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
691
|
+
}
|
|
692
|
+
async *_streamIterator(input, options) {
|
|
693
|
+
yield* this.bound._streamIterator(input, await this._mergeConfig(ensureConfig(options), this.kwargs));
|
|
694
|
+
}
|
|
695
|
+
async stream(input, options) {
|
|
696
|
+
return this.bound.stream(input, await this._mergeConfig(ensureConfig(options), this.kwargs));
|
|
697
|
+
}
|
|
698
|
+
async *transform(generator, options) {
|
|
699
|
+
yield* this.bound.transform(generator, await this._mergeConfig(ensureConfig(options), this.kwargs));
|
|
700
|
+
}
|
|
701
|
+
streamEvents(input, options, streamOptions) {
|
|
702
|
+
const outerThis = this;
|
|
703
|
+
const generator = async function* () {
|
|
704
|
+
yield* outerThis.bound.streamEvents(input, {
|
|
705
|
+
...await outerThis._mergeConfig(ensureConfig(options), outerThis.kwargs),
|
|
706
|
+
version: options.version
|
|
707
|
+
}, streamOptions);
|
|
708
|
+
};
|
|
709
|
+
return IterableReadableStream.fromAsyncGenerator(generator());
|
|
710
|
+
}
|
|
711
|
+
static isRunnableBinding(thing) {
|
|
712
|
+
return thing.bound && Runnable.isRunnable(thing.bound);
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
716
|
+
* The Run object contains information about the run, including its id,
|
|
717
|
+
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
718
|
+
* added to the run.
|
|
719
|
+
*
|
|
720
|
+
* @param {Object} params - The object containing the callback functions.
|
|
721
|
+
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
722
|
+
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
723
|
+
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
724
|
+
*/
|
|
725
|
+
withListeners({ onStart, onEnd, onError }) {
|
|
726
|
+
return new RunnableBinding({
|
|
727
|
+
bound: this.bound,
|
|
728
|
+
kwargs: this.kwargs,
|
|
729
|
+
config: this.config,
|
|
730
|
+
configFactories: [(config) => ({ callbacks: [new RootListenersTracer({
|
|
731
|
+
config,
|
|
732
|
+
onStart,
|
|
733
|
+
onEnd,
|
|
734
|
+
onError
|
|
735
|
+
})] })]
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
};
|
|
985
739
|
/**
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
* The Run object contains information about the run, including its id,
|
|
1068
|
-
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
1069
|
-
* added to the run.
|
|
1070
|
-
*
|
|
1071
|
-
* @param {Object} params - The object containing the callback functions.
|
|
1072
|
-
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
1073
|
-
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
1074
|
-
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
1075
|
-
*/
|
|
1076
|
-
withListeners({ onStart, onEnd, onError, }) {
|
|
1077
|
-
return new RunnableEach({
|
|
1078
|
-
bound: this.bound.withListeners({ onStart, onEnd, onError }),
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
740
|
+
* A runnable that delegates calls to another runnable
|
|
741
|
+
* with each element of the input sequence.
|
|
742
|
+
* @example
|
|
743
|
+
* ```typescript
|
|
744
|
+
* import { RunnableEach, RunnableLambda } from "@langchain/core/runnables";
|
|
745
|
+
*
|
|
746
|
+
* const toUpperCase = (input: string): string => input.toUpperCase();
|
|
747
|
+
* const addGreeting = (input: string): string => `Hello, ${input}!`;
|
|
748
|
+
*
|
|
749
|
+
* const upperCaseLambda = RunnableLambda.from(toUpperCase);
|
|
750
|
+
* const greetingLambda = RunnableLambda.from(addGreeting);
|
|
751
|
+
*
|
|
752
|
+
* const chain = new RunnableEach({
|
|
753
|
+
* bound: upperCaseLambda.pipe(greetingLambda),
|
|
754
|
+
* });
|
|
755
|
+
*
|
|
756
|
+
* const result = await chain.invoke(["alice", "bob", "carol"])
|
|
757
|
+
*
|
|
758
|
+
* // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
|
|
759
|
+
* ```
|
|
760
|
+
*
|
|
761
|
+
* @deprecated This will be removed in the next breaking release.
|
|
762
|
+
*/
|
|
763
|
+
var RunnableEach = class RunnableEach extends Runnable {
|
|
764
|
+
static lc_name() {
|
|
765
|
+
return "RunnableEach";
|
|
766
|
+
}
|
|
767
|
+
lc_serializable = true;
|
|
768
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
769
|
+
bound;
|
|
770
|
+
constructor(fields) {
|
|
771
|
+
super(fields);
|
|
772
|
+
this.bound = fields.bound;
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Binds the runnable with the specified arguments.
|
|
776
|
+
* @param kwargs The arguments to bind the runnable with.
|
|
777
|
+
* @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
|
|
778
|
+
*
|
|
779
|
+
* @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
|
|
780
|
+
*/
|
|
781
|
+
bind(kwargs) {
|
|
782
|
+
return new RunnableEach({ bound: this.bound.bind(kwargs) });
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Invokes the runnable with the specified input and configuration.
|
|
786
|
+
* @param input The input to invoke the runnable with.
|
|
787
|
+
* @param config The configuration to invoke the runnable with.
|
|
788
|
+
* @returns A promise that resolves to the output of the runnable.
|
|
789
|
+
*/
|
|
790
|
+
async invoke(inputs, config) {
|
|
791
|
+
return this._callWithConfig(this._invoke.bind(this), inputs, config);
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* A helper method that is used to invoke the runnable with the specified input and configuration.
|
|
795
|
+
* @param input The input to invoke the runnable with.
|
|
796
|
+
* @param config The configuration to invoke the runnable with.
|
|
797
|
+
* @returns A promise that resolves to the output of the runnable.
|
|
798
|
+
*/
|
|
799
|
+
async _invoke(inputs, config, runManager) {
|
|
800
|
+
return this.bound.batch(inputs, patchConfig(config, { callbacks: runManager?.getChild() }));
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
804
|
+
* The Run object contains information about the run, including its id,
|
|
805
|
+
* type, input, output, error, startTime, endTime, and any tags or metadata
|
|
806
|
+
* added to the run.
|
|
807
|
+
*
|
|
808
|
+
* @param {Object} params - The object containing the callback functions.
|
|
809
|
+
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
|
|
810
|
+
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
|
|
811
|
+
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
|
|
812
|
+
*/
|
|
813
|
+
withListeners({ onStart, onEnd, onError }) {
|
|
814
|
+
return new RunnableEach({ bound: this.bound.withListeners({
|
|
815
|
+
onStart,
|
|
816
|
+
onEnd,
|
|
817
|
+
onError
|
|
818
|
+
}) });
|
|
819
|
+
}
|
|
820
|
+
};
|
|
1082
821
|
/**
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
firstException = result;
|
|
1201
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1202
|
-
firstException.input = remainingInputs[i];
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
resultsMap[resultMapIndex.toString()] = result;
|
|
1206
|
-
}
|
|
1207
|
-
if (firstException) {
|
|
1208
|
-
throw firstException;
|
|
1209
|
-
}
|
|
1210
|
-
return results;
|
|
1211
|
-
}, {
|
|
1212
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1213
|
-
onFailedAttempt: (error) => this.onFailedAttempt(error, error.input),
|
|
1214
|
-
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
1215
|
-
randomize: true,
|
|
1216
|
-
});
|
|
1217
|
-
}
|
|
1218
|
-
catch (e) {
|
|
1219
|
-
if (batchOptions?.returnExceptions !== true) {
|
|
1220
|
-
throw e;
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
return Object.keys(resultsMap)
|
|
1224
|
-
.sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
|
|
1225
|
-
.map((key) => resultsMap[parseInt(key, 10)]);
|
|
1226
|
-
}
|
|
1227
|
-
async batch(inputs, options, batchOptions) {
|
|
1228
|
-
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
822
|
+
* Base class for runnables that can be retried a
|
|
823
|
+
* specified number of times.
|
|
824
|
+
* @example
|
|
825
|
+
* ```typescript
|
|
826
|
+
* import {
|
|
827
|
+
* RunnableLambda,
|
|
828
|
+
* RunnableRetry,
|
|
829
|
+
* } from "@langchain/core/runnables";
|
|
830
|
+
*
|
|
831
|
+
* // Simulate an API call that fails
|
|
832
|
+
* const simulateApiCall = (input: string): string => {
|
|
833
|
+
* console.log(`Attempting API call with input: ${input}`);
|
|
834
|
+
* throw new Error("API call failed due to network issue");
|
|
835
|
+
* };
|
|
836
|
+
*
|
|
837
|
+
* const apiCallLambda = RunnableLambda.from(simulateApiCall);
|
|
838
|
+
*
|
|
839
|
+
* // Apply retry logic using the .withRetry() method
|
|
840
|
+
* const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
|
|
841
|
+
*
|
|
842
|
+
* // Alternatively, create a RunnableRetry instance manually
|
|
843
|
+
* const manualRetry = new RunnableRetry({
|
|
844
|
+
* bound: apiCallLambda,
|
|
845
|
+
* maxAttemptNumber: 3,
|
|
846
|
+
* config: {},
|
|
847
|
+
* });
|
|
848
|
+
*
|
|
849
|
+
* // Example invocation using the .withRetry() method
|
|
850
|
+
* const res = await apiCallWithRetry
|
|
851
|
+
* .invoke("Request 1")
|
|
852
|
+
* .catch((error) => {
|
|
853
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
854
|
+
* });
|
|
855
|
+
*
|
|
856
|
+
* // Example invocation using the manual retry instance
|
|
857
|
+
* const res2 = await manualRetry
|
|
858
|
+
* .invoke("Request 2")
|
|
859
|
+
* .catch((error) => {
|
|
860
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
861
|
+
* });
|
|
862
|
+
* ```
|
|
863
|
+
*/
|
|
864
|
+
var RunnableRetry = class extends RunnableBinding {
|
|
865
|
+
static lc_name() {
|
|
866
|
+
return "RunnableRetry";
|
|
867
|
+
}
|
|
868
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
869
|
+
maxAttemptNumber = 3;
|
|
870
|
+
onFailedAttempt = () => {};
|
|
871
|
+
constructor(fields) {
|
|
872
|
+
super(fields);
|
|
873
|
+
this.maxAttemptNumber = fields.maxAttemptNumber ?? this.maxAttemptNumber;
|
|
874
|
+
this.onFailedAttempt = fields.onFailedAttempt ?? this.onFailedAttempt;
|
|
875
|
+
}
|
|
876
|
+
_patchConfigForRetry(attempt, config, runManager) {
|
|
877
|
+
const tag = attempt > 1 ? `retry:attempt:${attempt}` : void 0;
|
|
878
|
+
return patchConfig(config, { callbacks: runManager?.getChild(tag) });
|
|
879
|
+
}
|
|
880
|
+
async _invoke(input, config, runManager) {
|
|
881
|
+
return pRetry((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
|
|
882
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, input),
|
|
883
|
+
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
884
|
+
randomize: true
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Method that invokes the runnable with the specified input, run manager,
|
|
889
|
+
* and config. It handles the retry logic by catching any errors and
|
|
890
|
+
* recursively invoking itself with the updated config for the next retry
|
|
891
|
+
* attempt.
|
|
892
|
+
* @param input The input for the runnable.
|
|
893
|
+
* @param runManager The run manager for the runnable.
|
|
894
|
+
* @param config The config for the runnable.
|
|
895
|
+
* @returns A promise that resolves to the output of the runnable.
|
|
896
|
+
*/
|
|
897
|
+
async invoke(input, config) {
|
|
898
|
+
return this._callWithConfig(this._invoke.bind(this), input, config);
|
|
899
|
+
}
|
|
900
|
+
async _batch(inputs, configs, runManagers, batchOptions) {
|
|
901
|
+
const resultsMap = {};
|
|
902
|
+
try {
|
|
903
|
+
await pRetry(async (attemptNumber) => {
|
|
904
|
+
const remainingIndexes = inputs.map((_, i) => i).filter((i) => resultsMap[i.toString()] === void 0 || resultsMap[i.toString()] instanceof Error);
|
|
905
|
+
const remainingInputs = remainingIndexes.map((i) => inputs[i]);
|
|
906
|
+
const patchedConfigs = remainingIndexes.map((i) => this._patchConfigForRetry(attemptNumber, configs?.[i], runManagers?.[i]));
|
|
907
|
+
const results = await super.batch(remainingInputs, patchedConfigs, {
|
|
908
|
+
...batchOptions,
|
|
909
|
+
returnExceptions: true
|
|
910
|
+
});
|
|
911
|
+
let firstException;
|
|
912
|
+
for (let i = 0; i < results.length; i += 1) {
|
|
913
|
+
const result = results[i];
|
|
914
|
+
const resultMapIndex = remainingIndexes[i];
|
|
915
|
+
if (result instanceof Error) {
|
|
916
|
+
if (firstException === void 0) {
|
|
917
|
+
firstException = result;
|
|
918
|
+
firstException.input = remainingInputs[i];
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
resultsMap[resultMapIndex.toString()] = result;
|
|
922
|
+
}
|
|
923
|
+
if (firstException) throw firstException;
|
|
924
|
+
return results;
|
|
925
|
+
}, {
|
|
926
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, error.input),
|
|
927
|
+
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
928
|
+
randomize: true
|
|
929
|
+
});
|
|
930
|
+
} catch (e) {
|
|
931
|
+
if (batchOptions?.returnExceptions !== true) throw e;
|
|
932
|
+
}
|
|
933
|
+
return Object.keys(resultsMap).sort((a, b) => parseInt(a, 10) - parseInt(b, 10)).map((key) => resultsMap[parseInt(key, 10)]);
|
|
934
|
+
}
|
|
935
|
+
async batch(inputs, options, batchOptions) {
|
|
936
|
+
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
937
|
+
}
|
|
938
|
+
};
|
|
1231
939
|
/**
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
|
|
1401
|
-
}
|
|
1402
|
-
getGraph(config) {
|
|
1403
|
-
const graph = new Graph();
|
|
1404
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1405
|
-
let currentLastNode = null;
|
|
1406
|
-
this.steps.forEach((step, index) => {
|
|
1407
|
-
const stepGraph = step.getGraph(config);
|
|
1408
|
-
if (index !== 0) {
|
|
1409
|
-
stepGraph.trimFirstNode();
|
|
1410
|
-
}
|
|
1411
|
-
if (index !== this.steps.length - 1) {
|
|
1412
|
-
stepGraph.trimLastNode();
|
|
1413
|
-
}
|
|
1414
|
-
graph.extend(stepGraph);
|
|
1415
|
-
const stepFirstNode = stepGraph.firstNode();
|
|
1416
|
-
if (!stepFirstNode) {
|
|
1417
|
-
throw new Error(`Runnable ${step} has no first node`);
|
|
1418
|
-
}
|
|
1419
|
-
if (currentLastNode) {
|
|
1420
|
-
graph.addEdge(currentLastNode, stepFirstNode);
|
|
1421
|
-
}
|
|
1422
|
-
currentLastNode = stepGraph.lastNode();
|
|
1423
|
-
});
|
|
1424
|
-
return graph;
|
|
1425
|
-
}
|
|
1426
|
-
pipe(coerceable) {
|
|
1427
|
-
if (RunnableSequence.isRunnableSequence(coerceable)) {
|
|
1428
|
-
return new RunnableSequence({
|
|
1429
|
-
first: this.first,
|
|
1430
|
-
middle: this.middle.concat([
|
|
1431
|
-
this.last,
|
|
1432
|
-
coerceable.first,
|
|
1433
|
-
...coerceable.middle,
|
|
1434
|
-
]),
|
|
1435
|
-
last: coerceable.last,
|
|
1436
|
-
name: this.name ?? coerceable.name,
|
|
1437
|
-
});
|
|
1438
|
-
}
|
|
1439
|
-
else {
|
|
1440
|
-
return new RunnableSequence({
|
|
1441
|
-
first: this.first,
|
|
1442
|
-
middle: [...this.middle, this.last],
|
|
1443
|
-
last: _coerceToRunnable(coerceable),
|
|
1444
|
-
name: this.name,
|
|
1445
|
-
});
|
|
1446
|
-
}
|
|
1447
|
-
}
|
|
1448
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1449
|
-
static isRunnableSequence(thing) {
|
|
1450
|
-
return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
|
|
1451
|
-
}
|
|
1452
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1453
|
-
static from([first, ...runnables], nameOrFields) {
|
|
1454
|
-
let extra = {};
|
|
1455
|
-
if (typeof nameOrFields === "string") {
|
|
1456
|
-
extra.name = nameOrFields;
|
|
1457
|
-
}
|
|
1458
|
-
else if (nameOrFields !== undefined) {
|
|
1459
|
-
extra = nameOrFields;
|
|
1460
|
-
}
|
|
1461
|
-
return new RunnableSequence({
|
|
1462
|
-
...extra,
|
|
1463
|
-
first: _coerceToRunnable(first),
|
|
1464
|
-
middle: runnables.slice(0, -1).map(_coerceToRunnable),
|
|
1465
|
-
last: _coerceToRunnable(runnables[runnables.length - 1]),
|
|
1466
|
-
});
|
|
1467
|
-
}
|
|
1468
|
-
}
|
|
940
|
+
* A sequence of runnables, where the output of each is the input of the next.
|
|
941
|
+
* @example
|
|
942
|
+
* ```typescript
|
|
943
|
+
* const promptTemplate = PromptTemplate.fromTemplate(
|
|
944
|
+
* "Tell me a joke about {topic}",
|
|
945
|
+
* );
|
|
946
|
+
* const chain = RunnableSequence.from([promptTemplate, new ChatOpenAI({ model: "gpt-4o-mini" })]);
|
|
947
|
+
* const result = await chain.invoke({ topic: "bears" });
|
|
948
|
+
* ```
|
|
949
|
+
*/
|
|
950
|
+
var RunnableSequence = class RunnableSequence extends Runnable {
|
|
951
|
+
static lc_name() {
|
|
952
|
+
return "RunnableSequence";
|
|
953
|
+
}
|
|
954
|
+
first;
|
|
955
|
+
middle = [];
|
|
956
|
+
last;
|
|
957
|
+
omitSequenceTags = false;
|
|
958
|
+
lc_serializable = true;
|
|
959
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
960
|
+
constructor(fields) {
|
|
961
|
+
super(fields);
|
|
962
|
+
this.first = fields.first;
|
|
963
|
+
this.middle = fields.middle ?? this.middle;
|
|
964
|
+
this.last = fields.last;
|
|
965
|
+
this.name = fields.name;
|
|
966
|
+
this.omitSequenceTags = fields.omitSequenceTags ?? this.omitSequenceTags;
|
|
967
|
+
}
|
|
968
|
+
get steps() {
|
|
969
|
+
return [
|
|
970
|
+
this.first,
|
|
971
|
+
...this.middle,
|
|
972
|
+
this.last
|
|
973
|
+
];
|
|
974
|
+
}
|
|
975
|
+
async invoke(input, options) {
|
|
976
|
+
const config = ensureConfig(options);
|
|
977
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
978
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, void 0, void 0, void 0, config?.runName);
|
|
979
|
+
delete config.runId;
|
|
980
|
+
let nextStepInput = input;
|
|
981
|
+
let finalOutput;
|
|
982
|
+
try {
|
|
983
|
+
const initialSteps = [this.first, ...this.middle];
|
|
984
|
+
for (let i = 0; i < initialSteps.length; i += 1) {
|
|
985
|
+
const step = initialSteps[i];
|
|
986
|
+
const promise = step.invoke(nextStepInput, patchConfig(config, { callbacks: runManager?.getChild(this.omitSequenceTags ? void 0 : `seq:step:${i + 1}`) }));
|
|
987
|
+
nextStepInput = await raceWithSignal(promise, options?.signal);
|
|
988
|
+
}
|
|
989
|
+
if (options?.signal?.aborted) throw getAbortSignalError(options.signal);
|
|
990
|
+
finalOutput = await this.last.invoke(nextStepInput, patchConfig(config, { callbacks: runManager?.getChild(this.omitSequenceTags ? void 0 : `seq:step:${this.steps.length}`) }));
|
|
991
|
+
} catch (e) {
|
|
992
|
+
await runManager?.handleChainError(e);
|
|
993
|
+
throw e;
|
|
994
|
+
}
|
|
995
|
+
await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
|
|
996
|
+
return finalOutput;
|
|
997
|
+
}
|
|
998
|
+
async batch(inputs, options, batchOptions) {
|
|
999
|
+
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
1000
|
+
const callbackManagers = await Promise.all(configList.map(getCallbackManagerForConfig));
|
|
1001
|
+
const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
|
|
1002
|
+
const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, void 0, void 0, void 0, configList[i].runName);
|
|
1003
|
+
delete configList[i].runId;
|
|
1004
|
+
return handleStartRes;
|
|
1005
|
+
}));
|
|
1006
|
+
let nextStepInputs = inputs;
|
|
1007
|
+
try {
|
|
1008
|
+
for (let i = 0; i < this.steps.length; i += 1) {
|
|
1009
|
+
const step = this.steps[i];
|
|
1010
|
+
const promise = step.batch(nextStepInputs, runManagers.map((runManager, j) => {
|
|
1011
|
+
const childRunManager = runManager?.getChild(this.omitSequenceTags ? void 0 : `seq:step:${i + 1}`);
|
|
1012
|
+
return patchConfig(configList[j], { callbacks: childRunManager });
|
|
1013
|
+
}), batchOptions);
|
|
1014
|
+
nextStepInputs = await raceWithSignal(promise, configList[0]?.signal);
|
|
1015
|
+
}
|
|
1016
|
+
} catch (e) {
|
|
1017
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
1018
|
+
throw e;
|
|
1019
|
+
}
|
|
1020
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(nextStepInputs, "output"))));
|
|
1021
|
+
return nextStepInputs;
|
|
1022
|
+
}
|
|
1023
|
+
async *_streamIterator(input, options) {
|
|
1024
|
+
const callbackManager_ = await getCallbackManagerForConfig(options);
|
|
1025
|
+
const { runId,...otherOptions } = options ?? {};
|
|
1026
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, void 0, void 0, void 0, otherOptions?.runName);
|
|
1027
|
+
const steps = [
|
|
1028
|
+
this.first,
|
|
1029
|
+
...this.middle,
|
|
1030
|
+
this.last
|
|
1031
|
+
];
|
|
1032
|
+
let concatSupported = true;
|
|
1033
|
+
let finalOutput;
|
|
1034
|
+
async function* inputGenerator() {
|
|
1035
|
+
yield input;
|
|
1036
|
+
}
|
|
1037
|
+
try {
|
|
1038
|
+
let finalGenerator = steps[0].transform(inputGenerator(), patchConfig(otherOptions, { callbacks: runManager?.getChild(this.omitSequenceTags ? void 0 : `seq:step:1`) }));
|
|
1039
|
+
for (let i = 1; i < steps.length; i += 1) {
|
|
1040
|
+
const step = steps[i];
|
|
1041
|
+
finalGenerator = await step.transform(finalGenerator, patchConfig(otherOptions, { callbacks: runManager?.getChild(this.omitSequenceTags ? void 0 : `seq:step:${i + 1}`) }));
|
|
1042
|
+
}
|
|
1043
|
+
for await (const chunk of finalGenerator) {
|
|
1044
|
+
options?.signal?.throwIfAborted();
|
|
1045
|
+
yield chunk;
|
|
1046
|
+
if (concatSupported) if (finalOutput === void 0) finalOutput = chunk;
|
|
1047
|
+
else try {
|
|
1048
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1049
|
+
} catch (e) {
|
|
1050
|
+
finalOutput = void 0;
|
|
1051
|
+
concatSupported = false;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
} catch (e) {
|
|
1055
|
+
await runManager?.handleChainError(e);
|
|
1056
|
+
throw e;
|
|
1057
|
+
}
|
|
1058
|
+
await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
|
|
1059
|
+
}
|
|
1060
|
+
getGraph(config) {
|
|
1061
|
+
const graph = new Graph();
|
|
1062
|
+
let currentLastNode = null;
|
|
1063
|
+
this.steps.forEach((step, index) => {
|
|
1064
|
+
const stepGraph = step.getGraph(config);
|
|
1065
|
+
if (index !== 0) stepGraph.trimFirstNode();
|
|
1066
|
+
if (index !== this.steps.length - 1) stepGraph.trimLastNode();
|
|
1067
|
+
graph.extend(stepGraph);
|
|
1068
|
+
const stepFirstNode = stepGraph.firstNode();
|
|
1069
|
+
if (!stepFirstNode) throw new Error(`Runnable ${step} has no first node`);
|
|
1070
|
+
if (currentLastNode) graph.addEdge(currentLastNode, stepFirstNode);
|
|
1071
|
+
currentLastNode = stepGraph.lastNode();
|
|
1072
|
+
});
|
|
1073
|
+
return graph;
|
|
1074
|
+
}
|
|
1075
|
+
pipe(coerceable) {
|
|
1076
|
+
if (RunnableSequence.isRunnableSequence(coerceable)) return new RunnableSequence({
|
|
1077
|
+
first: this.first,
|
|
1078
|
+
middle: this.middle.concat([
|
|
1079
|
+
this.last,
|
|
1080
|
+
coerceable.first,
|
|
1081
|
+
...coerceable.middle
|
|
1082
|
+
]),
|
|
1083
|
+
last: coerceable.last,
|
|
1084
|
+
name: this.name ?? coerceable.name
|
|
1085
|
+
});
|
|
1086
|
+
else return new RunnableSequence({
|
|
1087
|
+
first: this.first,
|
|
1088
|
+
middle: [...this.middle, this.last],
|
|
1089
|
+
last: _coerceToRunnable(coerceable),
|
|
1090
|
+
name: this.name
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
static isRunnableSequence(thing) {
|
|
1094
|
+
return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
|
|
1095
|
+
}
|
|
1096
|
+
static from([first, ...runnables], nameOrFields) {
|
|
1097
|
+
let extra = {};
|
|
1098
|
+
if (typeof nameOrFields === "string") extra.name = nameOrFields;
|
|
1099
|
+
else if (nameOrFields !== void 0) extra = nameOrFields;
|
|
1100
|
+
return new RunnableSequence({
|
|
1101
|
+
...extra,
|
|
1102
|
+
first: _coerceToRunnable(first),
|
|
1103
|
+
middle: runnables.slice(0, -1).map(_coerceToRunnable),
|
|
1104
|
+
last: _coerceToRunnable(runnables[runnables.length - 1])
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1469
1108
|
/**
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
tasks.delete(key);
|
|
1563
|
-
if (!result.done) {
|
|
1564
|
-
yield { [key]: result.value };
|
|
1565
|
-
tasks.set(key, gen.next().then((result) => ({ key, gen, result })));
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
transform(generator, options) {
|
|
1570
|
-
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1571
|
-
}
|
|
1572
|
-
async stream(input, options) {
|
|
1573
|
-
async function* generator() {
|
|
1574
|
-
yield input;
|
|
1575
|
-
}
|
|
1576
|
-
const config = ensureConfig(options);
|
|
1577
|
-
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1578
|
-
generator: this.transform(generator(), config),
|
|
1579
|
-
config,
|
|
1580
|
-
});
|
|
1581
|
-
await wrappedGenerator.setup;
|
|
1582
|
-
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1583
|
-
}
|
|
1584
|
-
}
|
|
1109
|
+
* A runnable that runs a mapping of runnables in parallel,
|
|
1110
|
+
* and returns a mapping of their outputs.
|
|
1111
|
+
* @example
|
|
1112
|
+
* ```typescript
|
|
1113
|
+
* const mapChain = RunnableMap.from({
|
|
1114
|
+
* joke: PromptTemplate.fromTemplate("Tell me a joke about {topic}").pipe(
|
|
1115
|
+
* new ChatAnthropic({}),
|
|
1116
|
+
* ),
|
|
1117
|
+
* poem: PromptTemplate.fromTemplate("write a 2-line poem about {topic}").pipe(
|
|
1118
|
+
* new ChatAnthropic({}),
|
|
1119
|
+
* ),
|
|
1120
|
+
* });
|
|
1121
|
+
* const result = await mapChain.invoke({ topic: "bear" });
|
|
1122
|
+
* ```
|
|
1123
|
+
*/
|
|
1124
|
+
var RunnableMap = class RunnableMap extends Runnable {
|
|
1125
|
+
static lc_name() {
|
|
1126
|
+
return "RunnableMap";
|
|
1127
|
+
}
|
|
1128
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1129
|
+
lc_serializable = true;
|
|
1130
|
+
steps;
|
|
1131
|
+
getStepsKeys() {
|
|
1132
|
+
return Object.keys(this.steps);
|
|
1133
|
+
}
|
|
1134
|
+
constructor(fields) {
|
|
1135
|
+
super(fields);
|
|
1136
|
+
this.steps = {};
|
|
1137
|
+
for (const [key, value] of Object.entries(fields.steps)) this.steps[key] = _coerceToRunnable(value);
|
|
1138
|
+
}
|
|
1139
|
+
static from(steps) {
|
|
1140
|
+
return new RunnableMap({ steps });
|
|
1141
|
+
}
|
|
1142
|
+
async invoke(input, options) {
|
|
1143
|
+
const config = ensureConfig(options);
|
|
1144
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
1145
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), { input }, config.runId, void 0, void 0, void 0, config?.runName);
|
|
1146
|
+
delete config.runId;
|
|
1147
|
+
const output = {};
|
|
1148
|
+
try {
|
|
1149
|
+
const promises = Object.entries(this.steps).map(async ([key, runnable]) => {
|
|
1150
|
+
output[key] = await runnable.invoke(input, patchConfig(config, { callbacks: runManager?.getChild(`map:key:${key}`) }));
|
|
1151
|
+
});
|
|
1152
|
+
await raceWithSignal(Promise.all(promises), options?.signal);
|
|
1153
|
+
} catch (e) {
|
|
1154
|
+
await runManager?.handleChainError(e);
|
|
1155
|
+
throw e;
|
|
1156
|
+
}
|
|
1157
|
+
await runManager?.handleChainEnd(output);
|
|
1158
|
+
return output;
|
|
1159
|
+
}
|
|
1160
|
+
async *_transform(generator, runManager, options) {
|
|
1161
|
+
const steps = { ...this.steps };
|
|
1162
|
+
const inputCopies = atee(generator, Object.keys(steps).length);
|
|
1163
|
+
const tasks = new Map(Object.entries(steps).map(([key, runnable], i) => {
|
|
1164
|
+
const gen = runnable.transform(inputCopies[i], patchConfig(options, { callbacks: runManager?.getChild(`map:key:${key}`) }));
|
|
1165
|
+
return [key, gen.next().then((result) => ({
|
|
1166
|
+
key,
|
|
1167
|
+
gen,
|
|
1168
|
+
result
|
|
1169
|
+
}))];
|
|
1170
|
+
}));
|
|
1171
|
+
while (tasks.size) {
|
|
1172
|
+
const promise = Promise.race(tasks.values());
|
|
1173
|
+
const { key, result, gen } = await raceWithSignal(promise, options?.signal);
|
|
1174
|
+
tasks.delete(key);
|
|
1175
|
+
if (!result.done) {
|
|
1176
|
+
yield { [key]: result.value };
|
|
1177
|
+
tasks.set(key, gen.next().then((result$1) => ({
|
|
1178
|
+
key,
|
|
1179
|
+
gen,
|
|
1180
|
+
result: result$1
|
|
1181
|
+
})));
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
transform(generator, options) {
|
|
1186
|
+
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1187
|
+
}
|
|
1188
|
+
async stream(input, options) {
|
|
1189
|
+
async function* generator() {
|
|
1190
|
+
yield input;
|
|
1191
|
+
}
|
|
1192
|
+
const config = ensureConfig(options);
|
|
1193
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1194
|
+
generator: this.transform(generator(), config),
|
|
1195
|
+
config
|
|
1196
|
+
});
|
|
1197
|
+
await wrappedGenerator.setup;
|
|
1198
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
1585
1201
|
/**
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
return;
|
|
1629
|
-
}
|
|
1630
|
-
if (isIterator(result)) {
|
|
1631
|
-
while (true) {
|
|
1632
|
-
config?.signal?.throwIfAborted();
|
|
1633
|
-
const state = result.next();
|
|
1634
|
-
if (state.done)
|
|
1635
|
-
break;
|
|
1636
|
-
yield state.value;
|
|
1637
|
-
}
|
|
1638
|
-
return;
|
|
1639
|
-
}
|
|
1640
|
-
yield result;
|
|
1641
|
-
}
|
|
1642
|
-
static from(func) {
|
|
1643
|
-
return new RunnableTraceable({ func });
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1202
|
+
* A runnable that wraps a traced LangSmith function.
|
|
1203
|
+
*/
|
|
1204
|
+
var RunnableTraceable = class RunnableTraceable extends Runnable {
|
|
1205
|
+
lc_serializable = false;
|
|
1206
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1207
|
+
func;
|
|
1208
|
+
constructor(fields) {
|
|
1209
|
+
super(fields);
|
|
1210
|
+
if (!isTraceableFunction(fields.func)) throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
1211
|
+
this.func = fields.func;
|
|
1212
|
+
}
|
|
1213
|
+
async invoke(input, options) {
|
|
1214
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
1215
|
+
const callbacks = await getCallbackManagerForConfig(config);
|
|
1216
|
+
const promise = this.func(patchConfig(config, { callbacks }), input);
|
|
1217
|
+
return raceWithSignal(promise, config?.signal);
|
|
1218
|
+
}
|
|
1219
|
+
async *_streamIterator(input, options) {
|
|
1220
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
1221
|
+
const result = await this.invoke(input, options);
|
|
1222
|
+
if (isAsyncIterable(result)) {
|
|
1223
|
+
for await (const item of result) {
|
|
1224
|
+
config?.signal?.throwIfAborted();
|
|
1225
|
+
yield item;
|
|
1226
|
+
}
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
if (isIterator(result)) {
|
|
1230
|
+
while (true) {
|
|
1231
|
+
config?.signal?.throwIfAborted();
|
|
1232
|
+
const state = result.next();
|
|
1233
|
+
if (state.done) break;
|
|
1234
|
+
yield state.value;
|
|
1235
|
+
}
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
yield result;
|
|
1239
|
+
}
|
|
1240
|
+
static from(func) {
|
|
1241
|
+
return new RunnableTraceable({ func });
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1646
1244
|
function assertNonTraceableFunction(func) {
|
|
1647
|
-
|
|
1648
|
-
throw new Error("RunnableLambda requires a function that is not wrapped in traceable higher-order function. This shouldn't happen.");
|
|
1649
|
-
}
|
|
1245
|
+
if (isTraceableFunction(func)) throw new Error("RunnableLambda requires a function that is not wrapped in traceable higher-order function. This shouldn't happen.");
|
|
1650
1246
|
}
|
|
1651
1247
|
/**
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
const childConfig = patchConfig(config, {
|
|
1798
|
-
callbacks: runManager?.getChild(),
|
|
1799
|
-
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1800
|
-
});
|
|
1801
|
-
const output = await new Promise((resolve, reject) => {
|
|
1802
|
-
void AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(childConfig), async () => {
|
|
1803
|
-
try {
|
|
1804
|
-
const res = await this.func(finalChunk, {
|
|
1805
|
-
...childConfig,
|
|
1806
|
-
config: childConfig,
|
|
1807
|
-
});
|
|
1808
|
-
resolve(res);
|
|
1809
|
-
}
|
|
1810
|
-
catch (e) {
|
|
1811
|
-
reject(e);
|
|
1812
|
-
}
|
|
1813
|
-
});
|
|
1814
|
-
});
|
|
1815
|
-
if (output && Runnable.isRunnable(output)) {
|
|
1816
|
-
if (config?.recursionLimit === 0) {
|
|
1817
|
-
throw new Error("Recursion limit reached.");
|
|
1818
|
-
}
|
|
1819
|
-
const stream = await output.stream(finalChunk, childConfig);
|
|
1820
|
-
for await (const chunk of stream) {
|
|
1821
|
-
yield chunk;
|
|
1822
|
-
}
|
|
1823
|
-
}
|
|
1824
|
-
else if (isAsyncIterable(output)) {
|
|
1825
|
-
for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1826
|
-
config?.signal?.throwIfAborted();
|
|
1827
|
-
yield chunk;
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
else if (isIterableIterator(output)) {
|
|
1831
|
-
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1832
|
-
config?.signal?.throwIfAborted();
|
|
1833
|
-
yield chunk;
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
else {
|
|
1837
|
-
yield output;
|
|
1838
|
-
}
|
|
1839
|
-
}
|
|
1840
|
-
transform(generator, options) {
|
|
1841
|
-
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1842
|
-
}
|
|
1843
|
-
async stream(input, options) {
|
|
1844
|
-
async function* generator() {
|
|
1845
|
-
yield input;
|
|
1846
|
-
}
|
|
1847
|
-
const config = ensureConfig(options);
|
|
1848
|
-
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1849
|
-
generator: this.transform(generator(), config),
|
|
1850
|
-
config,
|
|
1851
|
-
});
|
|
1852
|
-
await wrappedGenerator.setup;
|
|
1853
|
-
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1248
|
+
* A runnable that wraps an arbitrary function that takes a single argument.
|
|
1249
|
+
* @example
|
|
1250
|
+
* ```typescript
|
|
1251
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
1252
|
+
*
|
|
1253
|
+
* const add = (input: { x: number; y: number }) => input.x + input.y;
|
|
1254
|
+
*
|
|
1255
|
+
* const multiply = (input: { value: number; multiplier: number }) =>
|
|
1256
|
+
* input.value * input.multiplier;
|
|
1257
|
+
*
|
|
1258
|
+
* // Create runnables for the functions
|
|
1259
|
+
* const addLambda = RunnableLambda.from(add);
|
|
1260
|
+
* const multiplyLambda = RunnableLambda.from(multiply);
|
|
1261
|
+
*
|
|
1262
|
+
* // Chain the lambdas for a mathematical operation
|
|
1263
|
+
* const chainedLambda = addLambda.pipe((result) =>
|
|
1264
|
+
* multiplyLambda.invoke({ value: result, multiplier: 2 })
|
|
1265
|
+
* );
|
|
1266
|
+
*
|
|
1267
|
+
* // Example invocation of the chainedLambda
|
|
1268
|
+
* const result = await chainedLambda.invoke({ x: 2, y: 3 });
|
|
1269
|
+
*
|
|
1270
|
+
* // Will log "10" (since (2 + 3) * 2 = 10)
|
|
1271
|
+
* ```
|
|
1272
|
+
*/
|
|
1273
|
+
var RunnableLambda = class RunnableLambda extends Runnable {
|
|
1274
|
+
static lc_name() {
|
|
1275
|
+
return "RunnableLambda";
|
|
1276
|
+
}
|
|
1277
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1278
|
+
func;
|
|
1279
|
+
constructor(fields) {
|
|
1280
|
+
if (isTraceableFunction(fields.func)) return RunnableTraceable.from(fields.func);
|
|
1281
|
+
super(fields);
|
|
1282
|
+
assertNonTraceableFunction(fields.func);
|
|
1283
|
+
this.func = fields.func;
|
|
1284
|
+
}
|
|
1285
|
+
static from(func) {
|
|
1286
|
+
return new RunnableLambda({ func });
|
|
1287
|
+
}
|
|
1288
|
+
async _invoke(input, config, runManager) {
|
|
1289
|
+
return new Promise((resolve, reject) => {
|
|
1290
|
+
const childConfig = patchConfig(config, {
|
|
1291
|
+
callbacks: runManager?.getChild(),
|
|
1292
|
+
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1
|
|
1293
|
+
});
|
|
1294
|
+
AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(childConfig), async () => {
|
|
1295
|
+
try {
|
|
1296
|
+
let output = await this.func(input, { ...childConfig });
|
|
1297
|
+
if (output && Runnable.isRunnable(output)) {
|
|
1298
|
+
if (config?.recursionLimit === 0) throw new Error("Recursion limit reached.");
|
|
1299
|
+
output = await output.invoke(input, {
|
|
1300
|
+
...childConfig,
|
|
1301
|
+
recursionLimit: (childConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1
|
|
1302
|
+
});
|
|
1303
|
+
} else if (isAsyncIterable(output)) {
|
|
1304
|
+
let finalOutput;
|
|
1305
|
+
for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1306
|
+
config?.signal?.throwIfAborted();
|
|
1307
|
+
if (finalOutput === void 0) finalOutput = chunk;
|
|
1308
|
+
else try {
|
|
1309
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1310
|
+
} catch (e) {
|
|
1311
|
+
finalOutput = chunk;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
output = finalOutput;
|
|
1315
|
+
} else if (isIterableIterator(output)) {
|
|
1316
|
+
let finalOutput;
|
|
1317
|
+
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1318
|
+
config?.signal?.throwIfAborted();
|
|
1319
|
+
if (finalOutput === void 0) finalOutput = chunk;
|
|
1320
|
+
else try {
|
|
1321
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1322
|
+
} catch (e) {
|
|
1323
|
+
finalOutput = chunk;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
output = finalOutput;
|
|
1327
|
+
}
|
|
1328
|
+
resolve(output);
|
|
1329
|
+
} catch (e) {
|
|
1330
|
+
reject(e);
|
|
1331
|
+
}
|
|
1332
|
+
});
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
async invoke(input, options) {
|
|
1336
|
+
return this._callWithConfig(this._invoke.bind(this), input, options);
|
|
1337
|
+
}
|
|
1338
|
+
async *_transform(generator, runManager, config) {
|
|
1339
|
+
let finalChunk;
|
|
1340
|
+
for await (const chunk of generator) if (finalChunk === void 0) finalChunk = chunk;
|
|
1341
|
+
else try {
|
|
1342
|
+
finalChunk = concat(finalChunk, chunk);
|
|
1343
|
+
} catch (e) {
|
|
1344
|
+
finalChunk = chunk;
|
|
1345
|
+
}
|
|
1346
|
+
const childConfig = patchConfig(config, {
|
|
1347
|
+
callbacks: runManager?.getChild(),
|
|
1348
|
+
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1
|
|
1349
|
+
});
|
|
1350
|
+
const output = await new Promise((resolve, reject) => {
|
|
1351
|
+
AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(childConfig), async () => {
|
|
1352
|
+
try {
|
|
1353
|
+
const res = await this.func(finalChunk, {
|
|
1354
|
+
...childConfig,
|
|
1355
|
+
config: childConfig
|
|
1356
|
+
});
|
|
1357
|
+
resolve(res);
|
|
1358
|
+
} catch (e) {
|
|
1359
|
+
reject(e);
|
|
1360
|
+
}
|
|
1361
|
+
});
|
|
1362
|
+
});
|
|
1363
|
+
if (output && Runnable.isRunnable(output)) {
|
|
1364
|
+
if (config?.recursionLimit === 0) throw new Error("Recursion limit reached.");
|
|
1365
|
+
const stream = await output.stream(finalChunk, childConfig);
|
|
1366
|
+
for await (const chunk of stream) yield chunk;
|
|
1367
|
+
} else if (isAsyncIterable(output)) for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1368
|
+
config?.signal?.throwIfAborted();
|
|
1369
|
+
yield chunk;
|
|
1370
|
+
}
|
|
1371
|
+
else if (isIterableIterator(output)) for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1372
|
+
config?.signal?.throwIfAborted();
|
|
1373
|
+
yield chunk;
|
|
1374
|
+
}
|
|
1375
|
+
else yield output;
|
|
1376
|
+
}
|
|
1377
|
+
transform(generator, options) {
|
|
1378
|
+
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1379
|
+
}
|
|
1380
|
+
async stream(input, options) {
|
|
1381
|
+
async function* generator() {
|
|
1382
|
+
yield input;
|
|
1383
|
+
}
|
|
1384
|
+
const config = ensureConfig(options);
|
|
1385
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1386
|
+
generator: this.transform(generator(), config),
|
|
1387
|
+
config
|
|
1388
|
+
});
|
|
1389
|
+
await wrappedGenerator.setup;
|
|
1390
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1391
|
+
}
|
|
1392
|
+
};
|
|
1856
1393
|
/**
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
}
|
|
1394
|
+
* A runnable that runs a mapping of runnables in parallel,
|
|
1395
|
+
* and returns a mapping of their outputs.
|
|
1396
|
+
* @example
|
|
1397
|
+
* ```typescript
|
|
1398
|
+
* import {
|
|
1399
|
+
* RunnableLambda,
|
|
1400
|
+
* RunnableParallel,
|
|
1401
|
+
* } from "@langchain/core/runnables";
|
|
1402
|
+
*
|
|
1403
|
+
* const addYears = (age: number): number => age + 5;
|
|
1404
|
+
* const yearsToFifty = (age: number): number => 50 - age;
|
|
1405
|
+
* const yearsToHundred = (age: number): number => 100 - age;
|
|
1406
|
+
*
|
|
1407
|
+
* const addYearsLambda = RunnableLambda.from(addYears);
|
|
1408
|
+
* const milestoneFiftyLambda = RunnableLambda.from(yearsToFifty);
|
|
1409
|
+
* const milestoneHundredLambda = RunnableLambda.from(yearsToHundred);
|
|
1410
|
+
*
|
|
1411
|
+
* // Pipe will coerce objects into RunnableParallel by default, but we
|
|
1412
|
+
* // explicitly instantiate one here to demonstrate
|
|
1413
|
+
* const sequence = addYearsLambda.pipe(
|
|
1414
|
+
* RunnableParallel.from({
|
|
1415
|
+
* years_to_fifty: milestoneFiftyLambda,
|
|
1416
|
+
* years_to_hundred: milestoneHundredLambda,
|
|
1417
|
+
* })
|
|
1418
|
+
* );
|
|
1419
|
+
*
|
|
1420
|
+
* // Invoke the sequence with a single age input
|
|
1421
|
+
* const res = await sequence.invoke(25);
|
|
1422
|
+
*
|
|
1423
|
+
* // { years_to_fifty: 20, years_to_hundred: 70 }
|
|
1424
|
+
* ```
|
|
1425
|
+
*/
|
|
1426
|
+
var RunnableParallel = class extends RunnableMap {};
|
|
1891
1427
|
/**
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
catch (e) {
|
|
2073
|
-
await runManager?.handleChainError(e);
|
|
2074
|
-
throw e;
|
|
2075
|
-
}
|
|
2076
|
-
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
2077
|
-
}
|
|
2078
|
-
async batch(inputs, options, batchOptions) {
|
|
2079
|
-
if (batchOptions?.returnExceptions) {
|
|
2080
|
-
throw new Error("Not implemented.");
|
|
2081
|
-
}
|
|
2082
|
-
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
2083
|
-
const callbackManagers = await Promise.all(configList.map((config) => getCallbackManagerForConfig(config)));
|
|
2084
|
-
const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
|
|
2085
|
-
const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, undefined, undefined, undefined, configList[i].runName);
|
|
2086
|
-
delete configList[i].runId;
|
|
2087
|
-
return handleStartRes;
|
|
2088
|
-
}));
|
|
2089
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2090
|
-
let firstError;
|
|
2091
|
-
for (const runnable of this.runnables()) {
|
|
2092
|
-
configList[0].signal?.throwIfAborted();
|
|
2093
|
-
try {
|
|
2094
|
-
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => patchConfig(configList[j], {
|
|
2095
|
-
callbacks: runManager?.getChild(),
|
|
2096
|
-
})), batchOptions);
|
|
2097
|
-
await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
|
|
2098
|
-
return outputs;
|
|
2099
|
-
}
|
|
2100
|
-
catch (e) {
|
|
2101
|
-
if (firstError === undefined) {
|
|
2102
|
-
firstError = e;
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
|
-
}
|
|
2106
|
-
if (!firstError) {
|
|
2107
|
-
throw new Error("No error stored at end of fallbacks.");
|
|
2108
|
-
}
|
|
2109
|
-
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
|
|
2110
|
-
throw firstError;
|
|
2111
|
-
}
|
|
2112
|
-
}
|
|
2113
|
-
// TODO: Figure out why the compiler needs help eliminating Error as a RunOutput type
|
|
2114
|
-
export function _coerceToRunnable(coerceable) {
|
|
2115
|
-
if (typeof coerceable === "function") {
|
|
2116
|
-
return new RunnableLambda({ func: coerceable });
|
|
2117
|
-
}
|
|
2118
|
-
else if (Runnable.isRunnable(coerceable)) {
|
|
2119
|
-
return coerceable;
|
|
2120
|
-
}
|
|
2121
|
-
else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
|
|
2122
|
-
const runnables = {};
|
|
2123
|
-
for (const [key, value] of Object.entries(coerceable)) {
|
|
2124
|
-
runnables[key] = _coerceToRunnable(value);
|
|
2125
|
-
}
|
|
2126
|
-
return new RunnableMap({
|
|
2127
|
-
steps: runnables,
|
|
2128
|
-
});
|
|
2129
|
-
}
|
|
2130
|
-
else {
|
|
2131
|
-
throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
|
|
2132
|
-
}
|
|
1428
|
+
* A Runnable that can fallback to other Runnables if it fails.
|
|
1429
|
+
* External APIs (e.g., APIs for a language model) may at times experience
|
|
1430
|
+
* degraded performance or even downtime.
|
|
1431
|
+
*
|
|
1432
|
+
* In these cases, it can be useful to have a fallback Runnable that can be
|
|
1433
|
+
* used in place of the original Runnable (e.g., fallback to another LLM provider).
|
|
1434
|
+
*
|
|
1435
|
+
* Fallbacks can be defined at the level of a single Runnable, or at the level
|
|
1436
|
+
* of a chain of Runnables. Fallbacks are tried in order until one succeeds or
|
|
1437
|
+
* all fail.
|
|
1438
|
+
*
|
|
1439
|
+
* While you can instantiate a `RunnableWithFallbacks` directly, it is usually
|
|
1440
|
+
* more convenient to use the `withFallbacks` method on an existing Runnable.
|
|
1441
|
+
*
|
|
1442
|
+
* When streaming, fallbacks will only be called on failures during the initial
|
|
1443
|
+
* stream creation. Errors that occur after a stream starts will not fallback
|
|
1444
|
+
* to the next Runnable.
|
|
1445
|
+
*
|
|
1446
|
+
* @example
|
|
1447
|
+
* ```typescript
|
|
1448
|
+
* import {
|
|
1449
|
+
* RunnableLambda,
|
|
1450
|
+
* RunnableWithFallbacks,
|
|
1451
|
+
* } from "@langchain/core/runnables";
|
|
1452
|
+
*
|
|
1453
|
+
* const primaryOperation = (input: string): string => {
|
|
1454
|
+
* if (input !== "safe") {
|
|
1455
|
+
* throw new Error("Primary operation failed due to unsafe input");
|
|
1456
|
+
* }
|
|
1457
|
+
* return `Processed: ${input}`;
|
|
1458
|
+
* };
|
|
1459
|
+
*
|
|
1460
|
+
* // Define a fallback operation that processes the input differently
|
|
1461
|
+
* const fallbackOperation = (input: string): string =>
|
|
1462
|
+
* `Fallback processed: ${input}`;
|
|
1463
|
+
*
|
|
1464
|
+
* const primaryRunnable = RunnableLambda.from(primaryOperation);
|
|
1465
|
+
* const fallbackRunnable = RunnableLambda.from(fallbackOperation);
|
|
1466
|
+
*
|
|
1467
|
+
* // Apply the fallback logic using the .withFallbacks() method
|
|
1468
|
+
* const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
|
|
1469
|
+
*
|
|
1470
|
+
* // Alternatively, create a RunnableWithFallbacks instance manually
|
|
1471
|
+
* const manualFallbackChain = new RunnableWithFallbacks({
|
|
1472
|
+
* runnable: primaryRunnable,
|
|
1473
|
+
* fallbacks: [fallbackRunnable],
|
|
1474
|
+
* });
|
|
1475
|
+
*
|
|
1476
|
+
* // Example invocation using .withFallbacks()
|
|
1477
|
+
* const res = await runnableWithFallback
|
|
1478
|
+
* .invoke("unsafe input")
|
|
1479
|
+
* .catch((error) => {
|
|
1480
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1481
|
+
* });
|
|
1482
|
+
*
|
|
1483
|
+
* // "Fallback processed: unsafe input"
|
|
1484
|
+
*
|
|
1485
|
+
* // Example invocation using manual instantiation
|
|
1486
|
+
* const res = await manualFallbackChain
|
|
1487
|
+
* .invoke("safe")
|
|
1488
|
+
* .catch((error) => {
|
|
1489
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1490
|
+
* });
|
|
1491
|
+
*
|
|
1492
|
+
* // "Processed: safe"
|
|
1493
|
+
* ```
|
|
1494
|
+
*/
|
|
1495
|
+
var RunnableWithFallbacks = class extends Runnable {
|
|
1496
|
+
static lc_name() {
|
|
1497
|
+
return "RunnableWithFallbacks";
|
|
1498
|
+
}
|
|
1499
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1500
|
+
lc_serializable = true;
|
|
1501
|
+
runnable;
|
|
1502
|
+
fallbacks;
|
|
1503
|
+
constructor(fields) {
|
|
1504
|
+
super(fields);
|
|
1505
|
+
this.runnable = fields.runnable;
|
|
1506
|
+
this.fallbacks = fields.fallbacks;
|
|
1507
|
+
}
|
|
1508
|
+
*runnables() {
|
|
1509
|
+
yield this.runnable;
|
|
1510
|
+
for (const fallback of this.fallbacks) yield fallback;
|
|
1511
|
+
}
|
|
1512
|
+
async invoke(input, options) {
|
|
1513
|
+
const config = ensureConfig(options);
|
|
1514
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
1515
|
+
const { runId,...otherConfigFields } = config;
|
|
1516
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, void 0, void 0, void 0, otherConfigFields?.runName);
|
|
1517
|
+
const childConfig = patchConfig(otherConfigFields, { callbacks: runManager?.getChild() });
|
|
1518
|
+
const res = await AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1519
|
+
let firstError;
|
|
1520
|
+
for (const runnable of this.runnables()) {
|
|
1521
|
+
config?.signal?.throwIfAborted();
|
|
1522
|
+
try {
|
|
1523
|
+
const output = await runnable.invoke(input, childConfig);
|
|
1524
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1525
|
+
return output;
|
|
1526
|
+
} catch (e) {
|
|
1527
|
+
if (firstError === void 0) firstError = e;
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
if (firstError === void 0) throw new Error("No error stored at end of fallback.");
|
|
1531
|
+
await runManager?.handleChainError(firstError);
|
|
1532
|
+
throw firstError;
|
|
1533
|
+
});
|
|
1534
|
+
return res;
|
|
1535
|
+
}
|
|
1536
|
+
async *_streamIterator(input, options) {
|
|
1537
|
+
const config = ensureConfig(options);
|
|
1538
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
1539
|
+
const { runId,...otherConfigFields } = config;
|
|
1540
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, void 0, void 0, void 0, otherConfigFields?.runName);
|
|
1541
|
+
let firstError;
|
|
1542
|
+
let stream;
|
|
1543
|
+
for (const runnable of this.runnables()) {
|
|
1544
|
+
config?.signal?.throwIfAborted();
|
|
1545
|
+
const childConfig = patchConfig(otherConfigFields, { callbacks: runManager?.getChild() });
|
|
1546
|
+
try {
|
|
1547
|
+
const originalStream = await runnable.stream(input, childConfig);
|
|
1548
|
+
stream = consumeAsyncIterableInContext(childConfig, originalStream);
|
|
1549
|
+
break;
|
|
1550
|
+
} catch (e) {
|
|
1551
|
+
if (firstError === void 0) firstError = e;
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
if (stream === void 0) {
|
|
1555
|
+
const error = firstError ?? /* @__PURE__ */ new Error("No error stored at end of fallback.");
|
|
1556
|
+
await runManager?.handleChainError(error);
|
|
1557
|
+
throw error;
|
|
1558
|
+
}
|
|
1559
|
+
let output;
|
|
1560
|
+
try {
|
|
1561
|
+
for await (const chunk of stream) {
|
|
1562
|
+
yield chunk;
|
|
1563
|
+
try {
|
|
1564
|
+
output = output === void 0 ? output : concat(output, chunk);
|
|
1565
|
+
} catch (e) {
|
|
1566
|
+
output = void 0;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
} catch (e) {
|
|
1570
|
+
await runManager?.handleChainError(e);
|
|
1571
|
+
throw e;
|
|
1572
|
+
}
|
|
1573
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1574
|
+
}
|
|
1575
|
+
async batch(inputs, options, batchOptions) {
|
|
1576
|
+
if (batchOptions?.returnExceptions) throw new Error("Not implemented.");
|
|
1577
|
+
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
1578
|
+
const callbackManagers = await Promise.all(configList.map((config) => getCallbackManagerForConfig(config)));
|
|
1579
|
+
const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
|
|
1580
|
+
const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, void 0, void 0, void 0, configList[i].runName);
|
|
1581
|
+
delete configList[i].runId;
|
|
1582
|
+
return handleStartRes;
|
|
1583
|
+
}));
|
|
1584
|
+
let firstError;
|
|
1585
|
+
for (const runnable of this.runnables()) {
|
|
1586
|
+
configList[0].signal?.throwIfAborted();
|
|
1587
|
+
try {
|
|
1588
|
+
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => patchConfig(configList[j], { callbacks: runManager?.getChild() })), batchOptions);
|
|
1589
|
+
await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
|
|
1590
|
+
return outputs;
|
|
1591
|
+
} catch (e) {
|
|
1592
|
+
if (firstError === void 0) firstError = e;
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
if (!firstError) throw new Error("No error stored at end of fallbacks.");
|
|
1596
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
|
|
1597
|
+
throw firstError;
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
function _coerceToRunnable(coerceable) {
|
|
1601
|
+
if (typeof coerceable === "function") return new RunnableLambda({ func: coerceable });
|
|
1602
|
+
else if (Runnable.isRunnable(coerceable)) return coerceable;
|
|
1603
|
+
else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
|
|
1604
|
+
const runnables = {};
|
|
1605
|
+
for (const [key, value] of Object.entries(coerceable)) runnables[key] = _coerceToRunnable(value);
|
|
1606
|
+
return new RunnableMap({ steps: runnables });
|
|
1607
|
+
} else throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
|
|
2133
1608
|
}
|
|
2134
1609
|
/**
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
for await (const chunk of forPassthrough) {
|
|
2214
|
-
if (typeof chunk !== "object" || Array.isArray(chunk)) {
|
|
2215
|
-
throw new Error(`RunnableAssign can only be used with objects as input, got ${typeof chunk}`);
|
|
2216
|
-
}
|
|
2217
|
-
const filtered = Object.fromEntries(Object.entries(chunk).filter(([key]) => !mapperKeys.includes(key)));
|
|
2218
|
-
if (Object.keys(filtered).length > 0) {
|
|
2219
|
-
yield filtered;
|
|
2220
|
-
}
|
|
2221
|
-
}
|
|
2222
|
-
// yield the mapper output
|
|
2223
|
-
yield (await firstMapperChunkPromise).value;
|
|
2224
|
-
for await (const chunk of mapperOutput) {
|
|
2225
|
-
yield chunk;
|
|
2226
|
-
}
|
|
2227
|
-
}
|
|
2228
|
-
transform(generator, options) {
|
|
2229
|
-
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
2230
|
-
}
|
|
2231
|
-
async stream(input, options) {
|
|
2232
|
-
async function* generator() {
|
|
2233
|
-
yield input;
|
|
2234
|
-
}
|
|
2235
|
-
const config = ensureConfig(options);
|
|
2236
|
-
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
2237
|
-
generator: this.transform(generator(), config),
|
|
2238
|
-
config,
|
|
2239
|
-
});
|
|
2240
|
-
await wrappedGenerator.setup;
|
|
2241
|
-
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
2242
|
-
}
|
|
2243
|
-
}
|
|
1610
|
+
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
1611
|
+
* @example
|
|
1612
|
+
* ```typescript
|
|
1613
|
+
* import {
|
|
1614
|
+
* RunnableAssign,
|
|
1615
|
+
* RunnableLambda,
|
|
1616
|
+
* RunnableParallel,
|
|
1617
|
+
* } from "@langchain/core/runnables";
|
|
1618
|
+
*
|
|
1619
|
+
* const calculateAge = (x: { birthYear: number }): { age: number } => {
|
|
1620
|
+
* const currentYear = new Date().getFullYear();
|
|
1621
|
+
* return { age: currentYear - x.birthYear };
|
|
1622
|
+
* };
|
|
1623
|
+
*
|
|
1624
|
+
* const createGreeting = (x: { name: string }): { greeting: string } => {
|
|
1625
|
+
* return { greeting: `Hello, ${x.name}!` };
|
|
1626
|
+
* };
|
|
1627
|
+
*
|
|
1628
|
+
* const mapper = RunnableParallel.from({
|
|
1629
|
+
* age_step: RunnableLambda.from(calculateAge),
|
|
1630
|
+
* greeting_step: RunnableLambda.from(createGreeting),
|
|
1631
|
+
* });
|
|
1632
|
+
*
|
|
1633
|
+
* const runnableAssign = new RunnableAssign({ mapper });
|
|
1634
|
+
*
|
|
1635
|
+
* const res = await runnableAssign.invoke({ name: "Alice", birthYear: 1990 });
|
|
1636
|
+
*
|
|
1637
|
+
* // { name: "Alice", birthYear: 1990, age_step: { age: 34 }, greeting_step: { greeting: "Hello, Alice!" } }
|
|
1638
|
+
* ```
|
|
1639
|
+
*/
|
|
1640
|
+
var RunnableAssign = class extends Runnable {
|
|
1641
|
+
static lc_name() {
|
|
1642
|
+
return "RunnableAssign";
|
|
1643
|
+
}
|
|
1644
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1645
|
+
lc_serializable = true;
|
|
1646
|
+
mapper;
|
|
1647
|
+
constructor(fields) {
|
|
1648
|
+
if (fields instanceof RunnableMap) fields = { mapper: fields };
|
|
1649
|
+
super(fields);
|
|
1650
|
+
this.mapper = fields.mapper;
|
|
1651
|
+
}
|
|
1652
|
+
async invoke(input, options) {
|
|
1653
|
+
const mapperResult = await this.mapper.invoke(input, options);
|
|
1654
|
+
return {
|
|
1655
|
+
...input,
|
|
1656
|
+
...mapperResult
|
|
1657
|
+
};
|
|
1658
|
+
}
|
|
1659
|
+
async *_transform(generator, runManager, options) {
|
|
1660
|
+
const mapperKeys = this.mapper.getStepsKeys();
|
|
1661
|
+
const [forPassthrough, forMapper] = atee(generator);
|
|
1662
|
+
const mapperOutput = this.mapper.transform(forMapper, patchConfig(options, { callbacks: runManager?.getChild() }));
|
|
1663
|
+
const firstMapperChunkPromise = mapperOutput.next();
|
|
1664
|
+
for await (const chunk of forPassthrough) {
|
|
1665
|
+
if (typeof chunk !== "object" || Array.isArray(chunk)) throw new Error(`RunnableAssign can only be used with objects as input, got ${typeof chunk}`);
|
|
1666
|
+
const filtered = Object.fromEntries(Object.entries(chunk).filter(([key]) => !mapperKeys.includes(key)));
|
|
1667
|
+
if (Object.keys(filtered).length > 0) yield filtered;
|
|
1668
|
+
}
|
|
1669
|
+
yield (await firstMapperChunkPromise).value;
|
|
1670
|
+
for await (const chunk of mapperOutput) yield chunk;
|
|
1671
|
+
}
|
|
1672
|
+
transform(generator, options) {
|
|
1673
|
+
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1674
|
+
}
|
|
1675
|
+
async stream(input, options) {
|
|
1676
|
+
async function* generator() {
|
|
1677
|
+
yield input;
|
|
1678
|
+
}
|
|
1679
|
+
const config = ensureConfig(options);
|
|
1680
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1681
|
+
generator: this.transform(generator(), config),
|
|
1682
|
+
config
|
|
1683
|
+
});
|
|
1684
|
+
await wrappedGenerator.setup;
|
|
1685
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
2244
1688
|
/**
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
}
|
|
2338
|
-
|
|
2339
|
-
constructor(fields) {
|
|
2340
|
-
const sequence = RunnableSequence.from([
|
|
2341
|
-
RunnableLambda.from(async (input) => {
|
|
2342
|
-
let toolInput;
|
|
2343
|
-
if (_isToolCall(input)) {
|
|
2344
|
-
try {
|
|
2345
|
-
toolInput = await interopParseAsync(this.schema, input.args);
|
|
2346
|
-
}
|
|
2347
|
-
catch (e) {
|
|
2348
|
-
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
2349
|
-
}
|
|
2350
|
-
}
|
|
2351
|
-
else {
|
|
2352
|
-
toolInput = input;
|
|
2353
|
-
}
|
|
2354
|
-
return toolInput;
|
|
2355
|
-
}).withConfig({ runName: `${fields.name}:parse_input` }),
|
|
2356
|
-
fields.bound,
|
|
2357
|
-
]).withConfig({ runName: fields.name });
|
|
2358
|
-
super({
|
|
2359
|
-
bound: sequence,
|
|
2360
|
-
config: fields.config ?? {},
|
|
2361
|
-
});
|
|
2362
|
-
Object.defineProperty(this, "name", {
|
|
2363
|
-
enumerable: true,
|
|
2364
|
-
configurable: true,
|
|
2365
|
-
writable: true,
|
|
2366
|
-
value: void 0
|
|
2367
|
-
});
|
|
2368
|
-
Object.defineProperty(this, "description", {
|
|
2369
|
-
enumerable: true,
|
|
2370
|
-
configurable: true,
|
|
2371
|
-
writable: true,
|
|
2372
|
-
value: void 0
|
|
2373
|
-
});
|
|
2374
|
-
Object.defineProperty(this, "schema", {
|
|
2375
|
-
enumerable: true,
|
|
2376
|
-
configurable: true,
|
|
2377
|
-
writable: true,
|
|
2378
|
-
value: void 0
|
|
2379
|
-
});
|
|
2380
|
-
this.name = fields.name;
|
|
2381
|
-
this.description = fields.description;
|
|
2382
|
-
this.schema = fields.schema;
|
|
2383
|
-
}
|
|
2384
|
-
static lc_name() {
|
|
2385
|
-
return "RunnableToolLike";
|
|
2386
|
-
}
|
|
2387
|
-
}
|
|
1689
|
+
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
1690
|
+
* Useful for streaming, can be automatically created and chained by calling `runnable.pick();`.
|
|
1691
|
+
* @example
|
|
1692
|
+
* ```typescript
|
|
1693
|
+
* import { RunnablePick } from "@langchain/core/runnables";
|
|
1694
|
+
*
|
|
1695
|
+
* const inputData = {
|
|
1696
|
+
* name: "John",
|
|
1697
|
+
* age: 30,
|
|
1698
|
+
* city: "New York",
|
|
1699
|
+
* country: "USA",
|
|
1700
|
+
* email: "john.doe@example.com",
|
|
1701
|
+
* phone: "+1234567890",
|
|
1702
|
+
* };
|
|
1703
|
+
*
|
|
1704
|
+
* const basicInfoRunnable = new RunnablePick(["name", "city"]);
|
|
1705
|
+
*
|
|
1706
|
+
* // Example invocation
|
|
1707
|
+
* const res = await basicInfoRunnable.invoke(inputData);
|
|
1708
|
+
*
|
|
1709
|
+
* // { name: 'John', city: 'New York' }
|
|
1710
|
+
* ```
|
|
1711
|
+
*/
|
|
1712
|
+
var RunnablePick = class extends Runnable {
|
|
1713
|
+
static lc_name() {
|
|
1714
|
+
return "RunnablePick";
|
|
1715
|
+
}
|
|
1716
|
+
lc_namespace = ["langchain_core", "runnables"];
|
|
1717
|
+
lc_serializable = true;
|
|
1718
|
+
keys;
|
|
1719
|
+
constructor(fields) {
|
|
1720
|
+
if (typeof fields === "string" || Array.isArray(fields)) fields = { keys: fields };
|
|
1721
|
+
super(fields);
|
|
1722
|
+
this.keys = fields.keys;
|
|
1723
|
+
}
|
|
1724
|
+
async _pick(input) {
|
|
1725
|
+
if (typeof this.keys === "string") return input[this.keys];
|
|
1726
|
+
else {
|
|
1727
|
+
const picked = this.keys.map((key) => [key, input[key]]).filter((v) => v[1] !== void 0);
|
|
1728
|
+
return picked.length === 0 ? void 0 : Object.fromEntries(picked);
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
async invoke(input, options) {
|
|
1732
|
+
return this._callWithConfig(this._pick.bind(this), input, options);
|
|
1733
|
+
}
|
|
1734
|
+
async *_transform(generator) {
|
|
1735
|
+
for await (const chunk of generator) {
|
|
1736
|
+
const picked = await this._pick(chunk);
|
|
1737
|
+
if (picked !== void 0) yield picked;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
transform(generator, options) {
|
|
1741
|
+
return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
|
|
1742
|
+
}
|
|
1743
|
+
async stream(input, options) {
|
|
1744
|
+
async function* generator() {
|
|
1745
|
+
yield input;
|
|
1746
|
+
}
|
|
1747
|
+
const config = ensureConfig(options);
|
|
1748
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1749
|
+
generator: this.transform(generator(), config),
|
|
1750
|
+
config
|
|
1751
|
+
});
|
|
1752
|
+
await wrappedGenerator.setup;
|
|
1753
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1754
|
+
}
|
|
1755
|
+
};
|
|
1756
|
+
var RunnableToolLike = class extends RunnableBinding {
|
|
1757
|
+
name;
|
|
1758
|
+
description;
|
|
1759
|
+
schema;
|
|
1760
|
+
constructor(fields) {
|
|
1761
|
+
const sequence = RunnableSequence.from([RunnableLambda.from(async (input) => {
|
|
1762
|
+
let toolInput;
|
|
1763
|
+
if (_isToolCall(input)) try {
|
|
1764
|
+
toolInput = await interopParseAsync(this.schema, input.args);
|
|
1765
|
+
} catch (e) {
|
|
1766
|
+
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
1767
|
+
}
|
|
1768
|
+
else toolInput = input;
|
|
1769
|
+
return toolInput;
|
|
1770
|
+
}).withConfig({ runName: `${fields.name}:parse_input` }), fields.bound]).withConfig({ runName: fields.name });
|
|
1771
|
+
super({
|
|
1772
|
+
bound: sequence,
|
|
1773
|
+
config: fields.config ?? {}
|
|
1774
|
+
});
|
|
1775
|
+
this.name = fields.name;
|
|
1776
|
+
this.description = fields.description;
|
|
1777
|
+
this.schema = fields.schema;
|
|
1778
|
+
}
|
|
1779
|
+
static lc_name() {
|
|
1780
|
+
return "RunnableToolLike";
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
2388
1783
|
/**
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
description,
|
|
2417
|
-
schema: fields.schema,
|
|
2418
|
-
bound: runnable,
|
|
2419
|
-
});
|
|
1784
|
+
* Given a runnable and a Zod schema, convert the runnable to a tool.
|
|
1785
|
+
*
|
|
1786
|
+
* @template RunInput The input type for the runnable.
|
|
1787
|
+
* @template RunOutput The output type for the runnable.
|
|
1788
|
+
*
|
|
1789
|
+
* @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
|
|
1790
|
+
* @param fields
|
|
1791
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
1792
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
1793
|
+
* @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
1794
|
+
* @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
1795
|
+
*/
|
|
1796
|
+
function convertRunnableToTool(runnable, fields) {
|
|
1797
|
+
const name = fields.name ?? runnable.getName();
|
|
1798
|
+
const description = fields.description ?? getSchemaDescription(fields.schema);
|
|
1799
|
+
if (isSimpleStringZodSchema(fields.schema)) return new RunnableToolLike({
|
|
1800
|
+
name,
|
|
1801
|
+
description,
|
|
1802
|
+
schema: z.object({ input: z.string() }).transform((input) => input.input),
|
|
1803
|
+
bound: runnable
|
|
1804
|
+
});
|
|
1805
|
+
return new RunnableToolLike({
|
|
1806
|
+
name,
|
|
1807
|
+
description,
|
|
1808
|
+
schema: fields.schema,
|
|
1809
|
+
bound: runnable
|
|
1810
|
+
});
|
|
2420
1811
|
}
|
|
1812
|
+
|
|
1813
|
+
//#endregion
|
|
1814
|
+
export { Runnable, RunnableAssign, RunnableBinding, RunnableEach, RunnableLambda, RunnableMap, RunnableParallel, RunnablePick, RunnableRetry, RunnableSequence, RunnableToolLike, RunnableWithFallbacks, _coerceToDict, _coerceToRunnable };
|
|
1815
|
+
//# sourceMappingURL=base.js.map
|