@langchain/core 1.1.38 → 1.1.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/agents.cjs.map +1 -1
- package/dist/agents.js.map +1 -1
- package/dist/callbacks/base.cjs.map +1 -1
- package/dist/callbacks/base.d.cts +71 -40
- package/dist/callbacks/base.d.cts.map +1 -1
- package/dist/callbacks/base.d.ts +71 -40
- package/dist/callbacks/base.d.ts.map +1 -1
- package/dist/callbacks/base.js.map +1 -1
- package/dist/callbacks/dispatch/index.cjs.map +1 -1
- package/dist/callbacks/dispatch/index.d.cts +0 -1
- package/dist/callbacks/dispatch/index.d.cts.map +1 -1
- package/dist/callbacks/dispatch/index.d.ts +0 -1
- package/dist/callbacks/dispatch/index.d.ts.map +1 -1
- package/dist/callbacks/dispatch/index.js.map +1 -1
- package/dist/callbacks/dispatch/web.cjs.map +1 -1
- package/dist/callbacks/dispatch/web.d.cts +0 -1
- package/dist/callbacks/dispatch/web.d.cts.map +1 -1
- package/dist/callbacks/dispatch/web.d.ts +0 -1
- package/dist/callbacks/dispatch/web.d.ts.map +1 -1
- package/dist/callbacks/dispatch/web.js.map +1 -1
- package/dist/callbacks/manager.cjs.map +1 -1
- package/dist/callbacks/manager.js.map +1 -1
- package/dist/chat_history.d.cts +0 -1
- package/dist/chat_history.d.cts.map +1 -1
- package/dist/document_loaders/langsmith.cjs.map +1 -1
- package/dist/document_loaders/langsmith.js.map +1 -1
- package/dist/documents/document.cjs.map +1 -1
- package/dist/documents/document.js.map +1 -1
- package/dist/errors/index.cjs.map +1 -1
- package/dist/errors/index.js.map +1 -1
- package/dist/indexing/base.cjs.map +1 -1
- package/dist/indexing/base.d.cts.map +1 -1
- package/dist/indexing/base.d.ts.map +1 -1
- package/dist/indexing/base.js.map +1 -1
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.cjs.map +1 -1
- package/dist/language_models/base.d.cts +1 -1
- package/dist/language_models/base.d.cts.map +1 -1
- package/dist/language_models/base.d.ts +1 -1
- package/dist/language_models/base.d.ts.map +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/language_models/base.js.map +1 -1
- package/dist/language_models/chat_models.cjs.map +1 -1
- package/dist/language_models/chat_models.d.cts +0 -1
- package/dist/language_models/chat_models.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/language_models/chat_models.js.map +1 -1
- package/dist/language_models/llms.cjs.map +1 -1
- package/dist/language_models/llms.d.cts.map +1 -1
- package/dist/language_models/llms.d.ts.map +1 -1
- package/dist/language_models/llms.js.map +1 -1
- package/dist/language_models/structured_output.cjs.map +1 -1
- package/dist/language_models/structured_output.d.cts +0 -1
- package/dist/language_models/structured_output.d.cts.map +1 -1
- package/dist/language_models/structured_output.d.ts.map +1 -1
- package/dist/language_models/structured_output.js.map +1 -1
- package/dist/load/index.cjs +1 -2
- package/dist/load/index.cjs.map +1 -1
- package/dist/load/index.js +1 -2
- package/dist/load/index.js.map +1 -1
- package/dist/load/map_keys.cjs.map +1 -1
- package/dist/load/map_keys.js.map +1 -1
- package/dist/load/serializable.cjs.map +1 -1
- package/dist/load/serializable.js.map +1 -1
- package/dist/memory.cjs +2 -8
- package/dist/memory.cjs.map +1 -1
- package/dist/memory.d.cts +2 -2
- package/dist/memory.d.cts.map +1 -1
- package/dist/memory.d.ts +2 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +2 -8
- package/dist/memory.js.map +1 -1
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/block_translators/utils.cjs.map +1 -1
- package/dist/messages/block_translators/utils.js.map +1 -1
- package/dist/messages/chat.cjs.map +1 -1
- package/dist/messages/chat.js.map +1 -1
- package/dist/messages/content/data.cjs.map +1 -1
- package/dist/messages/content/data.d.cts.map +1 -1
- package/dist/messages/content/data.d.ts.map +1 -1
- package/dist/messages/content/data.js.map +1 -1
- package/dist/messages/content/index.cjs.map +1 -1
- package/dist/messages/content/index.js.map +1 -1
- package/dist/messages/content/multimodal.cjs.map +1 -1
- package/dist/messages/content/multimodal.js.map +1 -1
- package/dist/messages/content/tools.cjs.map +1 -1
- package/dist/messages/content/tools.js.map +1 -1
- package/dist/messages/message.cjs.map +1 -1
- package/dist/messages/message.d.cts +3 -2
- package/dist/messages/message.d.cts.map +1 -1
- package/dist/messages/message.d.ts +3 -2
- package/dist/messages/message.d.ts.map +1 -1
- package/dist/messages/message.js.map +1 -1
- package/dist/messages/tool.cjs.map +1 -1
- package/dist/messages/tool.js.map +1 -1
- package/dist/messages/transformers.cjs.map +1 -1
- package/dist/messages/transformers.js.map +1 -1
- package/dist/messages/utils.d.cts.map +1 -1
- package/dist/messages/utils.d.ts.map +1 -1
- package/dist/output_parsers/base.d.cts +0 -1
- package/dist/output_parsers/base.d.cts.map +1 -1
- package/dist/output_parsers/base.d.ts.map +1 -1
- package/dist/output_parsers/json.cjs.map +1 -1
- package/dist/output_parsers/json.d.cts.map +1 -1
- package/dist/output_parsers/json.js.map +1 -1
- package/dist/output_parsers/list.d.cts.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.cjs.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.cts.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts.map +1 -1
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.js.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.cts.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js.map +1 -1
- package/dist/output_parsers/standard_schema.cjs.map +1 -1
- package/dist/output_parsers/standard_schema.js.map +1 -1
- package/dist/output_parsers/string.d.cts.map +1 -1
- package/dist/output_parsers/transform.cjs.map +1 -1
- package/dist/output_parsers/transform.js.map +1 -1
- package/dist/output_parsers/xml.cjs +2 -2
- package/dist/output_parsers/xml.cjs.map +1 -1
- package/dist/output_parsers/xml.d.cts +1 -1
- package/dist/output_parsers/xml.d.ts +1 -1
- package/dist/output_parsers/xml.js +2 -2
- package/dist/output_parsers/xml.js.map +1 -1
- package/dist/outputs.cjs.map +1 -1
- package/dist/outputs.js.map +1 -1
- package/dist/prompt_values.cjs.map +1 -1
- package/dist/prompt_values.js.map +1 -1
- package/dist/prompts/base.cjs.map +1 -1
- package/dist/prompts/base.d.cts +0 -1
- package/dist/prompts/base.d.cts.map +1 -1
- package/dist/prompts/base.js.map +1 -1
- package/dist/prompts/chat.cjs.map +1 -1
- package/dist/prompts/chat.d.cts.map +1 -1
- package/dist/prompts/chat.js.map +1 -1
- package/dist/prompts/few_shot.cjs.map +1 -1
- package/dist/prompts/few_shot.d.cts.map +1 -1
- package/dist/prompts/few_shot.js.map +1 -1
- package/dist/prompts/image.cjs.map +1 -1
- package/dist/prompts/image.d.cts.map +1 -1
- package/dist/prompts/image.js.map +1 -1
- package/dist/prompts/pipeline.cjs.map +1 -1
- package/dist/prompts/pipeline.js.map +1 -1
- package/dist/prompts/prompt.cjs.map +1 -1
- package/dist/prompts/prompt.d.cts.map +1 -1
- package/dist/prompts/prompt.js.map +1 -1
- package/dist/prompts/serde.d.cts.map +1 -1
- package/dist/prompts/string.cjs.map +1 -1
- package/dist/prompts/string.js.map +1 -1
- package/dist/prompts/structured.cjs.map +1 -1
- package/dist/prompts/structured.d.cts.map +1 -1
- package/dist/prompts/structured.d.ts.map +1 -1
- package/dist/prompts/structured.js.map +1 -1
- package/dist/prompts/template.cjs +2 -5
- package/dist/prompts/template.cjs.map +1 -1
- package/dist/prompts/template.d.cts +0 -1
- package/dist/prompts/template.d.cts.map +1 -1
- package/dist/prompts/template.d.ts.map +1 -1
- package/dist/prompts/template.js +2 -5
- package/dist/prompts/template.js.map +1 -1
- package/dist/retrievers/document_compressors/index.cjs.map +1 -1
- package/dist/retrievers/document_compressors/index.js.map +1 -1
- package/dist/retrievers/index.cjs.map +1 -1
- package/dist/retrievers/index.d.cts.map +1 -1
- package/dist/retrievers/index.d.ts.map +1 -1
- package/dist/retrievers/index.js.map +1 -1
- package/dist/runnables/base.cjs.map +1 -1
- package/dist/runnables/base.js.map +1 -1
- package/dist/runnables/branch.cjs.map +1 -1
- package/dist/runnables/branch.d.cts.map +1 -1
- package/dist/runnables/branch.d.ts.map +1 -1
- package/dist/runnables/branch.js.map +1 -1
- package/dist/runnables/config.cjs.map +1 -1
- package/dist/runnables/config.js.map +1 -1
- package/dist/runnables/graph.cjs.map +1 -1
- package/dist/runnables/graph.js.map +1 -1
- package/dist/runnables/history.cjs +2 -4
- package/dist/runnables/history.cjs.map +1 -1
- package/dist/runnables/history.d.cts +2 -4
- package/dist/runnables/history.d.cts.map +1 -1
- package/dist/runnables/history.d.ts +2 -4
- package/dist/runnables/history.d.ts.map +1 -1
- package/dist/runnables/history.js +2 -4
- package/dist/runnables/history.js.map +1 -1
- package/dist/runnables/passthrough.cjs.map +1 -1
- package/dist/runnables/passthrough.d.cts.map +1 -1
- package/dist/runnables/passthrough.d.ts.map +1 -1
- package/dist/runnables/passthrough.js.map +1 -1
- package/dist/runnables/router.cjs.map +1 -1
- package/dist/runnables/router.d.cts.map +1 -1
- package/dist/runnables/router.d.ts.map +1 -1
- package/dist/runnables/router.js.map +1 -1
- package/dist/runnables/utils.cjs.map +1 -1
- package/dist/runnables/utils.js.map +1 -1
- package/dist/singletons/async_local_storage/context.cjs.map +1 -1
- package/dist/singletons/async_local_storage/context.d.cts.map +1 -1
- package/dist/singletons/async_local_storage/context.d.ts.map +1 -1
- package/dist/singletons/async_local_storage/context.js.map +1 -1
- package/dist/singletons/async_local_storage/globals.cjs.map +1 -1
- package/dist/singletons/async_local_storage/globals.js.map +1 -1
- package/dist/singletons/async_local_storage/index.cjs.map +1 -1
- package/dist/singletons/async_local_storage/index.js.map +1 -1
- package/dist/singletons/callbacks.cjs.map +1 -1
- package/dist/singletons/callbacks.d.cts.map +1 -1
- package/dist/singletons/callbacks.js.map +1 -1
- package/dist/stores.cjs.map +1 -1
- package/dist/stores.js.map +1 -1
- package/dist/structured_query/ir.d.cts.map +1 -1
- package/dist/structured_query/ir.d.ts.map +1 -1
- package/dist/structured_query/utils.cjs.map +1 -1
- package/dist/structured_query/utils.js.map +1 -1
- package/dist/testing/fake_model_builder.cjs.map +1 -1
- package/dist/testing/fake_model_builder.d.cts.map +1 -1
- package/dist/testing/fake_model_builder.js.map +1 -1
- package/dist/testing/matchers.cjs.map +1 -1
- package/dist/testing/matchers.d.cts +8 -8
- package/dist/testing/matchers.d.cts.map +1 -1
- package/dist/testing/matchers.d.ts +8 -8
- package/dist/testing/matchers.d.ts.map +1 -1
- package/dist/testing/matchers.js.map +1 -1
- package/dist/tools/index.cjs.map +1 -1
- package/dist/tools/index.d.cts +3 -2
- package/dist/tools/index.d.cts.map +1 -1
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.d.cts +1 -1
- package/dist/tools/types.d.cts.map +1 -1
- package/dist/tools/types.d.ts +1 -1
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tracers/base.cjs.map +1 -1
- package/dist/tracers/base.d.cts +91 -91
- package/dist/tracers/base.d.cts.map +1 -1
- package/dist/tracers/base.d.ts +91 -91
- package/dist/tracers/base.d.ts.map +1 -1
- package/dist/tracers/base.js.map +1 -1
- package/dist/tracers/event_stream.cjs.map +1 -1
- package/dist/tracers/event_stream.d.cts +0 -1
- package/dist/tracers/event_stream.d.cts.map +1 -1
- package/dist/tracers/event_stream.js.map +1 -1
- package/dist/tracers/log_stream.cjs.map +1 -1
- package/dist/tracers/log_stream.d.cts.map +1 -1
- package/dist/tracers/log_stream.d.ts.map +1 -1
- package/dist/tracers/log_stream.js.map +1 -1
- package/dist/utils/async_caller.cjs.map +1 -1
- package/dist/utils/async_caller.js.map +1 -1
- package/dist/utils/chunk_array.d.cts.map +1 -1
- package/dist/utils/chunk_array.d.ts.map +1 -1
- package/dist/utils/env.cjs.map +1 -1
- package/dist/utils/env.js.map +1 -1
- package/dist/utils/event_source_parse.cjs.map +1 -1
- package/dist/utils/event_source_parse.d.cts +2 -2
- package/dist/utils/event_source_parse.d.cts.map +1 -1
- package/dist/utils/event_source_parse.d.ts +2 -2
- package/dist/utils/event_source_parse.d.ts.map +1 -1
- package/dist/utils/event_source_parse.js.map +1 -1
- package/dist/utils/function_calling.cjs.map +1 -1
- package/dist/utils/function_calling.js.map +1 -1
- package/dist/utils/is-network-error/index.cjs.map +1 -1
- package/dist/utils/is-network-error/index.js.map +1 -1
- package/dist/utils/json.cjs.map +1 -1
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/json_schema.d.cts.map +1 -1
- package/dist/utils/namespace.cjs.map +1 -1
- package/dist/utils/namespace.js.map +1 -1
- package/dist/utils/p-retry/index.cjs.map +1 -1
- package/dist/utils/p-retry/index.js.map +1 -1
- package/dist/utils/signal.cjs.map +1 -1
- package/dist/utils/signal.js.map +1 -1
- package/dist/utils/standard_schema.cjs.map +1 -1
- package/dist/utils/standard_schema.js.map +1 -1
- package/dist/utils/stream.cjs.map +1 -1
- package/dist/utils/stream.d.cts +2 -2
- package/dist/utils/stream.d.cts.map +1 -1
- package/dist/utils/stream.d.ts +2 -2
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/stream.js.map +1 -1
- package/dist/utils/testing/chat_models.cjs.map +1 -1
- package/dist/utils/testing/chat_models.d.cts.map +1 -1
- package/dist/utils/testing/chat_models.js.map +1 -1
- package/dist/utils/testing/retrievers.cjs.map +1 -1
- package/dist/utils/testing/retrievers.js.map +1 -1
- package/dist/utils/testing/vectorstores.cjs.map +1 -1
- package/dist/utils/testing/vectorstores.js.map +1 -1
- package/dist/utils/types/index.cjs.map +1 -1
- package/dist/utils/types/index.js.map +1 -1
- package/dist/utils/types/zod.cjs.map +1 -1
- package/dist/utils/types/zod.js.map +1 -1
- package/dist/utils/zod-to-json-schema/Options.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/Refs.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/errorMessages.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/errorMessages.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/index.d.cts +24 -0
- package/dist/utils/zod-to-json-schema/parseDef.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/array.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/array.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/bigint.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/bigint.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/branded.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/catch.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/date.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/date.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/default.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/effects.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/intersection.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/intersection.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/map.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/map.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/never.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/never.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/nullable.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/nullable.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/number.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/number.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/object.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/object.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/optional.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/pipeline.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/promise.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/readonly.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/parsers/record.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/record.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/set.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/set.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/string.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/string.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/tuple.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/tuple.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/undefined.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/undefined.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/union.d.cts +1 -2
- package/dist/utils/zod-to-json-schema/parsers/union.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/union.d.ts.map +1 -1
- package/dist/utils/zod-to-json-schema/parsers/unknown.d.cts +0 -1
- package/dist/utils/zod-to-json-schema/parsers/unknown.d.cts.map +1 -1
- package/dist/utils/zod-to-json-schema/selectParser.d.cts +1 -0
- package/dist/utils/zod-to-json-schema/zodToJsonSchema.d.cts +1 -0
- package/dist/vectorstores.cjs.map +1 -1
- package/dist/vectorstores.js.map +1 -1
- package/package.json +5 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log_stream.js","names":[],"sources":["../../src/tracers/log_stream.ts"],"sourcesContent":["import {\n applyPatch,\n type Operation as JSONPatchOperation,\n} from \"../utils/fast-json-patch/index.js\";\nimport { BaseTracer, type Run } from \"./base.js\";\nimport {\n BaseCallbackHandler,\n BaseCallbackHandlerInput,\n CallbackHandlerPrefersStreaming,\n HandleLLMNewTokenCallbackFields,\n} from \"../callbacks/base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ChatGenerationChunk, GenerationChunk } from \"../outputs.js\";\nimport { AIMessageChunk } from \"../messages/ai.js\";\nimport type { StreamEvent, StreamEventData } from \"./event_stream.js\";\n\nexport type { StreamEvent, StreamEventData };\n\n/**\n * Interface that represents the structure of a log entry in the\n * `LogStreamCallbackHandler`.\n */\nexport type LogEntry = {\n /** ID of the sub-run. */\n id: string;\n /** Name of the object being run. */\n name: string;\n /** Type of the object being run, eg. prompt, chain, llm, etc. */\n type: string;\n /** List of tags for the run. */\n tags: string[];\n /** Key-value pairs of metadata for the run. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata: Record<string, any>;\n /** ISO-8601 timestamp of when the run started. */\n start_time: string;\n /** List of general output chunks streamed by this run. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n streamed_output: any[];\n /** List of LLM tokens streamed by this run, if applicable. */\n streamed_output_str: string[];\n /** Inputs to this run. Not available currently via streamLog. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputs?: any;\n /** Final output of this run. Only available after the run has finished successfully. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n final_output?: any;\n /** ISO-8601 timestamp of when the run ended. Only available after the run has finished. */\n end_time?: string;\n};\n\nexport type RunState = {\n /** ID of the sub-run. */\n id: string;\n /** List of output chunks streamed by Runnable.stream() */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n streamed_output: any[];\n /** Final output of the run, usually the result of aggregating streamed_output. Only available after the run has finished successfully. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n final_output?: any;\n /**\n * List of sub-runs contained in this run, if any, in the order they were started.\n * If filters were supplied, this list will contain only the runs that matched the filters.\n */\n logs: Record<string, LogEntry>;\n /** Name of the object being run. */\n name: string;\n /** Type of the object being run, eg. prompt, chain, llm, etc. */\n type: string;\n};\n\n/**\n * List of jsonpatch JSONPatchOperations, which describe how to create the run state\n * from an empty dict. This is the minimal representation of the log, designed to\n * be serialized as JSON and sent over the wire to reconstruct the log on the other\n * side. Reconstruction of the state can be done with any jsonpatch-compliant library,\n * see https://jsonpatch.com for more information.\n */\nexport class RunLogPatch {\n ops: JSONPatchOperation[];\n\n constructor(fields: { ops?: JSONPatchOperation[] }) {\n this.ops = fields.ops ?? [];\n }\n\n concat(other: RunLogPatch) {\n const ops = this.ops.concat(other.ops);\n const states = applyPatch({}, ops);\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n return new RunLog({\n ops,\n state: states[states.length - 1].newDocument as RunState,\n });\n }\n}\n\nexport class RunLog extends RunLogPatch {\n state: RunState;\n\n constructor(fields: { ops?: JSONPatchOperation[]; state: RunState }) {\n super(fields);\n this.state = fields.state;\n }\n\n concat(other: RunLogPatch) {\n const ops = this.ops.concat(other.ops);\n const states = applyPatch(this.state, other.ops);\n return new RunLog({ ops, state: states[states.length - 1].newDocument });\n }\n\n static fromRunLogPatch(patch: RunLogPatch) {\n const states = applyPatch({}, patch.ops);\n return new RunLog({\n ops: patch.ops,\n state: states[states.length - 1].newDocument as RunState,\n });\n }\n}\n\nexport type SchemaFormat = \"original\" | \"streaming_events\";\n\nexport interface LogStreamCallbackHandlerInput extends BaseCallbackHandlerInput {\n autoClose?: boolean;\n includeNames?: string[];\n includeTypes?: string[];\n includeTags?: string[];\n excludeNames?: string[];\n excludeTypes?: string[];\n excludeTags?: string[];\n _schemaFormat?: SchemaFormat;\n}\n\nexport const isLogStreamHandler = (\n handler: BaseCallbackHandler\n): handler is LogStreamCallbackHandler => handler.name === \"log_stream_tracer\";\n\n/**\n * Extract standardized inputs from a run.\n *\n * Standardizes the inputs based on the type of the runnable used.\n *\n * @param run - Run object\n * @param schemaFormat - The schema format to use.\n *\n * @returns Valid inputs are only dict. By conventions, inputs always represented\n * invocation using named arguments.\n * A null means that the input is not yet known!\n */\nasync function _getStandardizedInputs(run: Run, schemaFormat: SchemaFormat) {\n if (schemaFormat === \"original\") {\n throw new Error(\n \"Do not assign inputs with original schema drop the key for now. \" +\n \"When inputs are added to streamLog they should be added with \" +\n \"standardized schema for streaming events.\"\n );\n }\n\n const { inputs } = run;\n\n if ([\"retriever\", \"llm\", \"prompt\"].includes(run.run_type)) {\n return inputs;\n }\n\n if (Object.keys(inputs).length === 1 && inputs?.input === \"\") {\n return undefined;\n }\n\n // new style chains\n // These nest an additional 'input' key inside the 'inputs' to make sure\n // the input is always a dict. We need to unpack and user the inner value.\n // We should try to fix this in Runnables and callbacks/tracers\n // Runnables should be using a null type here not a placeholder\n // dict.\n return inputs.input;\n}\n\nasync function _getStandardizedOutputs(run: Run, schemaFormat: SchemaFormat) {\n const { outputs } = run;\n if (schemaFormat === \"original\") {\n // Return the old schema, without standardizing anything\n return outputs;\n }\n\n if ([\"retriever\", \"llm\", \"prompt\"].includes(run.run_type)) {\n return outputs;\n }\n\n // TODO: Remove this hacky check\n if (\n outputs !== undefined &&\n Object.keys(outputs).length === 1 &&\n outputs?.output !== undefined\n ) {\n return outputs.output;\n }\n\n return outputs;\n}\n\nfunction isChatGenerationChunk(\n x?: ChatGenerationChunk | GenerationChunk\n): x is ChatGenerationChunk {\n return x !== undefined && (x as ChatGenerationChunk).message !== undefined;\n}\n\n/**\n * Class that extends the `BaseTracer` class from the\n * `langchain.callbacks.tracers.base` module. It represents a callback\n * handler that logs the execution of runs and emits `RunLog` instances to a\n * `RunLogStream`.\n */\nexport class LogStreamCallbackHandler\n extends BaseTracer\n implements CallbackHandlerPrefersStreaming\n{\n protected autoClose = true;\n\n protected includeNames?: string[];\n\n protected includeTypes?: string[];\n\n protected includeTags?: string[];\n\n protected excludeNames?: string[];\n\n protected excludeTypes?: string[];\n\n protected excludeTags?: string[];\n\n protected _schemaFormat: SchemaFormat = \"original\";\n\n protected rootId?: string;\n\n private keyMapByRunId: Record<string, string> = {};\n\n private counterMapByRunName: Record<string, number> = {};\n\n protected transformStream: TransformStream;\n\n public writer: WritableStreamDefaultWriter;\n\n public receiveStream: IterableReadableStream<RunLogPatch>;\n\n name = \"log_stream_tracer\";\n\n lc_prefer_streaming = true;\n\n constructor(fields?: LogStreamCallbackHandlerInput) {\n super({ _awaitHandler: true, ...fields });\n this.autoClose = fields?.autoClose ?? true;\n this.includeNames = fields?.includeNames;\n this.includeTypes = fields?.includeTypes;\n this.includeTags = fields?.includeTags;\n this.excludeNames = fields?.excludeNames;\n this.excludeTypes = fields?.excludeTypes;\n this.excludeTags = fields?.excludeTags;\n this._schemaFormat = fields?._schemaFormat ?? this._schemaFormat;\n this.transformStream = new TransformStream();\n this.writer = this.transformStream.writable.getWriter();\n this.receiveStream = IterableReadableStream.fromReadableStream(\n this.transformStream.readable\n );\n }\n\n [Symbol.asyncIterator]() {\n return this.receiveStream;\n }\n\n protected async persistRun(_run: Run): Promise<void> {\n // This is a legacy method only called once for an entire run tree\n // and is therefore not useful here\n }\n\n _includeRun(run: Run): boolean {\n if (run.id === this.rootId) {\n return false;\n }\n const runTags = run.tags ?? [];\n let include =\n this.includeNames === undefined &&\n this.includeTags === undefined &&\n this.includeTypes === undefined;\n if (this.includeNames !== undefined) {\n include = include || this.includeNames.includes(run.name);\n }\n if (this.includeTypes !== undefined) {\n include = include || this.includeTypes.includes(run.run_type);\n }\n if (this.includeTags !== undefined) {\n include =\n include ||\n runTags.find((tag) => this.includeTags?.includes(tag)) !== undefined;\n }\n if (this.excludeNames !== undefined) {\n include = include && !this.excludeNames.includes(run.name);\n }\n if (this.excludeTypes !== undefined) {\n include = include && !this.excludeTypes.includes(run.run_type);\n }\n if (this.excludeTags !== undefined) {\n include =\n include && runTags.every((tag) => !this.excludeTags?.includes(tag));\n }\n return include;\n }\n\n async *tapOutputIterable<T>(\n runId: string,\n output: AsyncGenerator<T>\n ): AsyncGenerator<T> {\n // Tap an output async iterator to stream its values to the log.\n for await (const chunk of output) {\n // root run is handled in .streamLog()\n if (runId !== this.rootId) {\n // if we can't find the run silently ignore\n // eg. because this run wasn't included in the log\n const key = this.keyMapByRunId[runId];\n if (key) {\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${key}/streamed_output/-`,\n value: chunk,\n },\n ],\n })\n );\n }\n }\n yield chunk;\n }\n }\n\n async onRunCreate(run: Run): Promise<void> {\n if (this.rootId === undefined) {\n this.rootId = run.id;\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"replace\",\n path: \"\",\n value: {\n id: run.id,\n name: run.name,\n type: run.run_type,\n streamed_output: [],\n final_output: undefined,\n logs: {},\n },\n },\n ],\n })\n );\n }\n\n if (!this._includeRun(run)) {\n return;\n }\n\n if (this.counterMapByRunName[run.name] === undefined) {\n this.counterMapByRunName[run.name] = 0;\n }\n this.counterMapByRunName[run.name] += 1;\n const count = this.counterMapByRunName[run.name];\n this.keyMapByRunId[run.id] =\n count === 1 ? run.name : `${run.name}:${count}`;\n\n const logEntry: LogEntry = {\n id: run.id,\n name: run.name,\n type: run.run_type,\n tags: run.tags ?? [],\n metadata: run.extra?.metadata ?? {},\n start_time: new Date(run.start_time).toISOString(),\n streamed_output: [],\n streamed_output_str: [],\n final_output: undefined,\n end_time: undefined,\n };\n\n if (this._schemaFormat === \"streaming_events\") {\n logEntry.inputs = await _getStandardizedInputs(run, this._schemaFormat);\n }\n\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${this.keyMapByRunId[run.id]}`,\n value: logEntry,\n },\n ],\n })\n );\n }\n\n async onRunUpdate(run: Run): Promise<void> {\n try {\n const runName = this.keyMapByRunId[run.id];\n if (runName === undefined) {\n return;\n }\n const ops: JSONPatchOperation[] = [];\n if (this._schemaFormat === \"streaming_events\") {\n ops.push({\n op: \"replace\",\n path: `/logs/${runName}/inputs`,\n value: await _getStandardizedInputs(run, this._schemaFormat),\n });\n }\n ops.push({\n op: \"add\",\n path: `/logs/${runName}/final_output`,\n value: await _getStandardizedOutputs(run, this._schemaFormat),\n });\n if (run.end_time !== undefined) {\n ops.push({\n op: \"add\",\n path: `/logs/${runName}/end_time`,\n value: new Date(run.end_time).toISOString(),\n });\n }\n const patch = new RunLogPatch({ ops });\n await this.writer.write(patch);\n } finally {\n if (run.id === this.rootId) {\n const patch = new RunLogPatch({\n ops: [\n {\n op: \"replace\",\n path: \"/final_output\",\n value: await _getStandardizedOutputs(run, this._schemaFormat),\n },\n ],\n });\n await this.writer.write(patch);\n if (this.autoClose) {\n await this.writer.close();\n }\n }\n }\n }\n\n async onLLMNewToken(\n run: Run,\n token: string,\n kwargs?: HandleLLMNewTokenCallbackFields\n ): Promise<void> {\n const runName = this.keyMapByRunId[run.id];\n if (runName === undefined) {\n return;\n }\n // TODO: Remove hack\n const isChatModel = run.inputs.messages !== undefined;\n let streamedOutputValue;\n if (isChatModel) {\n if (isChatGenerationChunk(kwargs?.chunk)) {\n streamedOutputValue = kwargs?.chunk;\n } else {\n streamedOutputValue = new AIMessageChunk({\n id: `run-${run.id}`,\n content: token,\n });\n }\n } else {\n streamedOutputValue = token;\n }\n const patch = new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${runName}/streamed_output_str/-`,\n value: token,\n },\n {\n op: \"add\",\n path: `/logs/${runName}/streamed_output/-`,\n value: streamedOutputValue,\n },\n ],\n });\n await this.writer.write(patch);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8EA,IAAa,cAAb,MAAyB;CACvB;CAEA,YAAY,QAAwC;AAClD,OAAK,MAAM,OAAO,OAAO,EAAE;;CAG7B,OAAO,OAAoB;EACzB,MAAM,MAAM,KAAK,IAAI,OAAO,MAAM,IAAI;EACtC,MAAM,SAAS,WAAW,EAAE,EAAE,IAAI;AAElC,SAAO,IAAI,OAAO;GAChB;GACA,OAAO,OAAO,OAAO,SAAS,GAAG;GAClC,CAAC;;;AAIN,IAAa,SAAb,MAAa,eAAe,YAAY;CACtC;CAEA,Y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AI,OAAO,aAAa,KAAA;EAC5C,IAAI;AACJ,MAAI,YACF,KAAI,sBAAsB,QAAQ,MAAM,CACtC,uBAAsB,QAAQ;MAE9B,uBAAsB,IAAI,eAAe;GACvC,IAAI,OAAO,IAAI;GACf,SAAS;GACV,CAAC;MAGJ,uBAAsB;EAExB,MAAM,QAAQ,IAAI,YAAY,EAC5B,KAAK,CACH;GACE,IAAI;GACJ,MAAM,SAAS,QAAQ;GACvB,OAAO;GACR,EACD;GACE,IAAI;GACJ,MAAM,SAAS,QAAQ;GACvB,OAAO;GACR,CACF,EACF,CAAC;AACF,QAAM,KAAK,OAAO,MAAM,MAAM"}
|
|
1
|
+
{"version":3,"file":"log_stream.js","names":[],"sources":["../../src/tracers/log_stream.ts"],"sourcesContent":["import {\n applyPatch,\n type Operation as JSONPatchOperation,\n} from \"../utils/fast-json-patch/index.js\";\nimport { BaseTracer, type Run } from \"./base.js\";\nimport {\n BaseCallbackHandler,\n BaseCallbackHandlerInput,\n CallbackHandlerPrefersStreaming,\n HandleLLMNewTokenCallbackFields,\n} from \"../callbacks/base.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { ChatGenerationChunk, GenerationChunk } from \"../outputs.js\";\nimport { AIMessageChunk } from \"../messages/ai.js\";\nimport type { StreamEvent, StreamEventData } from \"./event_stream.js\";\n\nexport type { StreamEvent, StreamEventData };\n\n/**\n * Interface that represents the structure of a log entry in the\n * `LogStreamCallbackHandler`.\n */\nexport type LogEntry = {\n /** ID of the sub-run. */\n id: string;\n /** Name of the object being run. */\n name: string;\n /** Type of the object being run, eg. prompt, chain, llm, etc. */\n type: string;\n /** List of tags for the run. */\n tags: string[];\n /** Key-value pairs of metadata for the run. */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n metadata: Record<string, any>;\n /** ISO-8601 timestamp of when the run started. */\n start_time: string;\n /** List of general output chunks streamed by this run. */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n streamed_output: any[];\n /** List of LLM tokens streamed by this run, if applicable. */\n streamed_output_str: string[];\n /** Inputs to this run. Not available currently via streamLog. */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n inputs?: any;\n /** Final output of this run. Only available after the run has finished successfully. */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n final_output?: any;\n /** ISO-8601 timestamp of when the run ended. Only available after the run has finished. */\n end_time?: string;\n};\n\nexport type RunState = {\n /** ID of the sub-run. */\n id: string;\n /** List of output chunks streamed by Runnable.stream() */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n streamed_output: any[];\n /** Final output of the run, usually the result of aggregating streamed_output. Only available after the run has finished successfully. */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n final_output?: any;\n /**\n * List of sub-runs contained in this run, if any, in the order they were started.\n * If filters were supplied, this list will contain only the runs that matched the filters.\n */\n logs: Record<string, LogEntry>;\n /** Name of the object being run. */\n name: string;\n /** Type of the object being run, eg. prompt, chain, llm, etc. */\n type: string;\n};\n\n/**\n * List of jsonpatch JSONPatchOperations, which describe how to create the run state\n * from an empty dict. This is the minimal representation of the log, designed to\n * be serialized as JSON and sent over the wire to reconstruct the log on the other\n * side. Reconstruction of the state can be done with any jsonpatch-compliant library,\n * see https://jsonpatch.com for more information.\n */\nexport class RunLogPatch {\n ops: JSONPatchOperation[];\n\n constructor(fields: { ops?: JSONPatchOperation[] }) {\n this.ops = fields.ops ?? [];\n }\n\n concat(other: RunLogPatch) {\n const ops = this.ops.concat(other.ops);\n const states = applyPatch({}, ops);\n // oxlint-disable-next-line @typescript-eslint/no-use-before-define\n return new RunLog({\n ops,\n state: states[states.length - 1].newDocument as RunState,\n });\n }\n}\n\nexport class RunLog extends RunLogPatch {\n state: RunState;\n\n constructor(fields: { ops?: JSONPatchOperation[]; state: RunState }) {\n super(fields);\n this.state = fields.state;\n }\n\n concat(other: RunLogPatch) {\n const ops = this.ops.concat(other.ops);\n const states = applyPatch(this.state, other.ops);\n return new RunLog({ ops, state: states[states.length - 1].newDocument });\n }\n\n static fromRunLogPatch(patch: RunLogPatch) {\n const states = applyPatch({}, patch.ops);\n return new RunLog({\n ops: patch.ops,\n state: states[states.length - 1].newDocument as RunState,\n });\n }\n}\n\nexport type SchemaFormat = \"original\" | \"streaming_events\";\n\nexport interface LogStreamCallbackHandlerInput extends BaseCallbackHandlerInput {\n autoClose?: boolean;\n includeNames?: string[];\n includeTypes?: string[];\n includeTags?: string[];\n excludeNames?: string[];\n excludeTypes?: string[];\n excludeTags?: string[];\n _schemaFormat?: SchemaFormat;\n}\n\nexport const isLogStreamHandler = (\n handler: BaseCallbackHandler\n): handler is LogStreamCallbackHandler => handler.name === \"log_stream_tracer\";\n\n/**\n * Extract standardized inputs from a run.\n *\n * Standardizes the inputs based on the type of the runnable used.\n *\n * @param run - Run object\n * @param schemaFormat - The schema format to use.\n *\n * @returns Valid inputs are only dict. By conventions, inputs always represented\n * invocation using named arguments.\n * A null means that the input is not yet known!\n */\nasync function _getStandardizedInputs(run: Run, schemaFormat: SchemaFormat) {\n if (schemaFormat === \"original\") {\n throw new Error(\n \"Do not assign inputs with original schema drop the key for now. \" +\n \"When inputs are added to streamLog they should be added with \" +\n \"standardized schema for streaming events.\"\n );\n }\n\n const { inputs } = run;\n\n if ([\"retriever\", \"llm\", \"prompt\"].includes(run.run_type)) {\n return inputs;\n }\n\n if (Object.keys(inputs).length === 1 && inputs?.input === \"\") {\n return undefined;\n }\n\n // new style chains\n // These nest an additional 'input' key inside the 'inputs' to make sure\n // the input is always a dict. We need to unpack and user the inner value.\n // We should try to fix this in Runnables and callbacks/tracers\n // Runnables should be using a null type here not a placeholder\n // dict.\n return inputs.input;\n}\n\nasync function _getStandardizedOutputs(run: Run, schemaFormat: SchemaFormat) {\n const { outputs } = run;\n if (schemaFormat === \"original\") {\n // Return the old schema, without standardizing anything\n return outputs;\n }\n\n if ([\"retriever\", \"llm\", \"prompt\"].includes(run.run_type)) {\n return outputs;\n }\n\n // TODO: Remove this hacky check\n if (\n outputs !== undefined &&\n Object.keys(outputs).length === 1 &&\n outputs?.output !== undefined\n ) {\n return outputs.output;\n }\n\n return outputs;\n}\n\nfunction isChatGenerationChunk(\n x?: ChatGenerationChunk | GenerationChunk\n): x is ChatGenerationChunk {\n return x !== undefined && (x as ChatGenerationChunk).message !== undefined;\n}\n\n/**\n * Class that extends the `BaseTracer` class from the\n * `langchain.callbacks.tracers.base` module. It represents a callback\n * handler that logs the execution of runs and emits `RunLog` instances to a\n * `RunLogStream`.\n */\nexport class LogStreamCallbackHandler\n extends BaseTracer\n implements CallbackHandlerPrefersStreaming\n{\n protected autoClose = true;\n\n protected includeNames?: string[];\n\n protected includeTypes?: string[];\n\n protected includeTags?: string[];\n\n protected excludeNames?: string[];\n\n protected excludeTypes?: string[];\n\n protected excludeTags?: string[];\n\n protected _schemaFormat: SchemaFormat = \"original\";\n\n protected rootId?: string;\n\n private keyMapByRunId: Record<string, string> = {};\n\n private counterMapByRunName: Record<string, number> = {};\n\n protected transformStream: TransformStream;\n\n public writer: WritableStreamDefaultWriter;\n\n public receiveStream: IterableReadableStream<RunLogPatch>;\n\n name = \"log_stream_tracer\";\n\n lc_prefer_streaming = true;\n\n constructor(fields?: LogStreamCallbackHandlerInput) {\n super({ _awaitHandler: true, ...fields });\n this.autoClose = fields?.autoClose ?? true;\n this.includeNames = fields?.includeNames;\n this.includeTypes = fields?.includeTypes;\n this.includeTags = fields?.includeTags;\n this.excludeNames = fields?.excludeNames;\n this.excludeTypes = fields?.excludeTypes;\n this.excludeTags = fields?.excludeTags;\n this._schemaFormat = fields?._schemaFormat ?? this._schemaFormat;\n this.transformStream = new TransformStream();\n this.writer = this.transformStream.writable.getWriter();\n this.receiveStream = IterableReadableStream.fromReadableStream(\n this.transformStream.readable\n );\n }\n\n [Symbol.asyncIterator]() {\n return this.receiveStream;\n }\n\n protected async persistRun(_run: Run): Promise<void> {\n // This is a legacy method only called once for an entire run tree\n // and is therefore not useful here\n }\n\n _includeRun(run: Run): boolean {\n if (run.id === this.rootId) {\n return false;\n }\n const runTags = run.tags ?? [];\n let include =\n this.includeNames === undefined &&\n this.includeTags === undefined &&\n this.includeTypes === undefined;\n if (this.includeNames !== undefined) {\n include = include || this.includeNames.includes(run.name);\n }\n if (this.includeTypes !== undefined) {\n include = include || this.includeTypes.includes(run.run_type);\n }\n if (this.includeTags !== undefined) {\n include =\n include ||\n runTags.find((tag) => this.includeTags?.includes(tag)) !== undefined;\n }\n if (this.excludeNames !== undefined) {\n include = include && !this.excludeNames.includes(run.name);\n }\n if (this.excludeTypes !== undefined) {\n include = include && !this.excludeTypes.includes(run.run_type);\n }\n if (this.excludeTags !== undefined) {\n include =\n include && runTags.every((tag) => !this.excludeTags?.includes(tag));\n }\n return include;\n }\n\n async *tapOutputIterable<T>(\n runId: string,\n output: AsyncGenerator<T>\n ): AsyncGenerator<T> {\n // Tap an output async iterator to stream its values to the log.\n for await (const chunk of output) {\n // root run is handled in .streamLog()\n if (runId !== this.rootId) {\n // if we can't find the run silently ignore\n // eg. because this run wasn't included in the log\n const key = this.keyMapByRunId[runId];\n if (key) {\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${key}/streamed_output/-`,\n value: chunk,\n },\n ],\n })\n );\n }\n }\n yield chunk;\n }\n }\n\n async onRunCreate(run: Run): Promise<void> {\n if (this.rootId === undefined) {\n this.rootId = run.id;\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"replace\",\n path: \"\",\n value: {\n id: run.id,\n name: run.name,\n type: run.run_type,\n streamed_output: [],\n final_output: undefined,\n logs: {},\n },\n },\n ],\n })\n );\n }\n\n if (!this._includeRun(run)) {\n return;\n }\n\n if (this.counterMapByRunName[run.name] === undefined) {\n this.counterMapByRunName[run.name] = 0;\n }\n this.counterMapByRunName[run.name] += 1;\n const count = this.counterMapByRunName[run.name];\n this.keyMapByRunId[run.id] =\n count === 1 ? run.name : `${run.name}:${count}`;\n\n const logEntry: LogEntry = {\n id: run.id,\n name: run.name,\n type: run.run_type,\n tags: run.tags ?? [],\n metadata: run.extra?.metadata ?? {},\n start_time: new Date(run.start_time).toISOString(),\n streamed_output: [],\n streamed_output_str: [],\n final_output: undefined,\n end_time: undefined,\n };\n\n if (this._schemaFormat === \"streaming_events\") {\n logEntry.inputs = await _getStandardizedInputs(run, this._schemaFormat);\n }\n\n await this.writer.write(\n new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${this.keyMapByRunId[run.id]}`,\n value: logEntry,\n },\n ],\n })\n );\n }\n\n async onRunUpdate(run: Run): Promise<void> {\n try {\n const runName = this.keyMapByRunId[run.id];\n if (runName === undefined) {\n return;\n }\n const ops: JSONPatchOperation[] = [];\n if (this._schemaFormat === \"streaming_events\") {\n ops.push({\n op: \"replace\",\n path: `/logs/${runName}/inputs`,\n value: await _getStandardizedInputs(run, this._schemaFormat),\n });\n }\n ops.push({\n op: \"add\",\n path: `/logs/${runName}/final_output`,\n value: await _getStandardizedOutputs(run, this._schemaFormat),\n });\n if (run.end_time !== undefined) {\n ops.push({\n op: \"add\",\n path: `/logs/${runName}/end_time`,\n value: new Date(run.end_time).toISOString(),\n });\n }\n const patch = new RunLogPatch({ ops });\n await this.writer.write(patch);\n } finally {\n if (run.id === this.rootId) {\n const patch = new RunLogPatch({\n ops: [\n {\n op: \"replace\",\n path: \"/final_output\",\n value: await _getStandardizedOutputs(run, this._schemaFormat),\n },\n ],\n });\n await this.writer.write(patch);\n if (this.autoClose) {\n await this.writer.close();\n }\n }\n }\n }\n\n async onLLMNewToken(\n run: Run,\n token: string,\n kwargs?: HandleLLMNewTokenCallbackFields\n ): Promise<void> {\n const runName = this.keyMapByRunId[run.id];\n if (runName === undefined) {\n return;\n }\n // TODO: Remove hack\n const isChatModel = run.inputs.messages !== undefined;\n let streamedOutputValue;\n if (isChatModel) {\n if (isChatGenerationChunk(kwargs?.chunk)) {\n streamedOutputValue = kwargs?.chunk;\n } else {\n streamedOutputValue = new AIMessageChunk({\n id: `run-${run.id}`,\n content: token,\n });\n }\n } else {\n streamedOutputValue = token;\n }\n const patch = new RunLogPatch({\n ops: [\n {\n op: \"add\",\n path: `/logs/${runName}/streamed_output_str/-`,\n value: token,\n },\n {\n op: \"add\",\n path: `/logs/${runName}/streamed_output/-`,\n value: streamedOutputValue,\n },\n ],\n });\n await this.writer.write(patch);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8EA,IAAa,cAAb,MAAyB;CACvB;CAEA,YAAY,QAAwC;AAClD,OAAK,MAAM,OAAO,OAAO,EAAE;;CAG7B,OAAO,OAAoB;EACzB,MAAM,MAAM,KAAK,IAAI,OAAO,MAAM,IAAI;EACtC,MAAM,SAAS,WAAW,EAAE,EAAE,IAAI;AAElC,SAAO,IAAI,OAAO;GAChB;GACA,OAAO,OAAO,OAAO,SAAS,GAAG;GAClC,CAAC;;;AAIN,IAAa,SAAb,MAAa,eAAe,YAAY;CACtC;CAEA,YAAY,QAAyD;AACnE,QAAM,OAAO;AACb,OAAK,QAAQ,OAAO;;CAGtB,OAAO,OAAoB;EACzB,MAAM,MAAM,KAAK,IAAI,OAAO,MAAM,IAAI;EACtC,MAAM,SAAS,WAAW,KAAK,OAAO,MAAM,IAAI;AAChD,SAAO,IAAI,OAAO;GAAE;GAAK,OAAO,OAAO,OAAO,SAAS,GAAG;GAAa,CAAC;;CAG1E,OAAO,gBAAgB,OAAoB;EACzC,MAAM,SAAS,WAAW,EAAE,EAAE,MAAM,IAAI;AACxC,SAAO,IAAI,OAAO;GAChB,KAAK,MAAM;GACX,OAAO,OAAO,OAAO,SAAS,GAAG;GAClC,CAAC;;;AAiBN,MAAa,sBACX,YACwC,QAAQ,SAAS;;;;;;;;;;;;;AAc3D,eAAe,uBAAuB,KAAU,cAA4B;AAC1E,KAAI,iBAAiB,WACnB,OAAM,IAAI,MACR,yKAGD;CAGH,MAAM,EAAE,WAAW;AAEnB,KAAI;EAAC;EAAa;EAAO;EAAS,CAAC,SAAS,IAAI,SAAS,CACvD,QAAO;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AAK;IACP,IAAI;IACJ,MAAM,SAAS,QAAQ;IACvB,OAAO,IAAI,KAAK,IAAI,SAAS,CAAC,aAAa;IAC5C,CAAC;GAEJ,MAAM,QAAQ,IAAI,YAAY,EAAE,KAAK,CAAC;AACtC,SAAM,KAAK,OAAO,MAAM,MAAM;YACtB;AACR,OAAI,IAAI,OAAO,KAAK,QAAQ;IAC1B,MAAM,QAAQ,IAAI,YAAY,EAC5B,KAAK,CACH;KACE,IAAI;KACJ,MAAM;KACN,OAAO,MAAM,wBAAwB,KAAK,KAAK,cAAc;KAC9D,CACF,EACF,CAAC;AACF,UAAM,KAAK,OAAO,MAAM,MAAM;AAC9B,QAAI,KAAK,UACP,OAAM,KAAK,OAAO,OAAO;;;;CAMjC,MAAM,cACJ,KACA,OACA,QACe;EACf,MAAM,UAAU,KAAK,cAAc,IAAI;AACvC,MAAI,YAAY,KAAA,EACd;EAGF,MAAM,cAAc,IAAI,OAAO,aAAa,KAAA;EAC5C,IAAI;AACJ,MAAI,YACF,KAAI,sBAAsB,QAAQ,MAAM,CACtC,uBAAsB,QAAQ;MAE9B,uBAAsB,IAAI,eAAe;GACvC,IAAI,OAAO,IAAI;GACf,SAAS;GACV,CAAC;MAGJ,uBAAsB;EAExB,MAAM,QAAQ,IAAI,YAAY,EAC5B,KAAK,CACH;GACE,IAAI;GACJ,MAAM,SAAS,QAAQ;GACvB,OAAO;GACR,EACD;GACE,IAAI;GACJ,MAAM,SAAS,QAAQ;GACvB,OAAO;GACR,CACF,EACF,CAAC;AACF,QAAM,KAAK,OAAO,MAAM,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.cjs","names":["PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const responseStatus =\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n\n // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n throw err;\n }\n};\n\n//
|
|
1
|
+
{"version":3,"file":"async_caller.cjs","names":["PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const responseStatus =\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n\n // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;AAOD,MAAM,+BAA+B,UAAmB;AACtD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KACG,aAAa,SACZ,OAAO,MAAM,YAAY,aACxB,MAAM,QAAQ,WAAW,SAAS,IACjC,MAAM,QAAQ,WAAW,aAAa,KACzC,UAAU,SACT,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,aAEjB,OAAM;AAER,KACE,UAAU,SACV,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,eAEf,OAAM;CAER,MAAM,iBACJ,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC7B,MAAM,SAAS,SACf,KAAA;CAGN,MAAM,eACJ,YAAY,SAAS,OAAO,MAAM,WAAW,WACzC,MAAM,SACN,KAAA;CAEN,MAAM,SAAS,kBAAkB;AACjC,KAAI,UAAU,gBAAgB,SAAS,CAAC,OAAO,CAC7C,OAAM;AAWR,MAPE,WAAW,S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oB,SAAS,SAAS;KAEvD;;AAEJ,SAAO,KAAK,KAAW,UAAU,GAAG,KAAK;;CAG3C,MAAM,GAAG,MAA0D;AACjE,SAAO,KAAK,WACV,MAAM,GAAG,KAAK,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.js","names":[],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const responseStatus =\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n\n // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n throw err;\n }\n};\n\n//
|
|
1
|
+
{"version":3,"file":"async_caller.js","names":[],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const responseStatus =\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n\n // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;AAOD,MAAM,+BAA+B,UAAmB;AACtD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KACG,aAAa,SACZ,OAAO,MAAM,YAAY,aACxB,MAAM,QAAQ,WAAW,SAAS,IACjC,MAAM,QAAQ,WAAW,aAAa,KACzC,UAAU,SACT,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS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,gBACE,SACA,UACA,GAAG,MAC8B;AAGjC,MAAI,QAAQ,QAAQ;GAClB,IAAI;AACJ,UAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,KAAK,EAClC,IAAI,SAAgB,GAAG,WAAW;AAChC,qBAAiB;AACf,YAAO,oBAAoB,QAAQ,OAAO,CAAC;;AAE7C,YAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,MAAM,CAAC;KACnE,CACH,CAAC,CAAC,cAAc;AACf,QAAI,QAAQ,UAAU,SACpB,SAAQ,OAAO,oBAAoB,SAAS,SAAS;KAEvD;;AAEJ,SAAO,KAAK,KAAW,UAAU,GAAG,KAAK;;CAG3C,MAAM,GAAG,MAA0D;AACjE,SAAO,KAAK,WACV,MAAM,GAAG,KAAK,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chunk_array.d.cts","names":[],"sources":["../../src/utils/chunk_array.ts"],"mappings":";cAAa,UAAA,MAAe,GAAA,
|
|
1
|
+
{"version":3,"file":"chunk_array.d.cts","names":[],"sources":["../../src/utils/chunk_array.ts"],"mappings":";cAAa,UAAA,MAAe,GAAA,EAAO,CAAA,IAAG,SAAA,aAAmB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chunk_array.d.ts","names":[],"sources":["../../src/utils/chunk_array.ts"],"mappings":";cAAa,UAAA,MAAe,GAAA,
|
|
1
|
+
{"version":3,"file":"chunk_array.d.ts","names":[],"sources":["../../src/utils/chunk_array.ts"],"mappings":";cAAa,UAAA,MAAe,GAAA,EAAO,CAAA,IAAG,SAAA,aAAmB,CAAA"}
|
package/dist/utils/env.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.cjs","names":[],"sources":["../../src/utils/env.ts"],"sourcesContent":["// Inlined from https://github.com/flexdinesh/browser-or-node\ndeclare global {\n const Deno:\n | {\n version: {\n deno: string;\n };\n env: {\n get: (name: string) => string | undefined;\n };\n }\n | undefined;\n}\n\nexport const isBrowser = () =>\n typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\nexport const isWebWorker = () =>\n typeof globalThis === \"object\" &&\n globalThis.constructor &&\n globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\n\nexport const isJsDom = () =>\n (typeof window !== \"undefined\" && window.name === \"nodejs\") ||\n (typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\"));\n\n// Supabase Edge Function provides a `Deno` global object\n// without `version` property\nexport const isDeno = () => typeof Deno !== \"undefined\";\n\n// Mark not-as-node if in Supabase Edge Function\nexport const isNode = () =>\n typeof process !== \"undefined\" &&\n typeof process.versions !== \"undefined\" &&\n typeof process.versions.node !== \"undefined\" &&\n !isDeno();\n\nexport const getEnv = () => {\n let env: string;\n if (isBrowser()) {\n env = \"browser\";\n } else if (isNode()) {\n env = \"node\";\n } else if (isWebWorker()) {\n env = \"webworker\";\n } else if (isJsDom()) {\n env = \"jsdom\";\n } else if (isDeno()) {\n env = \"deno\";\n } else {\n env = \"other\";\n }\n\n return env;\n};\n\nexport type RuntimeEnvironment = {\n library: string;\n libraryVersion?: string;\n runtime: string;\n runtimeVersion?: string;\n};\n\nlet runtimeEnvironment: RuntimeEnvironment | undefined;\n\nexport function getRuntimeEnvironment(): RuntimeEnvironment {\n if (runtimeEnvironment === undefined) {\n const env = getEnv();\n\n runtimeEnvironment = {\n library: \"langchain-js\",\n runtime: env,\n };\n }\n return runtimeEnvironment;\n}\n\nexport function getEnvironmentVariable(name: string): string | undefined {\n // Certain Deno setups will throw an error if you try to access environment variables\n // https://github.com/langchain-ai/langchainjs/issues/1412\n try {\n if (typeof process !== \"undefined\") {\n //
|
|
1
|
+
{"version":3,"file":"env.cjs","names":[],"sources":["../../src/utils/env.ts"],"sourcesContent":["// Inlined from https://github.com/flexdinesh/browser-or-node\ndeclare global {\n const Deno:\n | {\n version: {\n deno: string;\n };\n env: {\n get: (name: string) => string | undefined;\n };\n }\n | undefined;\n}\n\nexport const isBrowser = () =>\n typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\nexport const isWebWorker = () =>\n typeof globalThis === \"object\" &&\n globalThis.constructor &&\n globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\n\nexport const isJsDom = () =>\n (typeof window !== \"undefined\" && window.name === \"nodejs\") ||\n (typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\"));\n\n// Supabase Edge Function provides a `Deno` global object\n// without `version` property\nexport const isDeno = () => typeof Deno !== \"undefined\";\n\n// Mark not-as-node if in Supabase Edge Function\nexport const isNode = () =>\n typeof process !== \"undefined\" &&\n typeof process.versions !== \"undefined\" &&\n typeof process.versions.node !== \"undefined\" &&\n !isDeno();\n\nexport const getEnv = () => {\n let env: string;\n if (isBrowser()) {\n env = \"browser\";\n } else if (isNode()) {\n env = \"node\";\n } else if (isWebWorker()) {\n env = \"webworker\";\n } else if (isJsDom()) {\n env = \"jsdom\";\n } else if (isDeno()) {\n env = \"deno\";\n } else {\n env = \"other\";\n }\n\n return env;\n};\n\nexport type RuntimeEnvironment = {\n library: string;\n libraryVersion?: string;\n runtime: string;\n runtimeVersion?: string;\n};\n\nlet runtimeEnvironment: RuntimeEnvironment | undefined;\n\nexport function getRuntimeEnvironment(): RuntimeEnvironment {\n if (runtimeEnvironment === undefined) {\n const env = getEnv();\n\n runtimeEnvironment = {\n library: \"langchain-js\",\n runtime: env,\n };\n }\n return runtimeEnvironment;\n}\n\nexport function getEnvironmentVariable(name: string): string | undefined {\n // Certain Deno setups will throw an error if you try to access environment variables\n // https://github.com/langchain-ai/langchainjs/issues/1412\n try {\n if (typeof process !== \"undefined\") {\n // oxlint-disable-next-line no-process-env\n return process.env?.[name];\n } else if (isDeno()) {\n return Deno?.env.get(name);\n } else {\n return undefined;\n }\n } catch {\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAcA,MAAa,kBACX,OAAO,WAAW,eAAe,OAAO,OAAO,aAAa;AAE9D,MAAa,oBACX,OAAO,eAAe,YACtB,WAAW,eACX,WAAW,YAAY,SAAS;AAElC,MAAa,gBACV,OAAO,WAAW,eAAe,OAAO,SAAS,YACjD,OAAO,cAAc,eAAe,UAAU,UAAU,SAAS,QAAQ;AAI5E,MAAa,eAAe,OAAO,SAAS;AAG5C,MAAa,eACX,OAAO,YAAY,eACnB,OAAO,QAAQ,aAAa,eAC5B,OAAO,QAAQ,SAAS,SAAS,eACjC,CAAC,QAAQ;AAEX,MAAa,eAAe;CAC1B,IAAI;AACJ,KAAI,WAAW,CACb,OAAM;UACG,QAAQ,CACjB,OAAM;UACG,aAAa,CACtB,OAAM;UACG,SAAS,CAClB,OAAM;UACG,QAAQ,CACjB,OAAM;KAEN,OAAM;AAGR,QAAO;;AAUT,IAAI;AAEJ,SAAgB,wBAA4C;AAC1D,KAAI,uBAAuB,KAAA,EAGzB,sBAAqB;EACnB,SAAS;EACT,SAJU,QAAQ;EAKnB;AAEH,QAAO;;AAGT,SAAgB,uBAAuB,MAAkC;AAGvE,KAAI;AACF,MAAI,OAAO,YAAY,YAErB,QAAO,QAAQ,MAAM;WACZ,QAAQ,CACjB,QAAO,MAAM,IAAI,IAAI,KAAK;MAE1B;SAEI;AACN"}
|
package/dist/utils/env.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.js","names":[],"sources":["../../src/utils/env.ts"],"sourcesContent":["// Inlined from https://github.com/flexdinesh/browser-or-node\ndeclare global {\n const Deno:\n | {\n version: {\n deno: string;\n };\n env: {\n get: (name: string) => string | undefined;\n };\n }\n | undefined;\n}\n\nexport const isBrowser = () =>\n typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\nexport const isWebWorker = () =>\n typeof globalThis === \"object\" &&\n globalThis.constructor &&\n globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\n\nexport const isJsDom = () =>\n (typeof window !== \"undefined\" && window.name === \"nodejs\") ||\n (typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\"));\n\n// Supabase Edge Function provides a `Deno` global object\n// without `version` property\nexport const isDeno = () => typeof Deno !== \"undefined\";\n\n// Mark not-as-node if in Supabase Edge Function\nexport const isNode = () =>\n typeof process !== \"undefined\" &&\n typeof process.versions !== \"undefined\" &&\n typeof process.versions.node !== \"undefined\" &&\n !isDeno();\n\nexport const getEnv = () => {\n let env: string;\n if (isBrowser()) {\n env = \"browser\";\n } else if (isNode()) {\n env = \"node\";\n } else if (isWebWorker()) {\n env = \"webworker\";\n } else if (isJsDom()) {\n env = \"jsdom\";\n } else if (isDeno()) {\n env = \"deno\";\n } else {\n env = \"other\";\n }\n\n return env;\n};\n\nexport type RuntimeEnvironment = {\n library: string;\n libraryVersion?: string;\n runtime: string;\n runtimeVersion?: string;\n};\n\nlet runtimeEnvironment: RuntimeEnvironment | undefined;\n\nexport function getRuntimeEnvironment(): RuntimeEnvironment {\n if (runtimeEnvironment === undefined) {\n const env = getEnv();\n\n runtimeEnvironment = {\n library: \"langchain-js\",\n runtime: env,\n };\n }\n return runtimeEnvironment;\n}\n\nexport function getEnvironmentVariable(name: string): string | undefined {\n // Certain Deno setups will throw an error if you try to access environment variables\n // https://github.com/langchain-ai/langchainjs/issues/1412\n try {\n if (typeof process !== \"undefined\") {\n //
|
|
1
|
+
{"version":3,"file":"env.js","names":[],"sources":["../../src/utils/env.ts"],"sourcesContent":["// Inlined from https://github.com/flexdinesh/browser-or-node\ndeclare global {\n const Deno:\n | {\n version: {\n deno: string;\n };\n env: {\n get: (name: string) => string | undefined;\n };\n }\n | undefined;\n}\n\nexport const isBrowser = () =>\n typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\nexport const isWebWorker = () =>\n typeof globalThis === \"object\" &&\n globalThis.constructor &&\n globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\n\nexport const isJsDom = () =>\n (typeof window !== \"undefined\" && window.name === \"nodejs\") ||\n (typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\"));\n\n// Supabase Edge Function provides a `Deno` global object\n// without `version` property\nexport const isDeno = () => typeof Deno !== \"undefined\";\n\n// Mark not-as-node if in Supabase Edge Function\nexport const isNode = () =>\n typeof process !== \"undefined\" &&\n typeof process.versions !== \"undefined\" &&\n typeof process.versions.node !== \"undefined\" &&\n !isDeno();\n\nexport const getEnv = () => {\n let env: string;\n if (isBrowser()) {\n env = \"browser\";\n } else if (isNode()) {\n env = \"node\";\n } else if (isWebWorker()) {\n env = \"webworker\";\n } else if (isJsDom()) {\n env = \"jsdom\";\n } else if (isDeno()) {\n env = \"deno\";\n } else {\n env = \"other\";\n }\n\n return env;\n};\n\nexport type RuntimeEnvironment = {\n library: string;\n libraryVersion?: string;\n runtime: string;\n runtimeVersion?: string;\n};\n\nlet runtimeEnvironment: RuntimeEnvironment | undefined;\n\nexport function getRuntimeEnvironment(): RuntimeEnvironment {\n if (runtimeEnvironment === undefined) {\n const env = getEnv();\n\n runtimeEnvironment = {\n library: \"langchain-js\",\n runtime: env,\n };\n }\n return runtimeEnvironment;\n}\n\nexport function getEnvironmentVariable(name: string): string | undefined {\n // Certain Deno setups will throw an error if you try to access environment variables\n // https://github.com/langchain-ai/langchainjs/issues/1412\n try {\n if (typeof process !== \"undefined\") {\n // oxlint-disable-next-line no-process-env\n return process.env?.[name];\n } else if (isDeno()) {\n return Deno?.env.get(name);\n } else {\n return undefined;\n }\n } catch {\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAcA,MAAa,kBACX,OAAO,WAAW,eAAe,OAAO,OAAO,aAAa;AAE9D,MAAa,oBACX,OAAO,eAAe,YACtB,WAAW,eACX,WAAW,YAAY,SAAS;AAElC,MAAa,gBACV,OAAO,WAAW,eAAe,OAAO,SAAS,YACjD,OAAO,cAAc,eAAe,UAAU,UAAU,SAAS,QAAQ;AAI5E,MAAa,eAAe,OAAO,SAAS;AAG5C,MAAa,eACX,OAAO,YAAY,eACnB,OAAO,QAAQ,aAAa,eAC5B,OAAO,QAAQ,SAAS,SAAS,eACjC,CAAC,QAAQ;AAEX,MAAa,eAAe;CAC1B,IAAI;AACJ,KAAI,WAAW,CACb,OAAM;UACG,QAAQ,CACjB,OAAM;UACG,aAAa,CACtB,OAAM;UACG,SAAS,CAClB,OAAM;UACG,QAAQ,CACjB,OAAM;KAEN,OAAM;AAGR,QAAO;;AAUT,IAAI;AAEJ,SAAgB,wBAA4C;AAC1D,KAAI,uBAAuB,KAAA,EAGzB,sBAAqB;EACnB,SAAS;EACT,SAJU,QAAQ;EAKnB;AAEH,QAAO;;AAGT,SAAgB,uBAAuB,MAAkC;AAGvE,KAAI;AACF,MAAI,OAAO,YAAY,YAErB,QAAO,QAAQ,MAAM;WACZ,QAAQ,CACjB,QAAO,MAAM,IAAI,IAAI,KAAK;MAE1B;SAEI;AACN"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event_source_parse.cjs","names":["IterableReadableStream"],"sources":["../../src/utils/event_source_parse.ts"],"sourcesContent":["/* eslint-disable prefer-template */\n/* eslint-disable default-case */\n// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts\n// due to a packaging issue in the original.\n// MIT License\nimport { IterableReadableStream } from \"./stream.js\";\n\nexport const EventStreamContentType = \"text/event-stream\";\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventSourceMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * Converts a ReadableStream into a callback pattern.\n * @param stream The input ReadableStream.\n * @param onChunk A function that will be called on each new byte chunk in the stream.\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\n */\nexport async function getBytes(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: ReadableStream<Uint8Array> | AsyncIterable<any>,\n onChunk: (arr: Uint8Array, flush?: boolean) => void\n) {\n // TODO: Use Async iteration for both cases?\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (stream instanceof ReadableStream) {\n const reader = stream.getReader();\n // CHANGED: Introduced a \"flush\" mechanism to process potential pending messages when the stream ends.\n // This change is essential to ensure that we capture every last piece of information from streams,\n // such as those from Azure OpenAI, which may not terminate with a blank line. Without this\n // mechanism, we risk ignoring a possibly significant last message.\n // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.\n while (true) {\n const result = await reader.read();\n if (result.done) {\n onChunk(new Uint8Array(), true);\n break;\n }\n onChunk(result.value);\n }\n } else {\n try {\n // Handle Node.js Readable streams with async iteration\n for await (const chunk of stream) {\n onChunk(new Uint8Array(chunk));\n }\n onChunk(new Uint8Array(), true);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n throw new Error(\n [\n \"Parsing event source stream failed.\",\n \"Ensure your implementation of fetch returns a web or Node readable stream.\",\n `Error: ${e.message}`,\n ].join(\"\\n\")\n );\n }\n }\n}\n\nconst enum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Parses arbitary byte chunks into EventSource line buffers.\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n.\n * @param onLine A function that will be called on each new EventSource line.\n * @returns A function that should be called for each incoming byte chunk.\n */\nexport function getLines(\n onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void\n) {\n let buffer: Uint8Array | undefined;\n let position: number; // current read position\n let fieldLength: number; // length of the `field` portion of the line\n let discardTrailingNewline = false;\n\n // return a function that can process each incoming byte chunk:\n return function onChunk(arr: Uint8Array, flush?: boolean) {\n if (flush) {\n onLine(arr, 0, true);\n return;\n }\n\n if (buffer === undefined) {\n buffer = arr;\n position = 0;\n fieldLength = -1;\n } else {\n // we're still parsing the old line. Append the new bytes into buffer:\n buffer = concat(buffer, arr);\n }\n\n const bufLength = buffer.length;\n let lineStart = 0; // index where the current line starts\n while (position < bufLength) {\n if (discardTrailingNewline) {\n if (buffer[position] === ControlChars.NewLine) {\n lineStart = ++position; // skip to next char\n }\n\n discardTrailingNewline = false;\n }\n\n // start looking forward till the end of line:\n let lineEnd = -1; // index of the \\r or \\n char\n for (; position < bufLength && lineEnd === -1; ++position) {\n switch (buffer[position]) {\n case ControlChars.Colon:\n if (fieldLength === -1) {\n // first colon in line\n fieldLength = position - lineStart;\n }\n break;\n // @ts-expect-error \\r case below should fallthrough to \\n:\n case ControlChars.CarriageReturn:\n discardTrailingNewline = true;\n // eslint-disable-next-line no-fallthrough\n case ControlChars.NewLine:\n lineEnd = position;\n break;\n }\n }\n\n if (lineEnd === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next arr and then continue parsing:\n break;\n }\n\n // we've reached the line end, send it out:\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\n lineStart = position; // we're now on the next line\n fieldLength = -1;\n }\n\n if (lineStart === bufLength) {\n buffer = undefined; // we've finished reading it\n } else if (lineStart !== 0) {\n // Create a new view into buffer beginning at lineStart so we don't\n // need to copy over the previous lines when we get the new arr:\n buffer = buffer.subarray(lineStart);\n position -= lineStart;\n }\n };\n}\n\n/**\n * Parses line buffers into EventSourceMessages.\n * @param onId A function that will be called on each `id` field.\n * @param onRetry A function that will be called on each `retry` field.\n * @param onMessage A function that will be called on each message.\n * @returns A function that should be called for each incoming line buffer.\n */\nexport function getMessages(\n onMessage?: (msg: EventSourceMessage) => void,\n onId?: (id: string) => void,\n onRetry?: (retry: number) => void\n) {\n let message = newMessage();\n const decoder = new TextDecoder();\n\n // return a function that can process each incoming line buffer:\n return function onLine(\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) {\n if (flush) {\n if (!isEmpty(message)) {\n onMessage?.(message);\n message = newMessage();\n }\n return;\n }\n\n if (line.length === 0) {\n // empty line denotes end of message. Trigger the callback and start a new message:\n onMessage?.(message);\n message = newMessage();\n } else if (fieldLength > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLength));\n const valueOffset =\n fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n // if this message already has data, append the new value to the old.\n // otherwise, just set to the new value:\n message.data = message.data ? message.data + \"\\n\" + value : value; // otherwise,\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n onId?.((message.id = value));\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!Number.isNaN(retry)) {\n // per spec, ignore non-integers\n onRetry?.((message.retry = retry));\n }\n break;\n }\n }\n }\n };\n}\n\nfunction concat(a: Uint8Array, b: Uint8Array) {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction newMessage(): EventSourceMessage {\n // data, event, and id must be initialized to empty strings:\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n // retry should be initialized to undefined so we return a consistent shape\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\n return {\n data: \"\",\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nexport function convertEventStreamToIterableReadableDataStream(\n stream: ReadableStream,\n onMetadataEvent?: (e: unknown) => unknown\n) {\n const dataStream = new ReadableStream({\n async start(controller) {\n const enqueueLine = getMessages((msg) => {\n if (msg.event === \"error\") {\n throw new Error(msg.data ?? \"Unspecified event streaming error.\");\n } else if (msg.event === \"metadata\") {\n onMetadataEvent?.(msg);\n } else {\n if (msg.data) controller.enqueue(msg.data);\n }\n });\n const onLine = (\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) => {\n enqueueLine(line, fieldLength, flush);\n if (flush) controller.close();\n };\n await getBytes(stream, getLines(onLine));\n },\n });\n return IterableReadableStream.fromReadableStream(dataStream);\n}\n\nfunction isEmpty(message: EventSourceMessage): boolean {\n return (\n message.data === \"\" &&\n message.event === \"\" &&\n message.id === \"\" &&\n message.retry === undefined\n );\n}\n"],"mappings":";;;;;;;;;;;AAOA,MAAa,yBAAyB;;;;;;;AAuBtC,eAAsB,SAEpB,QACA,SACA;AAGA,KAAI,kBAAkB,gBAAgB;EACpC,MAAM,SAAS,OAAO,WAAW;AAMjC,SAAO,MAAM;GACX,MAAM,SAAS,MAAM,OAAO,MAAM;AAClC,OAAI,OAAO,MAAM;AACf,YAAQ,IAAI,YAAY,EAAE,KAAK;AAC/B;;AAEF,WAAQ,OAAO,MAAM;;OAGvB,KAAI;AAEF,aAAW,MAAM,SAAS,OACxB,SAAQ,IAAI,WAAW,MAAM,CAAC;AAEhC,UAAQ,IAAI,YAAY,EAAE,KAAK;UAExB,GAAQ;AACf,QAAM,IAAI,MACR;GACE;GACA;GACA,UAAU,EAAE;GACb,CAAC,KAAK,KAAK,CACb;;;AAKP,IAAW,eAAX,yBAAA,cAAA;AACE,cAAA,aAAA,aAAA,MAAA;AACA,cAAA,aAAA,oBAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;;EAJS,gBAAA,EAAA,CAKV;;;;;;;AAQD,SAAgB,SACd,QACA;CACA,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI,yBAAyB;AAG7B,QAAO,SAAS,QAAQ,KAAiB,OAAiB;AACxD,MAAI,OAAO;AACT,UAAO,KAAK,GAAG,KAAK;AACpB;;AAGF,MAAI,WAAW,KAAA,GAAW;AACxB,YAAS;AACT,cAAW;AACX,iBAAc;QAGd,UAAS,OAAO,QAAQ,IAAI;EAG9B,MAAM,YAAY,OAAO;EACzB,IAAI,YAAY;AAChB,SAAO,WAAW,WAAW;AAC3B,OAAI,wBAAwB;AAC1B,QAAI,OAAO,cAAc,aAAa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AEtB,WAAW,QAAQ,QAAQ,MAAO;AAEpC;;;;;;AAOV,SAAS,OAAO,GAAe,GAAe;CAC5C,MAAM,MAAM,IAAI,WAAW,EAAE,SAAS,EAAE,OAAO;AAC/C,KAAI,IAAI,EAAE;AACV,KAAI,IAAI,GAAG,EAAE,OAAO;AACpB,QAAO;;AAGT,SAAS,aAAiC;AAKxC,QAAO;EACL,MAAM;EACN,OAAO;EACP,IAAI;EACJ,OAAO,KAAA;EACR;;AAGH,SAAgB,+CACd,QACA,iBACA;CACA,MAAM,aAAa,IAAI,eAAe,EACpC,MAAM,MAAM,YAAY;EACtB,MAAM,cAAc,aAAa,QAAQ;AACvC,OAAI,IAAI,UAAU,QAChB,OAAM,IAAI,MAAM,IAAI,QAAQ,qCAAqC;YACxD,IAAI,UAAU,WACvB,mBAAkB,IAAI;YAElB,IAAI,KAAM,YAAW,QAAQ,IAAI,KAAK;IAE5C;EACF,MAAM,UACJ,MACA,aACA,UACG;AACH,eAAY,MAAM,aAAa,MAAM;AACrC,OAAI,MAAO,YAAW,OAAO;;AAE/B,QAAM,SAAS,QAAQ,SAAS,OAAO,CAAC;IAE3C,CAAC;AACF,QAAOA,qBAAAA,uBAAuB,mBAAmB,WAAW;;AAG9D,SAAS,QAAQ,SAAsC;AACrD,QACE,QAAQ,SAAS,MACjB,QAAQ,UAAU,MAClB,QAAQ,OAAO,MACf,QAAQ,UAAU,KAAA"}
|
|
1
|
+
{"version":3,"file":"event_source_parse.cjs","names":["IterableReadableStream"],"sources":["../../src/utils/event_source_parse.ts"],"sourcesContent":["/* oxlint-disable prefer-template */\n/* oxlint-disable default-case */\n// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts\n// due to a packaging issue in the original.\n// MIT License\nimport { IterableReadableStream } from \"./stream.js\";\n\nexport const EventStreamContentType = \"text/event-stream\";\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventSourceMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * Converts a ReadableStream into a callback pattern.\n * @param stream The input ReadableStream.\n * @param onChunk A function that will be called on each new byte chunk in the stream.\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\n */\nexport async function getBytes(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n stream: ReadableStream<Uint8Array> | AsyncIterable<any>,\n onChunk: (arr: Uint8Array, flush?: boolean) => void\n) {\n // TODO: Use Async iteration for both cases?\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (stream instanceof ReadableStream) {\n const reader = stream.getReader();\n // CHANGED: Introduced a \"flush\" mechanism to process potential pending messages when the stream ends.\n // This change is essential to ensure that we capture every last piece of information from streams,\n // such as those from Azure OpenAI, which may not terminate with a blank line. Without this\n // mechanism, we risk ignoring a possibly significant last message.\n // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.\n while (true) {\n const result = await reader.read();\n if (result.done) {\n onChunk(new Uint8Array(), true);\n break;\n }\n onChunk(result.value);\n }\n } else {\n try {\n // Handle Node.js Readable streams with async iteration\n for await (const chunk of stream) {\n onChunk(new Uint8Array(chunk));\n }\n onChunk(new Uint8Array(), true);\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n throw new Error(\n [\n \"Parsing event source stream failed.\",\n \"Ensure your implementation of fetch returns a web or Node readable stream.\",\n `Error: ${e.message}`,\n ].join(\"\\n\")\n );\n }\n }\n}\n\nconst enum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Parses arbitary byte chunks into EventSource line buffers.\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n.\n * @param onLine A function that will be called on each new EventSource line.\n * @returns A function that should be called for each incoming byte chunk.\n */\nexport function getLines(\n onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void\n) {\n let buffer: Uint8Array | undefined;\n let position: number; // current read position\n let fieldLength: number; // length of the `field` portion of the line\n let discardTrailingNewline = false;\n\n // return a function that can process each incoming byte chunk:\n return function onChunk(arr: Uint8Array, flush?: boolean) {\n if (flush) {\n onLine(arr, 0, true);\n return;\n }\n\n if (buffer === undefined) {\n buffer = arr;\n position = 0;\n fieldLength = -1;\n } else {\n // we're still parsing the old line. Append the new bytes into buffer:\n buffer = concat(buffer, arr);\n }\n\n const bufLength = buffer.length;\n let lineStart = 0; // index where the current line starts\n while (position < bufLength) {\n if (discardTrailingNewline) {\n if (buffer[position] === ControlChars.NewLine) {\n lineStart = ++position; // skip to next char\n }\n\n discardTrailingNewline = false;\n }\n\n // start looking forward till the end of line:\n let lineEnd = -1; // index of the \\r or \\n char\n for (; position < bufLength && lineEnd === -1; ++position) {\n switch (buffer[position]) {\n case ControlChars.Colon:\n if (fieldLength === -1) {\n // first colon in line\n fieldLength = position - lineStart;\n }\n break;\n // @ts-expect-error \\r case below should fallthrough to \\n:\n case ControlChars.CarriageReturn:\n discardTrailingNewline = true;\n // oxlint-disable-next-line no-fallthrough\n case ControlChars.NewLine:\n lineEnd = position;\n break;\n }\n }\n\n if (lineEnd === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next arr and then continue parsing:\n break;\n }\n\n // we've reached the line end, send it out:\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\n lineStart = position; // we're now on the next line\n fieldLength = -1;\n }\n\n if (lineStart === bufLength) {\n buffer = undefined; // we've finished reading it\n } else if (lineStart !== 0) {\n // Create a new view into buffer beginning at lineStart so we don't\n // need to copy over the previous lines when we get the new arr:\n buffer = buffer.subarray(lineStart);\n position -= lineStart;\n }\n };\n}\n\n/**\n * Parses line buffers into EventSourceMessages.\n * @param onId A function that will be called on each `id` field.\n * @param onRetry A function that will be called on each `retry` field.\n * @param onMessage A function that will be called on each message.\n * @returns A function that should be called for each incoming line buffer.\n */\nexport function getMessages(\n onMessage?: (msg: EventSourceMessage) => void,\n onId?: (id: string) => void,\n onRetry?: (retry: number) => void\n) {\n let message = newMessage();\n const decoder = new TextDecoder();\n\n // return a function that can process each incoming line buffer:\n return function onLine(\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) {\n if (flush) {\n if (!isEmpty(message)) {\n onMessage?.(message);\n message = newMessage();\n }\n return;\n }\n\n if (line.length === 0) {\n // empty line denotes end of message. Trigger the callback and start a new message:\n onMessage?.(message);\n message = newMessage();\n } else if (fieldLength > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLength));\n const valueOffset =\n fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n // if this message already has data, append the new value to the old.\n // otherwise, just set to the new value:\n message.data = message.data ? message.data + \"\\n\" + value : value; // otherwise,\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n onId?.((message.id = value));\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!Number.isNaN(retry)) {\n // per spec, ignore non-integers\n onRetry?.((message.retry = retry));\n }\n break;\n }\n }\n }\n };\n}\n\nfunction concat(a: Uint8Array, b: Uint8Array) {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction newMessage(): EventSourceMessage {\n // data, event, and id must be initialized to empty strings:\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n // retry should be initialized to undefined so we return a consistent shape\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\n return {\n data: \"\",\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nexport function convertEventStreamToIterableReadableDataStream(\n stream: ReadableStream,\n onMetadataEvent?: (e: unknown) => unknown\n) {\n const dataStream = new ReadableStream({\n async start(controller) {\n const enqueueLine = getMessages((msg) => {\n if (msg.event === \"error\") {\n throw new Error(msg.data ?? \"Unspecified event streaming error.\");\n } else if (msg.event === \"metadata\") {\n onMetadataEvent?.(msg);\n } else {\n if (msg.data) controller.enqueue(msg.data);\n }\n });\n const onLine = (\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) => {\n enqueueLine(line, fieldLength, flush);\n if (flush) controller.close();\n };\n await getBytes(stream, getLines(onLine));\n },\n });\n return IterableReadableStream.fromReadableStream(dataStream);\n}\n\nfunction isEmpty(message: EventSourceMessage): boolean {\n return (\n message.data === \"\" &&\n message.event === \"\" &&\n message.id === \"\" &&\n message.retry === undefined\n );\n}\n"],"mappings":";;;;;;;;;;;AAOA,MAAa,yBAAyB;;;;;;;AAuBtC,eAAsB,SAEpB,QACA,SACA;AAGA,KAAI,kBAAkB,gBAAgB;EACpC,MAAM,SAAS,OAAO,WAAW;AAMjC,SAAO,MAAM;GACX,MAAM,SAAS,MAAM,OAAO,MAAM;AAClC,OAAI,OAAO,MAAM;AACf,YAAQ,IAAI,YAAY,EAAE,KAAK;AAC/B;;AAEF,WAAQ,OAAO,MAAM;;OAGvB,KAAI;AAEF,aAAW,MAAM,SAAS,OACxB,SAAQ,IAAI,WAAW,MAAM,CAAC;AAEhC,UAAQ,IAAI,YAAY,EAAE,KAAK;UAExB,GAAQ;AACf,QAAM,IAAI,MACR;GACE;GACA;GACA,UAAU,EAAE;GACb,CAAC,KAAK,KAAK,CACb;;;AAKP,IAAW,eAAX,yBAAA,cAAA;AACE,cAAA,aAAA,aAAA,MAAA;AACA,cAAA,aAAA,oBAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;;EAJS,gBAAA,EAAA,CAKV;;;;;;;AAQD,SAAgB,SACd,QACA;CACA,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI,yBAAyB;AAG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int8Array> | AsyncIterable<any
|
|
|
29
29
|
* @param onLine A function that will be called on each new EventSource line.
|
|
30
30
|
* @returns A function that should be called for each incoming byte chunk.
|
|
31
31
|
*/
|
|
32
|
-
declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array
|
|
32
|
+
declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array, flush?: boolean) => void;
|
|
33
33
|
/**
|
|
34
34
|
* Parses line buffers into EventSourceMessages.
|
|
35
35
|
* @param onId A function that will be called on each `id` field.
|
|
@@ -37,7 +37,7 @@ declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?
|
|
|
37
37
|
* @param onMessage A function that will be called on each message.
|
|
38
38
|
* @returns A function that should be called for each incoming line buffer.
|
|
39
39
|
*/
|
|
40
|
-
declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array
|
|
40
|
+
declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;
|
|
41
41
|
declare function convertEventStreamToIterableReadableDataStream(stream: ReadableStream, onMetadataEvent?: (e: unknown) => unknown): IterableReadableStream<any>;
|
|
42
42
|
//#endregion
|
|
43
43
|
export { EventSourceMessage, EventStreamContentType, convertEventStreamToIterableReadableDataStream, getBytes, getLines, getMessages };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event_source_parse.d.cts","names":[],"sources":["../../src/utils/event_source_parse.ts"],"mappings":";;;cAOa,sBAAA;;AAAb;;;UAMiB,kBAAA;EANkB;EAQjC,EAAA;EAFiC;EAIjC,KAAA;EAJiC;EAMjC,IAAA;EAFA;EAIA,KAAA;AAAA;;;AASF;;;;iBAAsB,QAAA,CAEpB,MAAA,EAAQ,cAAA,CAAe,UAAA,IAAc,aAAA,OACrC,OAAA,GAAU,GAAA,EAAK,UAAA,EAAY,KAAA,sBAAwB,OAAA;;;;;;;iBAoDrC,QAAA,CACd,MAAA,GAAS,IAAA,EAAM,UAAA,EAAY,WAAA,UAAqB,KAAA,uBAAwB,GAAA,
|
|
1
|
+
{"version":3,"file":"event_source_parse.d.cts","names":[],"sources":["../../src/utils/event_source_parse.ts"],"mappings":";;;cAOa,sBAAA;;AAAb;;;UAMiB,kBAAA;EANkB;EAQjC,EAAA;EAFiC;EAIjC,KAAA;EAJiC;EAMjC,IAAA;EAFA;EAIA,KAAA;AAAA;;;AASF;;;;iBAAsB,QAAA,CAEpB,MAAA,EAAQ,cAAA,CAAe,UAAA,IAAc,aAAA,OACrC,OAAA,GAAU,GAAA,EAAK,UAAA,EAAY,KAAA,sBAAwB,OAAA;;;;;;;iBAoDrC,QAAA,CACd,MAAA,GAAS,IAAA,EAAM,UAAA,EAAY,WAAA,UAAqB,KAAA,uBAAwB,GAAA,EAQ3C,UAAA,EAAU,KAAA;;;;;;;;iBA4EzB,WAAA,CACd,SAAA,IAAa,GAAA,EAAK,kBAAA,WAClB,IAAA,IAAQ,EAAA,mBACR,OAAA,IAAW,KAAA,qBAAsB,IAAA,EAOzB,UAAA,EAAU,WAAA,UACG,KAAA;AAAA,iBAqEP,8CAAA,CACd,MAAA,EAAQ,cAAA,EACR,eAAA,IAAmB,CAAA,wBAAsB,sBAAA"}
|
|
@@ -29,7 +29,7 @@ declare function getBytes(stream: ReadableStream<Uint8Array> | AsyncIterable<any
|
|
|
29
29
|
* @param onLine A function that will be called on each new EventSource line.
|
|
30
30
|
* @returns A function that should be called for each incoming byte chunk.
|
|
31
31
|
*/
|
|
32
|
-
declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array
|
|
32
|
+
declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array, flush?: boolean) => void;
|
|
33
33
|
/**
|
|
34
34
|
* Parses line buffers into EventSourceMessages.
|
|
35
35
|
* @param onId A function that will be called on each `id` field.
|
|
@@ -37,7 +37,7 @@ declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?
|
|
|
37
37
|
* @param onMessage A function that will be called on each message.
|
|
38
38
|
* @returns A function that should be called for each incoming line buffer.
|
|
39
39
|
*/
|
|
40
|
-
declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array
|
|
40
|
+
declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;
|
|
41
41
|
declare function convertEventStreamToIterableReadableDataStream(stream: ReadableStream, onMetadataEvent?: (e: unknown) => unknown): IterableReadableStream<any>;
|
|
42
42
|
//#endregion
|
|
43
43
|
export { EventSourceMessage, EventStreamContentType, convertEventStreamToIterableReadableDataStream, getBytes, getLines, getMessages };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event_source_parse.d.ts","names":[],"sources":["../../src/utils/event_source_parse.ts"],"mappings":";;;cAOa,sBAAA;;AAAb;;;UAMiB,kBAAA;EANkB;EAQjC,EAAA;EAFiC;EAIjC,KAAA;EAJiC;EAMjC,IAAA;EAFA;EAIA,KAAA;AAAA;;;AASF;;;;iBAAsB,QAAA,CAEpB,MAAA,EAAQ,cAAA,CAAe,UAAA,IAAc,aAAA,OACrC,OAAA,GAAU,GAAA,EAAK,UAAA,EAAY,KAAA,sBAAwB,OAAA;;;;;;;iBAoDrC,QAAA,CACd,MAAA,GAAS,IAAA,EAAM,UAAA,EAAY,WAAA,UAAqB,KAAA,uBAAwB,GAAA,
|
|
1
|
+
{"version":3,"file":"event_source_parse.d.ts","names":[],"sources":["../../src/utils/event_source_parse.ts"],"mappings":";;;cAOa,sBAAA;;AAAb;;;UAMiB,kBAAA;EANkB;EAQjC,EAAA;EAFiC;EAIjC,KAAA;EAJiC;EAMjC,IAAA;EAFA;EAIA,KAAA;AAAA;;;AASF;;;;iBAAsB,QAAA,CAEpB,MAAA,EAAQ,cAAA,CAAe,UAAA,IAAc,aAAA,OACrC,OAAA,GAAU,GAAA,EAAK,UAAA,EAAY,KAAA,sBAAwB,OAAA;;;;;;;iBAoDrC,QAAA,CACd,MAAA,GAAS,IAAA,EAAM,UAAA,EAAY,WAAA,UAAqB,KAAA,uBAAwB,GAAA,EAQ3C,UAAA,EAAU,KAAA;;;;;;;;iBA4EzB,WAAA,CACd,SAAA,IAAa,GAAA,EAAK,kBAAA,WAClB,IAAA,IAAQ,EAAA,mBACR,OAAA,IAAW,KAAA,qBAAsB,IAAA,EAOzB,UAAA,EAAU,WAAA,UACG,KAAA;AAAA,iBAqEP,8CAAA,CACd,MAAA,EAAQ,cAAA,EACR,eAAA,IAAmB,CAAA,wBAAsB,sBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event_source_parse.js","names":[],"sources":["../../src/utils/event_source_parse.ts"],"sourcesContent":["/* eslint-disable prefer-template */\n/* eslint-disable default-case */\n// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts\n// due to a packaging issue in the original.\n// MIT License\nimport { IterableReadableStream } from \"./stream.js\";\n\nexport const EventStreamContentType = \"text/event-stream\";\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventSourceMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * Converts a ReadableStream into a callback pattern.\n * @param stream The input ReadableStream.\n * @param onChunk A function that will be called on each new byte chunk in the stream.\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\n */\nexport async function getBytes(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: ReadableStream<Uint8Array> | AsyncIterable<any>,\n onChunk: (arr: Uint8Array, flush?: boolean) => void\n) {\n // TODO: Use Async iteration for both cases?\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (stream instanceof ReadableStream) {\n const reader = stream.getReader();\n // CHANGED: Introduced a \"flush\" mechanism to process potential pending messages when the stream ends.\n // This change is essential to ensure that we capture every last piece of information from streams,\n // such as those from Azure OpenAI, which may not terminate with a blank line. Without this\n // mechanism, we risk ignoring a possibly significant last message.\n // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.\n while (true) {\n const result = await reader.read();\n if (result.done) {\n onChunk(new Uint8Array(), true);\n break;\n }\n onChunk(result.value);\n }\n } else {\n try {\n // Handle Node.js Readable streams with async iteration\n for await (const chunk of stream) {\n onChunk(new Uint8Array(chunk));\n }\n onChunk(new Uint8Array(), true);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n throw new Error(\n [\n \"Parsing event source stream failed.\",\n \"Ensure your implementation of fetch returns a web or Node readable stream.\",\n `Error: ${e.message}`,\n ].join(\"\\n\")\n );\n }\n }\n}\n\nconst enum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Parses arbitary byte chunks into EventSource line buffers.\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n.\n * @param onLine A function that will be called on each new EventSource line.\n * @returns A function that should be called for each incoming byte chunk.\n */\nexport function getLines(\n onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void\n) {\n let buffer: Uint8Array | undefined;\n let position: number; // current read position\n let fieldLength: number; // length of the `field` portion of the line\n let discardTrailingNewline = false;\n\n // return a function that can process each incoming byte chunk:\n return function onChunk(arr: Uint8Array, flush?: boolean) {\n if (flush) {\n onLine(arr, 0, true);\n return;\n }\n\n if (buffer === undefined) {\n buffer = arr;\n position = 0;\n fieldLength = -1;\n } else {\n // we're still parsing the old line. Append the new bytes into buffer:\n buffer = concat(buffer, arr);\n }\n\n const bufLength = buffer.length;\n let lineStart = 0; // index where the current line starts\n while (position < bufLength) {\n if (discardTrailingNewline) {\n if (buffer[position] === ControlChars.NewLine) {\n lineStart = ++position; // skip to next char\n }\n\n discardTrailingNewline = false;\n }\n\n // start looking forward till the end of line:\n let lineEnd = -1; // index of the \\r or \\n char\n for (; position < bufLength && lineEnd === -1; ++position) {\n switch (buffer[position]) {\n case ControlChars.Colon:\n if (fieldLength === -1) {\n // first colon in line\n fieldLength = position - lineStart;\n }\n break;\n // @ts-expect-error \\r case below should fallthrough to \\n:\n case ControlChars.CarriageReturn:\n discardTrailingNewline = true;\n // eslint-disable-next-line no-fallthrough\n case ControlChars.NewLine:\n lineEnd = position;\n break;\n }\n }\n\n if (lineEnd === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next arr and then continue parsing:\n break;\n }\n\n // we've reached the line end, send it out:\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\n lineStart = position; // we're now on the next line\n fieldLength = -1;\n }\n\n if (lineStart === bufLength) {\n buffer = undefined; // we've finished reading it\n } else if (lineStart !== 0) {\n // Create a new view into buffer beginning at lineStart so we don't\n // need to copy over the previous lines when we get the new arr:\n buffer = buffer.subarray(lineStart);\n position -= lineStart;\n }\n };\n}\n\n/**\n * Parses line buffers into EventSourceMessages.\n * @param onId A function that will be called on each `id` field.\n * @param onRetry A function that will be called on each `retry` field.\n * @param onMessage A function that will be called on each message.\n * @returns A function that should be called for each incoming line buffer.\n */\nexport function getMessages(\n onMessage?: (msg: EventSourceMessage) => void,\n onId?: (id: string) => void,\n onRetry?: (retry: number) => void\n) {\n let message = newMessage();\n const decoder = new TextDecoder();\n\n // return a function that can process each incoming line buffer:\n return function onLine(\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) {\n if (flush) {\n if (!isEmpty(message)) {\n onMessage?.(message);\n message = newMessage();\n }\n return;\n }\n\n if (line.length === 0) {\n // empty line denotes end of message. Trigger the callback and start a new message:\n onMessage?.(message);\n message = newMessage();\n } else if (fieldLength > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLength));\n const valueOffset =\n fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n // if this message already has data, append the new value to the old.\n // otherwise, just set to the new value:\n message.data = message.data ? message.data + \"\\n\" + value : value; // otherwise,\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n onId?.((message.id = value));\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!Number.isNaN(retry)) {\n // per spec, ignore non-integers\n onRetry?.((message.retry = retry));\n }\n break;\n }\n }\n }\n };\n}\n\nfunction concat(a: Uint8Array, b: Uint8Array) {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction newMessage(): EventSourceMessage {\n // data, event, and id must be initialized to empty strings:\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n // retry should be initialized to undefined so we return a consistent shape\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\n return {\n data: \"\",\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nexport function convertEventStreamToIterableReadableDataStream(\n stream: ReadableStream,\n onMetadataEvent?: (e: unknown) => unknown\n) {\n const dataStream = new ReadableStream({\n async start(controller) {\n const enqueueLine = getMessages((msg) => {\n if (msg.event === \"error\") {\n throw new Error(msg.data ?? \"Unspecified event streaming error.\");\n } else if (msg.event === \"metadata\") {\n onMetadataEvent?.(msg);\n } else {\n if (msg.data) controller.enqueue(msg.data);\n }\n });\n const onLine = (\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) => {\n enqueueLine(line, fieldLength, flush);\n if (flush) controller.close();\n };\n await getBytes(stream, getLines(onLine));\n },\n });\n return IterableReadableStream.fromReadableStream(dataStream);\n}\n\nfunction isEmpty(message: EventSourceMessage): boolean {\n return (\n message.data === \"\" &&\n message.event === \"\" &&\n message.id === \"\" &&\n message.retry === undefined\n );\n}\n"],"mappings":";;;;;;;;;;AAOA,MAAa,yBAAyB;;;;;;;AAuBtC,eAAsB,SAEpB,QACA,SACA;AAGA,KAAI,kBAAkB,gBAAgB;EACpC,MAAM,SAAS,OAAO,WAAW;AAMjC,SAAO,MAAM;GACX,MAAM,SAAS,MAAM,OAAO,MAAM;AAClC,OAAI,OAAO,MAAM;AACf,YAAQ,IAAI,YAAY,EAAE,KAAK;AAC/B;;AAEF,WAAQ,OAAO,MAAM;;OAGvB,KAAI;AAEF,aAAW,MAAM,SAAS,OACxB,SAAQ,IAAI,WAAW,MAAM,CAAC;AAEhC,UAAQ,IAAI,YAAY,EAAE,KAAK;UAExB,GAAQ;AACf,QAAM,IAAI,MACR;GACE;GACA;GACA,UAAU,EAAE;GACb,CAAC,KAAK,KAAK,CACb;;;AAKP,IAAW,eAAX,yBAAA,cAAA;AACE,cAAA,aAAA,aAAA,MAAA;AACA,cAAA,aAAA,oBAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;;EAJS,gBAAA,EAAA,CAKV;;;;;;;AAQD,SAAgB,SACd,QACA;CACA,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI,yBAAyB;AAG7B,QAAO,SAAS,QAAQ,KAAiB,OAAiB;AACxD,MAAI,OAAO;AACT,UAAO,KAAK,GAAG,KAAK;AACpB;;AAGF,MAAI,WAAW,KAAA,GAAW;AACxB,YAAS;AACT,cAAW;AACX,iBAAc;QAGd,UAAS,OAAO,QAAQ,IAAI;EAG9B,MAAM,YAAY,OAAO;EACzB,IAAI,YAAY;AAChB,SAAO,WAAW,WAAW;AAC3B,OAAI,wBAAwB;AAC1B,QAAI,OAAO,cAAc,aAAa,QACpC,aAAY,EAAE;AAGhB,6BAAyB;;GAI3B,IAAI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OV,SAAS,OAAO,GAAe,GAAe;CAC5C,MAAM,MAAM,IAAI,WAAW,EAAE,SAAS,EAAE,OAAO;AAC/C,KAAI,IAAI,EAAE;AACV,KAAI,IAAI,GAAG,EAAE,OAAO;AACpB,QAAO;;AAGT,SAAS,aAAiC;AAKxC,QAAO;EACL,MAAM;EACN,OAAO;EACP,IAAI;EACJ,OAAO,KAAA;EACR;;AAGH,SAAgB,+CACd,QACA,iBACA;CACA,MAAM,aAAa,IAAI,eAAe,EACpC,MAAM,MAAM,YAAY;EACtB,MAAM,cAAc,aAAa,QAAQ;AACvC,OAAI,IAAI,UAAU,QAChB,OAAM,IAAI,MAAM,IAAI,QAAQ,qCAAqC;YACxD,IAAI,UAAU,WACvB,mBAAkB,IAAI;YAElB,IAAI,KAAM,YAAW,QAAQ,IAAI,KAAK;IAE5C;EACF,MAAM,UACJ,MACA,aACA,UACG;AACH,eAAY,MAAM,aAAa,MAAM;AACrC,OAAI,MAAO,YAAW,OAAO;;AAE/B,QAAM,SAAS,QAAQ,SAAS,OAAO,CAAC;IAE3C,CAAC;AACF,QAAO,uBAAuB,mBAAmB,WAAW;;AAG9D,SAAS,QAAQ,SAAsC;AACrD,QACE,QAAQ,SAAS,MACjB,QAAQ,UAAU,MAClB,QAAQ,OAAO,MACf,QAAQ,UAAU,KAAA"}
|
|
1
|
+
{"version":3,"file":"event_source_parse.js","names":[],"sources":["../../src/utils/event_source_parse.ts"],"sourcesContent":["/* oxlint-disable prefer-template */\n/* oxlint-disable default-case */\n// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts\n// due to a packaging issue in the original.\n// MIT License\nimport { IterableReadableStream } from \"./stream.js\";\n\nexport const EventStreamContentType = \"text/event-stream\";\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventSourceMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * Converts a ReadableStream into a callback pattern.\n * @param stream The input ReadableStream.\n * @param onChunk A function that will be called on each new byte chunk in the stream.\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\n */\nexport async function getBytes(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n stream: ReadableStream<Uint8Array> | AsyncIterable<any>,\n onChunk: (arr: Uint8Array, flush?: boolean) => void\n) {\n // TODO: Use Async iteration for both cases?\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (stream instanceof ReadableStream) {\n const reader = stream.getReader();\n // CHANGED: Introduced a \"flush\" mechanism to process potential pending messages when the stream ends.\n // This change is essential to ensure that we capture every last piece of information from streams,\n // such as those from Azure OpenAI, which may not terminate with a blank line. Without this\n // mechanism, we risk ignoring a possibly significant last message.\n // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.\n while (true) {\n const result = await reader.read();\n if (result.done) {\n onChunk(new Uint8Array(), true);\n break;\n }\n onChunk(result.value);\n }\n } else {\n try {\n // Handle Node.js Readable streams with async iteration\n for await (const chunk of stream) {\n onChunk(new Uint8Array(chunk));\n }\n onChunk(new Uint8Array(), true);\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n throw new Error(\n [\n \"Parsing event source stream failed.\",\n \"Ensure your implementation of fetch returns a web or Node readable stream.\",\n `Error: ${e.message}`,\n ].join(\"\\n\")\n );\n }\n }\n}\n\nconst enum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Parses arbitary byte chunks into EventSource line buffers.\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n.\n * @param onLine A function that will be called on each new EventSource line.\n * @returns A function that should be called for each incoming byte chunk.\n */\nexport function getLines(\n onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void\n) {\n let buffer: Uint8Array | undefined;\n let position: number; // current read position\n let fieldLength: number; // length of the `field` portion of the line\n let discardTrailingNewline = false;\n\n // return a function that can process each incoming byte chunk:\n return function onChunk(arr: Uint8Array, flush?: boolean) {\n if (flush) {\n onLine(arr, 0, true);\n return;\n }\n\n if (buffer === undefined) {\n buffer = arr;\n position = 0;\n fieldLength = -1;\n } else {\n // we're still parsing the old line. Append the new bytes into buffer:\n buffer = concat(buffer, arr);\n }\n\n const bufLength = buffer.length;\n let lineStart = 0; // index where the current line starts\n while (position < bufLength) {\n if (discardTrailingNewline) {\n if (buffer[position] === ControlChars.NewLine) {\n lineStart = ++position; // skip to next char\n }\n\n discardTrailingNewline = false;\n }\n\n // start looking forward till the end of line:\n let lineEnd = -1; // index of the \\r or \\n char\n for (; position < bufLength && lineEnd === -1; ++position) {\n switch (buffer[position]) {\n case ControlChars.Colon:\n if (fieldLength === -1) {\n // first colon in line\n fieldLength = position - lineStart;\n }\n break;\n // @ts-expect-error \\r case below should fallthrough to \\n:\n case ControlChars.CarriageReturn:\n discardTrailingNewline = true;\n // oxlint-disable-next-line no-fallthrough\n case ControlChars.NewLine:\n lineEnd = position;\n break;\n }\n }\n\n if (lineEnd === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next arr and then continue parsing:\n break;\n }\n\n // we've reached the line end, send it out:\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\n lineStart = position; // we're now on the next line\n fieldLength = -1;\n }\n\n if (lineStart === bufLength) {\n buffer = undefined; // we've finished reading it\n } else if (lineStart !== 0) {\n // Create a new view into buffer beginning at lineStart so we don't\n // need to copy over the previous lines when we get the new arr:\n buffer = buffer.subarray(lineStart);\n position -= lineStart;\n }\n };\n}\n\n/**\n * Parses line buffers into EventSourceMessages.\n * @param onId A function that will be called on each `id` field.\n * @param onRetry A function that will be called on each `retry` field.\n * @param onMessage A function that will be called on each message.\n * @returns A function that should be called for each incoming line buffer.\n */\nexport function getMessages(\n onMessage?: (msg: EventSourceMessage) => void,\n onId?: (id: string) => void,\n onRetry?: (retry: number) => void\n) {\n let message = newMessage();\n const decoder = new TextDecoder();\n\n // return a function that can process each incoming line buffer:\n return function onLine(\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) {\n if (flush) {\n if (!isEmpty(message)) {\n onMessage?.(message);\n message = newMessage();\n }\n return;\n }\n\n if (line.length === 0) {\n // empty line denotes end of message. Trigger the callback and start a new message:\n onMessage?.(message);\n message = newMessage();\n } else if (fieldLength > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLength));\n const valueOffset =\n fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n // if this message already has data, append the new value to the old.\n // otherwise, just set to the new value:\n message.data = message.data ? message.data + \"\\n\" + value : value; // otherwise,\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n onId?.((message.id = value));\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!Number.isNaN(retry)) {\n // per spec, ignore non-integers\n onRetry?.((message.retry = retry));\n }\n break;\n }\n }\n }\n };\n}\n\nfunction concat(a: Uint8Array, b: Uint8Array) {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction newMessage(): EventSourceMessage {\n // data, event, and id must be initialized to empty strings:\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n // retry should be initialized to undefined so we return a consistent shape\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\n return {\n data: \"\",\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nexport function convertEventStreamToIterableReadableDataStream(\n stream: ReadableStream,\n onMetadataEvent?: (e: unknown) => unknown\n) {\n const dataStream = new ReadableStream({\n async start(controller) {\n const enqueueLine = getMessages((msg) => {\n if (msg.event === \"error\") {\n throw new Error(msg.data ?? \"Unspecified event streaming error.\");\n } else if (msg.event === \"metadata\") {\n onMetadataEvent?.(msg);\n } else {\n if (msg.data) controller.enqueue(msg.data);\n }\n });\n const onLine = (\n line: Uint8Array,\n fieldLength: number,\n flush?: boolean\n ) => {\n enqueueLine(line, fieldLength, flush);\n if (flush) controller.close();\n };\n await getBytes(stream, getLines(onLine));\n },\n });\n return IterableReadableStream.fromReadableStream(dataStream);\n}\n\nfunction isEmpty(message: EventSourceMessage): boolean {\n return (\n message.data === \"\" &&\n message.event === \"\" &&\n message.id === \"\" &&\n message.retry === undefined\n );\n}\n"],"mappings":";;;;;;;;;;AAOA,MAAa,yBAAyB;;;;;;;AAuBtC,eAAsB,SAEpB,QACA,SACA;AAGA,KAAI,kBAAkB,gBAAgB;EACpC,MAAM,SAAS,OAAO,WAAW;AAMjC,SAAO,MAAM;GACX,MAAM,SAAS,MAAM,OAAO,MAAM;AAClC,OAAI,OAAO,MAAM;AACf,YAAQ,IAAI,YAAY,EAAE,KAAK;AAC/B;;AAEF,WAAQ,OAAO,MAAM;;OAGvB,KAAI;AAEF,aAAW,MAAM,SAAS,OACxB,SAAQ,IAAI,WAAW,MAAM,CAAC;AAEhC,UAAQ,IAAI,YAAY,EAAE,KAAK;UAExB,GAAQ;AACf,QAAM,IAAI,MACR;GACE;GACA;GACA,UAAU,EAAE;GACb,CAAC,KAAK,KAAK,CACb;;;AAKP,IAAW,eAAX,yBAAA,cAAA;AACE,cAAA,aAAA,aAAA,MAAA;AACA,cAAA,aAAA,oBAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;AACA,cAAA,aAAA,WAAA,MAAA;;EAJS,gBAAA,EAAA,CAKV;;;;;;;AAQD,SAAgB,SACd,QACA;CACA,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI,yBAAyB;AAG7B,QAAO,SAAS,QAAQ,KAAiB,OAAiB;AACxD,MAAI,OAAO;AACT,UAAO,KAAK,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AQ,OAAO,QAAQ,OAAO,QAAQ,OAAO,OAAO,QAAQ;AAC5D;IACF,KAAK;AACH,aAAQ,QAAQ;AAChB;IACF,KAAK;AACH,YAAQ,QAAQ,KAAK,MAAO;AAC5B;IACF,KAAK,SAAS;KACZ,MAAM,QAAQ,SAAS,OAAO,GAAG;AACjC,SAAI,CAAC,OAAO,MAAM,MAAM,CAEtB,WAAW,QAAQ,QAAQ,MAAO;AAEpC;;;;;;AAOV,SAAS,OAAO,GAAe,GAAe;CAC5C,MAAM,MAAM,IAAI,WAAW,EAAE,SAAS,EAAE,OAAO;AAC/C,KAAI,IAAI,EAAE;AACV,KAAI,IAAI,GAAG,EAAE,OAAO;AACpB,QAAO;;AAGT,SAAS,aAAiC;AAKxC,QAAO;EACL,MAAM;EACN,OAAO;EACP,IAAI;EACJ,OAAO,KAAA;EACR;;AAGH,SAAgB,+CACd,QACA,iBACA;CACA,MAAM,aAAa,IAAI,eAAe,EACpC,MAAM,MAAM,YAAY;EACtB,MAAM,cAAc,aAAa,QAAQ;AACvC,OAAI,IAAI,UAAU,QAChB,OAAM,IAAI,MAAM,IAAI,QAAQ,qCAAqC;YACxD,IAAI,UAAU,WACvB,mBAAkB,IAAI;YAElB,IAAI,KAAM,YAAW,QAAQ,IAAI,KAAK;IAE5C;EACF,MAAM,UACJ,MACA,aACA,UACG;AACH,eAAY,MAAM,aAAa,MAAM;AACrC,OAAI,MAAO,YAAW,OAAO;;AAE/B,QAAM,SAAS,QAAQ,SAAS,OAAO,CAAC;IAE3C,CAAC;AACF,QAAO,uBAAuB,mBAAmB,WAAW;;AAG9D,SAAS,QAAQ,SAAsC;AACrD,QACE,QAAQ,SAAS,MACjB,QAAQ,UAAU,MAClB,QAAQ,OAAO,MACf,QAAQ,UAAU,KAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"function_calling.cjs","names":["toJsonSchema","isLangChainTool"],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n StructuredToolInterface,\n StructuredToolParams,\n isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n isStructuredTool,\n isStructuredToolParams,\n isRunnableToolLike,\n isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): FunctionDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n return {\n name: tool.name,\n description: tool.description,\n parameters: toJsonSchema(tool.schema),\n // Do not include the `strict` field if it is `undefined`.\n ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n //
|
|
1
|
+
{"version":3,"file":"function_calling.cjs","names":["toJsonSchema","isLangChainTool"],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n StructuredToolInterface,\n StructuredToolParams,\n isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n isStructuredTool,\n isStructuredToolParams,\n isRunnableToolLike,\n isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): FunctionDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n return {\n name: tool.name,\n description: tool.description,\n parameters: toJsonSchema(tool.schema),\n // Do not include the `strict` field if it is `undefined`.\n ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n tool: StructuredToolInterface | Record<string, any> | RunnableToolLike,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): ToolDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n let toolDef: ToolDefinition | undefined;\n if (isLangChainTool(tool)) {\n toolDef = {\n type: \"function\",\n function: convertToOpenAIFunction(tool),\n };\n } else {\n toolDef = tool as ToolDefinition;\n }\n\n if (fieldsCopy?.strict !== undefined) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n (toolDef.function as any).strict = fieldsCopy.strict;\n }\n\n return toolDef;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACd,MACA,QASoB;CAEpB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;AAE5D,QAAO;EACL,MAAM,KAAK;EACX,aAAa,KAAK;EAClB,YAAYA,0BAAAA,aAAa,KAAK,OAAO;EAErC,GAAI,YAAY,WAAW,KAAA,IAAY,EAAE,QAAQ,WAAW,QAAQ,GAAG,EAAE;EAC1E;;;;;;;;;;;AAYH,SAAgB,oBAEd,MACA,QASgB;CAEhB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;CAE5D,IAAI;AACJ,KAAIC,cAAAA,gBAAgB,KAAK,CACvB,WAAU;EACR,MAAM;EACN,UAAU,wBAAwB,KAAK;EACxC;KAED,WAAU;AAGZ,KAAI,YAAY,WAAW,KAAA,EAExB,SAAQ,SAAiB,SAAS,WAAW;AAGhD,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"function_calling.js","names":[],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n StructuredToolInterface,\n StructuredToolParams,\n isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n isStructuredTool,\n isStructuredToolParams,\n isRunnableToolLike,\n isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): FunctionDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n return {\n name: tool.name,\n description: tool.description,\n parameters: toJsonSchema(tool.schema),\n // Do not include the `strict` field if it is `undefined`.\n ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n //
|
|
1
|
+
{"version":3,"file":"function_calling.js","names":[],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n StructuredToolInterface,\n StructuredToolParams,\n isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n isStructuredTool,\n isStructuredToolParams,\n isRunnableToolLike,\n isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): FunctionDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n return {\n name: tool.name,\n description: tool.description,\n parameters: toJsonSchema(tool.schema),\n // Do not include the `strict` field if it is `undefined`.\n ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n tool: StructuredToolInterface | Record<string, any> | RunnableToolLike,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): ToolDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n let toolDef: ToolDefinition | undefined;\n if (isLangChainTool(tool)) {\n toolDef = {\n type: \"function\",\n function: convertToOpenAIFunction(tool),\n };\n } else {\n toolDef = tool as ToolDefinition;\n }\n\n if (fieldsCopy?.strict !== undefined) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n (toolDef.function as any).strict = fieldsCopy.strict;\n }\n\n return toolDef;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACd,MACA,QASoB;CAEpB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;AAE5D,QAAO;EACL,MAAM,KAAK;EACX,aAAa,KAAK;EAClB,YAAY,aAAa,KAAK,OAAO;EAErC,GAAI,YAAY,WAAW,KAAA,IAAY,EAAE,QAAQ,WAAW,QAAQ,GAAG,EAAE;EAC1E;;;;;;;;;;;AAYH,SAAgB,oBAEd,MACA,QASgB;CAEhB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;CAE5D,IAAI;AACJ,KAAI,gBAAgB,KAAK,CACvB,WAAU;EACR,MAAM;EACN,UAAU,wBAAwB,KAAK;EACxC;KAED,WAAU;AAGZ,KAAI,YAAY,WAAW,KAAA,EAExB,SAAQ,SAAiB,SAAS,WAAW;AAGhD,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../../../src/utils/is-network-error/index.js"],"sourcesContent":["/*
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../../../src/utils/is-network-error/index.js"],"sourcesContent":["/* oxlint-disable */\nconst objectToString = Object.prototype.toString;\n\nconst isError = (value) => objectToString.call(value) === \"[object Error]\";\n\nconst errorMessages = new Set([\n \"network error\", // Chrome\n \"Failed to fetch\", // Chrome\n \"NetworkError when attempting to fetch resource.\", // Firefox\n \"The Internet connection appears to be offline.\", // Safari 16\n \"Network request failed\", // `cross-fetch`\n \"fetch failed\", // Undici (Node.js)\n \"terminated\", // Undici (Node.js)\n \" A network error occurred.\", // Bun (WebKit)\n \"Network connection lost\", // Cloudflare Workers (fetch)\n]);\n\nexport default function isNetworkError(error) {\n const isValid =\n error &&\n isError(error) &&\n error.name === \"TypeError\" &&\n typeof error.message === \"string\";\n\n if (!isValid) {\n return false;\n }\n\n const { message, stack } = error;\n\n // Safari 17+ has generic message but no stack for network errors\n if (message === \"Load failed\") {\n return (\n stack === undefined ||\n // Sentry adds its own stack trace to the fetch error, so also check for that\n \"__sentry_captured__\" in error\n );\n }\n\n // Deno network errors start with specific text\n if (message.startsWith(\"error sending request for url\")) {\n return true;\n }\n\n // Standard network error messages\n return errorMessages.has(message);\n}\n"],"mappings":";AACA,MAAM,iBAAiB,OAAO,UAAU;AAExC,MAAM,WAAW,UAAU,eAAe,KAAK,MAAM,KAAK;AAE1D,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAwB,eAAe,OAAO;AAO5C,KAAI,EALF,SACA,QAAQ,MAAM,IACd,MAAM,SAAS,eACf,OAAO,MAAM,YAAY,UAGzB,QAAO;CAGT,MAAM,EAAE,SAAS,UAAU;AAG3B,KAAI,YAAY,cACd,QACE,UAAU,KAAA,KAEV,yBAAyB;AAK7B,KAAI,QAAQ,WAAW,gCAAgC,CACrD,QAAO;AAIT,QAAO,cAAc,IAAI,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/is-network-error/index.js"],"sourcesContent":["/*
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/is-network-error/index.js"],"sourcesContent":["/* oxlint-disable */\nconst objectToString = Object.prototype.toString;\n\nconst isError = (value) => objectToString.call(value) === \"[object Error]\";\n\nconst errorMessages = new Set([\n \"network error\", // Chrome\n \"Failed to fetch\", // Chrome\n \"NetworkError when attempting to fetch resource.\", // Firefox\n \"The Internet connection appears to be offline.\", // Safari 16\n \"Network request failed\", // `cross-fetch`\n \"fetch failed\", // Undici (Node.js)\n \"terminated\", // Undici (Node.js)\n \" A network error occurred.\", // Bun (WebKit)\n \"Network connection lost\", // Cloudflare Workers (fetch)\n]);\n\nexport default function isNetworkError(error) {\n const isValid =\n error &&\n isError(error) &&\n error.name === \"TypeError\" &&\n typeof error.message === \"string\";\n\n if (!isValid) {\n return false;\n }\n\n const { message, stack } = error;\n\n // Safari 17+ has generic message but no stack for network errors\n if (message === \"Load failed\") {\n return (\n stack === undefined ||\n // Sentry adds its own stack trace to the fetch error, so also check for that\n \"__sentry_captured__\" in error\n );\n }\n\n // Deno network errors start with specific text\n if (message.startsWith(\"error sending request for url\")) {\n return true;\n }\n\n // Standard network error messages\n return errorMessages.has(message);\n}\n"],"mappings":";AACA,MAAM,iBAAiB,OAAO,UAAU;AAExC,MAAM,WAAW,UAAU,eAAe,KAAK,MAAM,KAAK;AAE1D,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAwB,eAAe,OAAO;AAO5C,KAAI,EALF,SACA,QAAQ,MAAM,IACd,MAAM,SAAS,eACf,OAAO,MAAM,YAAY,UAGzB,QAAO;CAGT,MAAM,EAAE,SAAS,UAAU;AAG3B,KAAI,YAAY,cACd,QACE,UAAU,KAAA,KAEV,yBAAyB;AAK7B,KAAI,QAAQ,WAAW,gCAAgC,CACrD,QAAO;AAIT,QAAO,cAAc,IAAI,QAAQ"}
|
package/dist/utils/json.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.cjs","names":[],"sources":["../../src/utils/json.ts"],"sourcesContent":["export function parseJsonMarkdown(s: string, parser = parsePartialJson) {\n // eslint-disable-next-line no-param-reassign\n s = s.trim();\n\n const firstFenceIndex = s.indexOf(\"```\");\n if (firstFenceIndex === -1) {\n return parser(s);\n }\n\n let contentAfterFence = s.substring(firstFenceIndex + 3);\n\n if (contentAfterFence.startsWith(\"json\\n\")) {\n contentAfterFence = contentAfterFence.substring(5);\n } else if (contentAfterFence.startsWith(\"json\")) {\n contentAfterFence = contentAfterFence.substring(4);\n } else if (contentAfterFence.startsWith(\"\\n\")) {\n contentAfterFence = contentAfterFence.substring(1);\n }\n\n const closingFenceIndex = contentAfterFence.indexOf(\"```\");\n let finalContent = contentAfterFence;\n if (closingFenceIndex !== -1) {\n finalContent = contentAfterFence.substring(0, closingFenceIndex);\n }\n\n return parser(finalContent.trim());\n}\n\n/**\n * Recursive descent partial JSON parser.\n * @param s - The string to parse.\n * @returns The parsed value.\n * @throws Error if the input is a malformed JSON string.\n */\nexport function strictParsePartialJson(s: string): unknown {\n try {\n return JSON.parse(s);\n } catch {\n // Continue to partial parsing\n }\n\n const buffer = s.trim();\n if (buffer.length === 0) throw new Error(\"Unexpected end of JSON input\");\n\n let pos = 0;\n\n function skipWhitespace(): void {\n while (pos < buffer.length && /\\s/.test(buffer[pos])) {\n pos += 1;\n }\n }\n\n function parseString(): string {\n if (buffer[pos] !== '\"') {\n throw new Error(`Expected '\"' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n pos += 1;\n let result = \"\";\n let escaped = false;\n\n while (pos < buffer.length) {\n const char = buffer[pos];\n\n if (escaped) {\n if (char === \"n\") {\n result += \"\\n\";\n } else if (char === \"t\") {\n result += \"\\t\";\n } else if (char === \"r\") {\n result += \"\\r\";\n } else if (char === \"\\\\\") {\n result += \"\\\\\";\n } else if (char === '\"') {\n result += '\"';\n } else if (char === \"b\") {\n result += \"\\b\";\n } else if (char === \"f\") {\n result += \"\\f\";\n } else if (char === \"/\") {\n result += \"/\";\n } else if (char === \"u\") {\n const hex = buffer.substring(pos + 1, pos + 5);\n if (/^[0-9A-Fa-f]{0,4}$/.test(hex)) {\n if (hex.length === 4) {\n result += String.fromCharCode(Number.parseInt(hex, 16));\n } else {\n result += `u${hex}`;\n }\n\n pos += hex.length;\n } else {\n throw new Error(\n `Invalid unicode escape sequence '\\\\u${hex}' at position ${pos}`\n );\n }\n } else {\n throw new Error(\n `Invalid escape sequence '\\\\${char}' at position ${pos}`\n );\n }\n escaped = false;\n } else if (char === \"\\\\\") {\n escaped = true;\n } else if (char === '\"') {\n pos += 1;\n return result;\n } else {\n result += char;\n }\n\n pos += 1;\n }\n\n if (escaped) result += \"\\\\\";\n return result;\n }\n\n function parseNumber(): number {\n const start = pos;\n let numStr = \"\";\n\n if (buffer[pos] === \"-\") {\n numStr += \"-\";\n pos += 1;\n }\n\n if (pos < buffer.length && buffer[pos] === \"0\") {\n numStr += \"0\";\n pos += 1;\n\n if (buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n throw new Error(`Invalid number at position ${start}`);\n }\n }\n\n if (pos < buffer.length && buffer[pos] >= \"1\" && buffer[pos] <= \"9\") {\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (pos < buffer.length && buffer[pos] === \".\") {\n numStr += \".\";\n pos += 1;\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (pos < buffer.length && (buffer[pos] === \"e\" || buffer[pos] === \"E\")) {\n numStr += buffer[pos];\n pos += 1;\n if (pos < buffer.length && (buffer[pos] === \"+\" || buffer[pos] === \"-\")) {\n numStr += buffer[pos];\n pos += 1;\n }\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (numStr === \"-\") return -0;\n\n const num = Number.parseFloat(numStr);\n\n if (Number.isNaN(num)) {\n pos = start;\n throw new Error(`Invalid number '${numStr}' at position ${start}`);\n }\n\n return num;\n }\n\n function parseValue(): unknown {\n skipWhitespace();\n\n if (pos >= buffer.length) {\n throw new Error(`Unexpected end of input at position ${pos}`);\n }\n\n const char = buffer[pos];\n\n if (char === \"{\") return parseObject();\n if (char === \"[\") return parseArray();\n if (char === '\"') return parseString();\n\n if (\"null\".startsWith(buffer.substring(pos, pos + 4))) {\n pos += Math.min(4, buffer.length - pos);\n return null;\n }\n\n if (\"true\".startsWith(buffer.substring(pos, pos + 4))) {\n pos += Math.min(4, buffer.length - pos);\n return true;\n }\n\n if (\"false\".startsWith(buffer.substring(pos, pos + 5))) {\n pos += Math.min(5, buffer.length - pos);\n return false;\n }\n\n if (char === \"-\" || (char >= \"0\" && char <= \"9\")) {\n return parseNumber();\n }\n\n throw new Error(`Unexpected character '${char}' at position ${pos}`);\n }\n\n function parseArray(): unknown[] {\n if (buffer[pos] !== \"[\") {\n throw new Error(`Expected '[' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n const arr: unknown[] = [];\n\n pos += 1;\n skipWhitespace();\n\n if (pos >= buffer.length) return arr;\n if (buffer[pos] === \"]\") {\n pos += 1;\n return arr;\n }\n\n while (pos < buffer.length) {\n skipWhitespace();\n if (pos >= buffer.length) return arr;\n\n arr.push(parseValue());\n\n skipWhitespace();\n if (pos >= buffer.length) return arr;\n\n if (buffer[pos] === \"]\") {\n pos += 1;\n return arr;\n } else if (buffer[pos] === \",\") {\n pos += 1;\n continue;\n }\n\n throw new Error(\n `Expected ',' or ']' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n\n return arr;\n }\n\n function parseObject(): Record<string, unknown> {\n if (buffer[pos] !== \"{\") {\n throw new Error(`Expected '{' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n const obj: Record<string, unknown> = {};\n pos += 1;\n skipWhitespace();\n\n if (pos >= buffer.length) return obj;\n if (buffer[pos] === \"}\") {\n pos += 1;\n return obj;\n }\n\n while (pos < buffer.length) {\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n const key = parseString();\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n if (buffer[pos] !== \":\") {\n throw new Error(\n `Expected ':' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n pos += 1;\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n obj[key] = parseValue();\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n if (buffer[pos] === \"}\") {\n pos += 1;\n return obj;\n } else if (buffer[pos] === \",\") {\n pos += 1;\n continue;\n }\n\n throw new Error(\n `Expected ',' or '}' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n\n return obj;\n }\n\n const value = parseValue();\n skipWhitespace();\n\n if (pos < buffer.length) {\n throw new Error(`Unexpected character '${buffer[pos]}' at position ${pos}`);\n }\n\n return value;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function parsePartialJson(s: string): any | null {\n // Attempt to parse the modified string as JSON.\n try {\n if (typeof s === \"undefined\") return null;\n return strictParsePartialJson(s);\n } catch {\n // If we still can't parse the string as JSON, return null to indicate failure.\n return null;\n }\n}\n"],"mappings":";AAAA,SAAgB,kBAAkB,GAAW,SAAS,kBAAkB;AAEtE,KAAI,EAAE,MAAM;CAEZ,MAAM,kBAAkB,EAAE,QAAQ,MAAM;AACxC,KAAI,oBAAoB,GACtB,QAAO,OAAO,EAAE;CAGlB,IAAI,oBAAoB,EAAE,UAAU,kBAAkB,EAAE;AAExD,KAAI,kBAAkB,WAAW,SAAS,CACxC,qBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,OAAO,CAC7C,qBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,KAAK,CAC3C,qBAAoB,kBAAkB,UAAU,EAAE;CAGpD,MAAM,oBAAoB,kBAAkB,QAAQ,MAAM;CAC1D,IAAI,eAAe;AACnB,KAAI,sBAAsB,GACxB,gBAAe,kBAAkB,UAAU,GAAG,kBAAkB;AAGlE,QAAO,OAAO,aAAa,MAAM,CAAC;;;;;;;;AASpC,SAAgB,uBAAuB,GAAoB;AACzD,KAAI;AACF,SAAO,KAAK,MAAM,EAAE;SACd;CAIR,MAAM,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AAO,SAAS,KAAK;AACvB,UAAO;AACP,UAAO;;AAGT,SAAO,MAAM,OAAO,QAAQ;AAC1B,mBAAgB;AAChB,OAAI,OAAO,OAAO,OAAQ,QAAO;GAEjC,MAAM,MAAM,aAAa;AAEzB,mBAAgB;AAChB,OAAI,OAAO,OAAO,OAAQ,QAAO;AAEjC,OAAI,OAAO,SAAS,IAClB,OAAM,IAAI,MACR,4BAA4B,IAAI,SAAS,OAAO,KAAK,GACtD;AAEH,UAAO;AAEP,mBAAgB;AAChB,OAAI,OAAO,OAAO,OAAQ,QAAO;AAEjC,OAAI,OAAO,YAAY;AAEvB,mBAAgB;AAChB,OAAI,OAAO,OAAO,OAAQ,QAAO;AAEjC,OAAI,OAAO,SAAS,KAAK;AACvB,WAAO;AACP,WAAO;cACE,OAAO,SAAS,KAAK;AAC9B,WAAO;AACP;;AAGF,SAAM,IAAI,MACR,mCAAmC,IAAI,SAAS,OAAO,KAAK,GAC7D;;AAGH,SAAO;;CAGT,MAAM,QAAQ,YAAY;AAC1B,iBAAgB;AAEhB,KAAI,MAAM,OAAO,OACf,OAAM,IAAI,MAAM,yBAAyB,OAAO,KAAK,gBAAgB,MAAM;AAG7E,QAAO;;AAIT,SAAgB,iBAAiB,GAAuB;AAEtD,KAAI;AACF,MAAI,OAAO,MAAM,YAAa,QAAO;AACrC,SAAO,uBAAuB,EAAE;SAC1B;AAEN,SAAO"}
|
|
1
|
+
{"version":3,"file":"json.cjs","names":[],"sources":["../../src/utils/json.ts"],"sourcesContent":["export function parseJsonMarkdown(s: string, parser = parsePartialJson) {\n // oxlint-disable-next-line no-param-reassign\n s = s.trim();\n\n const firstFenceIndex = s.indexOf(\"```\");\n if (firstFenceIndex === -1) {\n return parser(s);\n }\n\n let contentAfterFence = s.substring(firstFenceIndex + 3);\n\n if (contentAfterFence.startsWith(\"json\\n\")) {\n contentAfterFence = contentAfterFence.substring(5);\n } else if (contentAfterFence.startsWith(\"json\")) {\n contentAfterFence = contentAfterFence.substring(4);\n } else if (contentAfterFence.startsWith(\"\\n\")) {\n contentAfterFence = contentAfterFence.substring(1);\n }\n\n const closingFenceIndex = contentAfterFence.indexOf(\"```\");\n let finalContent = contentAfterFence;\n if (closingFenceIndex !== -1) {\n finalContent = contentAfterFence.substring(0, closingFenceIndex);\n }\n\n return parser(finalContent.trim());\n}\n\n/**\n * Recursive descent partial JSON parser.\n * @param s - The string to parse.\n * @returns The parsed value.\n * @throws Error if the input is a malformed JSON string.\n */\nexport function strictParsePartialJson(s: string): unknown {\n try {\n return JSON.parse(s);\n } catch {\n // Continue to partial parsing\n }\n\n const buffer = s.trim();\n if (buffer.length === 0) throw new Error(\"Unexpected end of JSON input\");\n\n let pos = 0;\n\n function skipWhitespace(): void {\n while (pos < buffer.length && /\\s/.test(buffer[pos])) {\n pos += 1;\n }\n }\n\n function parseString(): string {\n if (buffer[pos] !== '\"') {\n throw new Error(`Expected '\"' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n pos += 1;\n let result = \"\";\n let escaped = false;\n\n while (pos < buffer.length) {\n const char = buffer[pos];\n\n if (escaped) {\n if (char === \"n\") {\n result += \"\\n\";\n } else if (char === \"t\") {\n result += \"\\t\";\n } else if (char === \"r\") {\n result += \"\\r\";\n } else if (char === \"\\\\\") {\n result += \"\\\\\";\n } else if (char === '\"') {\n result += '\"';\n } else if (char === \"b\") {\n result += \"\\b\";\n } else if (char === \"f\") {\n result += \"\\f\";\n } else if (char === \"/\") {\n result += \"/\";\n } else if (char === \"u\") {\n const hex = buffer.substring(pos + 1, pos + 5);\n if (/^[0-9A-Fa-f]{0,4}$/.test(hex)) {\n if (hex.length === 4) {\n result += String.fromCharCode(Number.parseInt(hex, 16));\n } else {\n result += `u${hex}`;\n }\n\n pos += hex.length;\n } else {\n throw new Error(\n `Invalid unicode escape sequence '\\\\u${hex}' at position ${pos}`\n );\n }\n } else {\n throw new Error(\n `Invalid escape sequence '\\\\${char}' at position ${pos}`\n );\n }\n escaped = false;\n } else if (char === \"\\\\\") {\n escaped = true;\n } else if (char === '\"') {\n pos += 1;\n return result;\n } else {\n result += char;\n }\n\n pos += 1;\n }\n\n if (escaped) result += \"\\\\\";\n return result;\n }\n\n function parseNumber(): number {\n const start = pos;\n let numStr = \"\";\n\n if (buffer[pos] === \"-\") {\n numStr += \"-\";\n pos += 1;\n }\n\n if (pos < buffer.length && buffer[pos] === \"0\") {\n numStr += \"0\";\n pos += 1;\n\n if (buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n throw new Error(`Invalid number at position ${start}`);\n }\n }\n\n if (pos < buffer.length && buffer[pos] >= \"1\" && buffer[pos] <= \"9\") {\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (pos < buffer.length && buffer[pos] === \".\") {\n numStr += \".\";\n pos += 1;\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (pos < buffer.length && (buffer[pos] === \"e\" || buffer[pos] === \"E\")) {\n numStr += buffer[pos];\n pos += 1;\n if (pos < buffer.length && (buffer[pos] === \"+\" || buffer[pos] === \"-\")) {\n numStr += buffer[pos];\n pos += 1;\n }\n while (pos < buffer.length && buffer[pos] >= \"0\" && buffer[pos] <= \"9\") {\n numStr += buffer[pos];\n pos += 1;\n }\n }\n\n if (numStr === \"-\") return -0;\n\n const num = Number.parseFloat(numStr);\n\n if (Number.isNaN(num)) {\n pos = start;\n throw new Error(`Invalid number '${numStr}' at position ${start}`);\n }\n\n return num;\n }\n\n function parseValue(): unknown {\n skipWhitespace();\n\n if (pos >= buffer.length) {\n throw new Error(`Unexpected end of input at position ${pos}`);\n }\n\n const char = buffer[pos];\n\n if (char === \"{\") return parseObject();\n if (char === \"[\") return parseArray();\n if (char === '\"') return parseString();\n\n if (\"null\".startsWith(buffer.substring(pos, pos + 4))) {\n pos += Math.min(4, buffer.length - pos);\n return null;\n }\n\n if (\"true\".startsWith(buffer.substring(pos, pos + 4))) {\n pos += Math.min(4, buffer.length - pos);\n return true;\n }\n\n if (\"false\".startsWith(buffer.substring(pos, pos + 5))) {\n pos += Math.min(5, buffer.length - pos);\n return false;\n }\n\n if (char === \"-\" || (char >= \"0\" && char <= \"9\")) {\n return parseNumber();\n }\n\n throw new Error(`Unexpected character '${char}' at position ${pos}`);\n }\n\n function parseArray(): unknown[] {\n if (buffer[pos] !== \"[\") {\n throw new Error(`Expected '[' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n const arr: unknown[] = [];\n\n pos += 1;\n skipWhitespace();\n\n if (pos >= buffer.length) return arr;\n if (buffer[pos] === \"]\") {\n pos += 1;\n return arr;\n }\n\n while (pos < buffer.length) {\n skipWhitespace();\n if (pos >= buffer.length) return arr;\n\n arr.push(parseValue());\n\n skipWhitespace();\n if (pos >= buffer.length) return arr;\n\n if (buffer[pos] === \"]\") {\n pos += 1;\n return arr;\n } else if (buffer[pos] === \",\") {\n pos += 1;\n continue;\n }\n\n throw new Error(\n `Expected ',' or ']' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n\n return arr;\n }\n\n function parseObject(): Record<string, unknown> {\n if (buffer[pos] !== \"{\") {\n throw new Error(`Expected '{' at position ${pos}, got '${buffer[pos]}'`);\n }\n\n const obj: Record<string, unknown> = {};\n pos += 1;\n skipWhitespace();\n\n if (pos >= buffer.length) return obj;\n if (buffer[pos] === \"}\") {\n pos += 1;\n return obj;\n }\n\n while (pos < buffer.length) {\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n const key = parseString();\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n if (buffer[pos] !== \":\") {\n throw new Error(\n `Expected ':' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n pos += 1;\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n obj[key] = parseValue();\n\n skipWhitespace();\n if (pos >= buffer.length) return obj;\n\n if (buffer[pos] === \"}\") {\n pos += 1;\n return obj;\n } else if (buffer[pos] === \",\") {\n pos += 1;\n continue;\n }\n\n throw new Error(\n `Expected ',' or '}' at position ${pos}, got '${buffer[pos]}'`\n );\n }\n\n return obj;\n }\n\n const value = parseValue();\n skipWhitespace();\n\n if (pos < buffer.length) {\n throw new Error(`Unexpected character '${buffer[pos]}' at position ${pos}`);\n }\n\n return value;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport function parsePartialJson(s: string): any | null {\n // Attempt to parse the modified string as JSON.\n try {\n if (typeof s === \"undefined\") return null;\n return strictParsePartialJson(s);\n } catch {\n // If we still can't parse the string as JSON, return null to indicate failure.\n return null;\n }\n}\n"],"mappings":";AAAA,SAAgB,kBAAkB,GAAW,SAAS,kBAAkB;AAEtE,KAAI,EAAE,MAAM;CAEZ,MAAM,kBAAkB,EAAE,QAAQ,MAAM;AACxC,KAAI,oBAAoB,GACtB,QAAO,OAAO,EAAE;CAGlB,IAAI,oBAAoB,EAAE,UAAU,kBAAkB,EAAE;AAExD,KAAI,kBAAkB,WAAW,SAAS,CACxC,qBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,OAAO,CAC7C,qBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,KAAK,CAC3C,qBAAoB,kBAAkB,UAAU,EAAE;CAGpD,MAAM,oBAAoB,kBAAkB,QAAQ,MAAM;CAC1D,IAAI,eAAe;AACnB,KAAI,sBAAsB,GACxB,gBAAe,kBAAkB,UAAU,GAAG,kBAAkB;AAGlE,QAAO,OAAO,aAAa,MAAM,CAAC;;;;;;;;AASpC,SAAgB,uBAAuB,GAAoB;AACzD,KAAI;AACF,SAAO,KAAK,MAAM,EAAE;SACd;CAIR,MAAM,SAAS,EAAE,MAAM;AACvB,KAAI,OAAO,WAAW,EAAG,OAAM,IAAI,MAAM,+BAA+B;CAExE,IAAI,MAAM;CAEV,SAAS,iBAAuB;AAC9B,SAAO,MAAM,OAAO,UAAU,KAAK,KAAK,OAAO,KAAK,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