@alpic80/rivet-core 1.19.1-aidon.1
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/LICENSE +7 -0
- package/README.md +176 -0
- package/dist/cjs/bundle.cjs +18915 -0
- package/dist/cjs/bundle.cjs.map +7 -0
- package/dist/esm/api/createProcessor.js +131 -0
- package/dist/esm/api/streaming.js +116 -0
- package/dist/esm/exports.js +32 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/integrations/AudioProvider.js +1 -0
- package/dist/esm/integrations/DatasetProvider.js +92 -0
- package/dist/esm/integrations/EmbeddingGenerator.js +1 -0
- package/dist/esm/integrations/GptTokenizerTokenizer.js +65 -0
- package/dist/esm/integrations/LLMProvider.js +1 -0
- package/dist/esm/integrations/Tokenizer.js +1 -0
- package/dist/esm/integrations/VectorDatabase.js +1 -0
- package/dist/esm/integrations/enableIntegrations.js +3 -0
- package/dist/esm/integrations/integrations.js +19 -0
- package/dist/esm/integrations/openai/OpenAIEmbeddingGenerator.js +23 -0
- package/dist/esm/model/DataValue.js +176 -0
- package/dist/esm/model/Dataset.js +1 -0
- package/dist/esm/model/EditorDefinition.js +1 -0
- package/dist/esm/model/GraphProcessor.js +1198 -0
- package/dist/esm/model/NodeBase.js +1 -0
- package/dist/esm/model/NodeBodySpec.js +1 -0
- package/dist/esm/model/NodeDefinition.js +12 -0
- package/dist/esm/model/NodeGraph.js +14 -0
- package/dist/esm/model/NodeImpl.js +49 -0
- package/dist/esm/model/NodeRegistration.js +144 -0
- package/dist/esm/model/Nodes.js +227 -0
- package/dist/esm/model/PluginLoadSpec.js +1 -0
- package/dist/esm/model/ProcessContext.js +1 -0
- package/dist/esm/model/Project.js +2 -0
- package/dist/esm/model/RivetPlugin.js +1 -0
- package/dist/esm/model/RivetUIContext.js +1 -0
- package/dist/esm/model/Settings.js +1 -0
- package/dist/esm/model/nodes/AbortGraphNode.js +100 -0
- package/dist/esm/model/nodes/AppendToDatasetNode.js +115 -0
- package/dist/esm/model/nodes/ArrayNode.js +144 -0
- package/dist/esm/model/nodes/AssembleMessageNode.js +199 -0
- package/dist/esm/model/nodes/AssemblePromptNode.js +129 -0
- package/dist/esm/model/nodes/AudioNode.js +101 -0
- package/dist/esm/model/nodes/BooleanNode.js +74 -0
- package/dist/esm/model/nodes/CallGraphNode.js +136 -0
- package/dist/esm/model/nodes/ChatNode.js +964 -0
- package/dist/esm/model/nodes/ChunkNode.js +166 -0
- package/dist/esm/model/nodes/CoalesceNode.js +104 -0
- package/dist/esm/model/nodes/CodeNode.js +136 -0
- package/dist/esm/model/nodes/CommentNode.js +69 -0
- package/dist/esm/model/nodes/CompareNode.js +138 -0
- package/dist/esm/model/nodes/ContextNode.js +99 -0
- package/dist/esm/model/nodes/CreateDatasetNode.js +71 -0
- package/dist/esm/model/nodes/DatasetNearestNeigborsNode.js +97 -0
- package/dist/esm/model/nodes/DelayNode.js +105 -0
- package/dist/esm/model/nodes/DelegateFunctionCallNode.js +136 -0
- package/dist/esm/model/nodes/DestructureNode.js +86 -0
- package/dist/esm/model/nodes/EvaluateNode.js +141 -0
- package/dist/esm/model/nodes/ExternalCallNode.js +162 -0
- package/dist/esm/model/nodes/ExtractJsonNode.js +122 -0
- package/dist/esm/model/nodes/ExtractMarkdownCodeBlocksNode.js +100 -0
- package/dist/esm/model/nodes/ExtractObjectPathNode.js +128 -0
- package/dist/esm/model/nodes/ExtractRegexNode.js +201 -0
- package/dist/esm/model/nodes/ExtractYamlNode.js +214 -0
- package/dist/esm/model/nodes/FilterNode.js +73 -0
- package/dist/esm/model/nodes/GetAllDatasetsNode.js +53 -0
- package/dist/esm/model/nodes/GetDatasetRowNode.js +99 -0
- package/dist/esm/model/nodes/GetEmbeddingNode.js +130 -0
- package/dist/esm/model/nodes/GetGlobalNode.js +139 -0
- package/dist/esm/model/nodes/GptFunctionNode.js +169 -0
- package/dist/esm/model/nodes/GraphInputNode.js +130 -0
- package/dist/esm/model/nodes/GraphOutputNode.js +104 -0
- package/dist/esm/model/nodes/GraphReferenceNode.js +128 -0
- package/dist/esm/model/nodes/HashNode.js +97 -0
- package/dist/esm/model/nodes/HttpCallNode.js +257 -0
- package/dist/esm/model/nodes/IfElseNode.js +138 -0
- package/dist/esm/model/nodes/IfNode.js +124 -0
- package/dist/esm/model/nodes/ImageNode.js +107 -0
- package/dist/esm/model/nodes/JoinNode.js +135 -0
- package/dist/esm/model/nodes/ListGraphsNode.js +61 -0
- package/dist/esm/model/nodes/LoadDatasetNode.js +83 -0
- package/dist/esm/model/nodes/LoopControllerNode.js +206 -0
- package/dist/esm/model/nodes/MatchNode.js +137 -0
- package/dist/esm/model/nodes/NumberNode.js +86 -0
- package/dist/esm/model/nodes/ObjectNode.js +121 -0
- package/dist/esm/model/nodes/PassthroughNode.js +78 -0
- package/dist/esm/model/nodes/PlayAudioNode.js +61 -0
- package/dist/esm/model/nodes/PopNode.js +89 -0
- package/dist/esm/model/nodes/PromptNode.js +227 -0
- package/dist/esm/model/nodes/RaceInputsNode.js +86 -0
- package/dist/esm/model/nodes/RaiseEventNode.js +84 -0
- package/dist/esm/model/nodes/RandomNumberNode.js +106 -0
- package/dist/esm/model/nodes/ReadDirectoryNode.js +165 -0
- package/dist/esm/model/nodes/ReadFileNode.js +114 -0
- package/dist/esm/model/nodes/ReplaceDatasetNode.js +118 -0
- package/dist/esm/model/nodes/SetGlobalNode.js +124 -0
- package/dist/esm/model/nodes/ShuffleNode.js +64 -0
- package/dist/esm/model/nodes/SliceNode.js +100 -0
- package/dist/esm/model/nodes/SplitNode.js +101 -0
- package/dist/esm/model/nodes/SubGraphNode.js +181 -0
- package/dist/esm/model/nodes/TextNode.js +97 -0
- package/dist/esm/model/nodes/ToJsonNode.js +78 -0
- package/dist/esm/model/nodes/ToYamlNode.js +68 -0
- package/dist/esm/model/nodes/TrimChatMessagesNode.js +120 -0
- package/dist/esm/model/nodes/URLReferenceNode.js +79 -0
- package/dist/esm/model/nodes/UserInputNode.js +111 -0
- package/dist/esm/model/nodes/VectorNearestNeighborsNode.js +127 -0
- package/dist/esm/model/nodes/VectorStoreNode.js +124 -0
- package/dist/esm/model/nodes/WaitForEventNode.js +88 -0
- package/dist/esm/native/BaseDir.js +32 -0
- package/dist/esm/native/BrowserNativeApi.js +19 -0
- package/dist/esm/native/NativeApi.js +1 -0
- package/dist/esm/plugins/aidon/index.js +2 -0
- package/dist/esm/plugins/aidon/nodes/ChatAidonNode.js +215 -0
- package/dist/esm/plugins/aidon/plugin.js +9 -0
- package/dist/esm/plugins/anthropic/anthropic.js +187 -0
- package/dist/esm/plugins/anthropic/fetchEventSource.js +106 -0
- package/dist/esm/plugins/anthropic/index.js +2 -0
- package/dist/esm/plugins/anthropic/nodes/ChatAnthropicNode.js +652 -0
- package/dist/esm/plugins/anthropic/plugin.js +18 -0
- package/dist/esm/plugins/assemblyAi/LemurActionItemsNode.js +75 -0
- package/dist/esm/plugins/assemblyAi/LemurQaNode.js +155 -0
- package/dist/esm/plugins/assemblyAi/LemurSummaryNode.js +79 -0
- package/dist/esm/plugins/assemblyAi/LemurTaskNode.js +82 -0
- package/dist/esm/plugins/assemblyAi/TranscribeAudioNode.js +125 -0
- package/dist/esm/plugins/assemblyAi/index.js +2 -0
- package/dist/esm/plugins/assemblyAi/lemurHelpers.js +114 -0
- package/dist/esm/plugins/assemblyAi/plugin.js +32 -0
- package/dist/esm/plugins/autoevals/AutoEvalsNode.js +223 -0
- package/dist/esm/plugins/autoevals/index.js +2 -0
- package/dist/esm/plugins/autoevals/plugin.js +8 -0
- package/dist/esm/plugins/gentrace/index.js +2 -0
- package/dist/esm/plugins/gentrace/plugin.js +192 -0
- package/dist/esm/plugins/google/google.js +60 -0
- package/dist/esm/plugins/google/index.js +2 -0
- package/dist/esm/plugins/google/nodes/ChatGoogleNode.js +364 -0
- package/dist/esm/plugins/google/plugin.js +32 -0
- package/dist/esm/plugins/huggingface/index.js +2 -0
- package/dist/esm/plugins/huggingface/nodes/ChatHuggingFace.js +243 -0
- package/dist/esm/plugins/huggingface/nodes/TextToImageHuggingFace.js +189 -0
- package/dist/esm/plugins/huggingface/plugin.js +26 -0
- package/dist/esm/plugins/openai/handleOpenaiError.js +17 -0
- package/dist/esm/plugins/openai/index.js +2 -0
- package/dist/esm/plugins/openai/nodes/AttachAssistantFileNode.js +123 -0
- package/dist/esm/plugins/openai/nodes/CreateAssistantNode.js +289 -0
- package/dist/esm/plugins/openai/nodes/CreateThreadMessageNode.js +176 -0
- package/dist/esm/plugins/openai/nodes/CreateThreadNode.js +157 -0
- package/dist/esm/plugins/openai/nodes/DeleteAssistantNode.js +104 -0
- package/dist/esm/plugins/openai/nodes/DeleteThreadNode.js +97 -0
- package/dist/esm/plugins/openai/nodes/GetAssistantNode.js +118 -0
- package/dist/esm/plugins/openai/nodes/GetOpenAIFileNode.js +100 -0
- package/dist/esm/plugins/openai/nodes/GetThreadNode.js +108 -0
- package/dist/esm/plugins/openai/nodes/ListAssistantsNode.js +202 -0
- package/dist/esm/plugins/openai/nodes/ListOpenAIFilesNode.js +94 -0
- package/dist/esm/plugins/openai/nodes/ListThreadMessagesNode.js +224 -0
- package/dist/esm/plugins/openai/nodes/RunThreadNode.js +630 -0
- package/dist/esm/plugins/openai/nodes/ThreadMessageNode.js +145 -0
- package/dist/esm/plugins/openai/nodes/UploadFileNode.js +121 -0
- package/dist/esm/plugins/openai/plugin.js +44 -0
- package/dist/esm/plugins/pinecone/PineconeVectorDatabase.js +88 -0
- package/dist/esm/plugins/pinecone/index.js +2 -0
- package/dist/esm/plugins/pinecone/plugin.js +19 -0
- package/dist/esm/plugins.js +21 -0
- package/dist/esm/recording/ExecutionRecorder.js +177 -0
- package/dist/esm/recording/RecordedEvents.js +1 -0
- package/dist/esm/utils/assertNever.js +3 -0
- package/dist/esm/utils/base64.js +25 -0
- package/dist/esm/utils/chatMessageToOpenAIChatCompletionMessage.js +60 -0
- package/dist/esm/utils/coerceType.js +322 -0
- package/dist/esm/utils/compatibility.js +27 -0
- package/dist/esm/utils/copyToClipboard.js +10 -0
- package/dist/esm/utils/defaults.js +2 -0
- package/dist/esm/utils/errors.js +7 -0
- package/dist/esm/utils/expectType.js +34 -0
- package/dist/esm/utils/fetchEventSource.js +120 -0
- package/dist/esm/utils/genericUtilFunctions.js +25 -0
- package/dist/esm/utils/getPluginConfig.js +23 -0
- package/dist/esm/utils/handleEscapeCharacters.js +11 -0
- package/dist/esm/utils/index.js +14 -0
- package/dist/esm/utils/inputs.js +16 -0
- package/dist/esm/utils/interpolation.js +6 -0
- package/dist/esm/utils/misc.js +1 -0
- package/dist/esm/utils/newId.js +4 -0
- package/dist/esm/utils/openai.js +219 -0
- package/dist/esm/utils/outputs.js +14 -0
- package/dist/esm/utils/serialization/serialization.js +86 -0
- package/dist/esm/utils/serialization/serializationUtils.js +13 -0
- package/dist/esm/utils/serialization/serialization_v1.js +19 -0
- package/dist/esm/utils/serialization/serialization_v2.js +24 -0
- package/dist/esm/utils/serialization/serialization_v3.js +145 -0
- package/dist/esm/utils/serialization/serialization_v4.js +200 -0
- package/dist/esm/utils/symbols.js +2 -0
- package/dist/esm/utils/time.js +14 -0
- package/dist/esm/utils/typeSafety.js +42 -0
- package/dist/types/api/createProcessor.d.ts +37 -0
- package/dist/types/api/streaming.d.ts +56 -0
- package/dist/types/exports.d.ts +33 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/integrations/AudioProvider.d.ts +4 -0
- package/dist/types/integrations/DatasetProvider.d.ts +33 -0
- package/dist/types/integrations/EmbeddingGenerator.d.ts +3 -0
- package/dist/types/integrations/GptTokenizerTokenizer.d.ts +20 -0
- package/dist/types/integrations/LLMProvider.d.ts +7 -0
- package/dist/types/integrations/Tokenizer.d.ts +11 -0
- package/dist/types/integrations/VectorDatabase.d.ts +7 -0
- package/dist/types/integrations/enableIntegrations.d.ts +1 -0
- package/dist/types/integrations/integrations.d.ts +12 -0
- package/dist/types/integrations/openai/OpenAIEmbeddingGenerator.d.ts +10 -0
- package/dist/types/model/DataValue.d.ts +138 -0
- package/dist/types/model/Dataset.d.ts +19 -0
- package/dist/types/model/EditorDefinition.d.ts +142 -0
- package/dist/types/model/GraphProcessor.d.ts +192 -0
- package/dist/types/model/NodeBase.d.ts +110 -0
- package/dist/types/model/NodeBodySpec.d.ts +19 -0
- package/dist/types/model/NodeDefinition.d.ts +13 -0
- package/dist/types/model/NodeGraph.d.ts +15 -0
- package/dist/types/model/NodeImpl.d.ts +55 -0
- package/dist/types/model/NodeRegistration.d.ts +24 -0
- package/dist/types/model/Nodes.d.ts +84 -0
- package/dist/types/model/PluginLoadSpec.d.ts +17 -0
- package/dist/types/model/ProcessContext.d.ts +69 -0
- package/dist/types/model/Project.d.ts +17 -0
- package/dist/types/model/RivetPlugin.d.ts +45 -0
- package/dist/types/model/RivetUIContext.d.ts +18 -0
- package/dist/types/model/Settings.d.ts +15 -0
- package/dist/types/model/nodes/AbortGraphNode.d.ts +22 -0
- package/dist/types/model/nodes/AppendToDatasetNode.d.ts +21 -0
- package/dist/types/model/nodes/ArrayNode.d.ts +20 -0
- package/dist/types/model/nodes/AssembleMessageNode.d.ts +23 -0
- package/dist/types/model/nodes/AssemblePromptNode.d.ts +20 -0
- package/dist/types/model/nodes/AudioNode.d.ts +20 -0
- package/dist/types/model/nodes/BooleanNode.d.ts +19 -0
- package/dist/types/model/nodes/CallGraphNode.d.ts +16 -0
- package/dist/types/model/nodes/ChatNode.d.ts +77 -0
- package/dist/types/model/nodes/ChunkNode.d.ts +22 -0
- package/dist/types/model/nodes/CoalesceNode.d.ts +14 -0
- package/dist/types/model/nodes/CodeNode.d.ts +21 -0
- package/dist/types/model/nodes/CommentNode.d.ts +20 -0
- package/dist/types/model/nodes/CompareNode.d.ts +19 -0
- package/dist/types/model/nodes/ContextNode.d.ts +24 -0
- package/dist/types/model/nodes/CreateDatasetNode.d.ts +13 -0
- package/dist/types/model/nodes/DatasetNearestNeigborsNode.d.ts +19 -0
- package/dist/types/model/nodes/DelayNode.d.ts +20 -0
- package/dist/types/model/nodes/DelegateFunctionCallNode.d.ts +25 -0
- package/dist/types/model/nodes/DestructureNode.d.ts +18 -0
- package/dist/types/model/nodes/EvaluateNode.d.ts +19 -0
- package/dist/types/model/nodes/ExternalCallNode.d.ts +22 -0
- package/dist/types/model/nodes/ExtractJsonNode.d.ts +13 -0
- package/dist/types/model/nodes/ExtractMarkdownCodeBlocksNode.d.ts +12 -0
- package/dist/types/model/nodes/ExtractObjectPathNode.d.ts +19 -0
- package/dist/types/model/nodes/ExtractRegexNode.d.ts +22 -0
- package/dist/types/model/nodes/ExtractYamlNode.d.ts +21 -0
- package/dist/types/model/nodes/FilterNode.d.ts +13 -0
- package/dist/types/model/nodes/GetAllDatasetsNode.d.ts +14 -0
- package/dist/types/model/nodes/GetDatasetRowNode.d.ts +19 -0
- package/dist/types/model/nodes/GetEmbeddingNode.d.ts +24 -0
- package/dist/types/model/nodes/GetGlobalNode.d.ts +29 -0
- package/dist/types/model/nodes/GptFunctionNode.d.ts +25 -0
- package/dist/types/model/nodes/GraphInputNode.d.ts +24 -0
- package/dist/types/model/nodes/GraphOutputNode.d.ts +22 -0
- package/dist/types/model/nodes/GraphReferenceNode.d.ts +22 -0
- package/dist/types/model/nodes/HashNode.d.ts +17 -0
- package/dist/types/model/nodes/HttpCallNode.d.ts +27 -0
- package/dist/types/model/nodes/IfElseNode.d.ts +18 -0
- package/dist/types/model/nodes/IfNode.d.ts +17 -0
- package/dist/types/model/nodes/ImageNode.d.ts +20 -0
- package/dist/types/model/nodes/JoinNode.d.ts +21 -0
- package/dist/types/model/nodes/ListGraphsNode.d.ts +14 -0
- package/dist/types/model/nodes/LoadDatasetNode.d.ts +17 -0
- package/dist/types/model/nodes/LoopControllerNode.d.ts +20 -0
- package/dist/types/model/nodes/MatchNode.d.ts +19 -0
- package/dist/types/model/nodes/NumberNode.d.ts +21 -0
- package/dist/types/model/nodes/ObjectNode.d.ts +18 -0
- package/dist/types/model/nodes/PassthroughNode.d.ts +14 -0
- package/dist/types/model/nodes/PlayAudioNode.d.ts +15 -0
- package/dist/types/model/nodes/PopNode.d.ts +19 -0
- package/dist/types/model/nodes/PromptNode.d.ts +23 -0
- package/dist/types/model/nodes/RaceInputsNode.d.ts +17 -0
- package/dist/types/model/nodes/RaiseEventNode.d.ts +22 -0
- package/dist/types/model/nodes/RandomNumberNode.d.ts +23 -0
- package/dist/types/model/nodes/ReadDirectoryNode.d.ts +30 -0
- package/dist/types/model/nodes/ReadFileNode.d.ts +23 -0
- package/dist/types/model/nodes/ReplaceDatasetNode.d.ts +21 -0
- package/dist/types/model/nodes/SetGlobalNode.d.ts +23 -0
- package/dist/types/model/nodes/ShuffleNode.d.ts +12 -0
- package/dist/types/model/nodes/SliceNode.d.ts +22 -0
- package/dist/types/model/nodes/SplitNode.d.ts +19 -0
- package/dist/types/model/nodes/SubGraphNode.d.ts +29 -0
- package/dist/types/model/nodes/TextNode.d.ts +18 -0
- package/dist/types/model/nodes/ToJsonNode.d.ts +18 -0
- package/dist/types/model/nodes/ToYamlNode.d.ts +13 -0
- package/dist/types/model/nodes/TrimChatMessagesNode.d.ts +20 -0
- package/dist/types/model/nodes/URLReferenceNode.d.ts +19 -0
- package/dist/types/model/nodes/UserInputNode.d.ts +21 -0
- package/dist/types/model/nodes/VectorNearestNeighborsNode.d.ts +24 -0
- package/dist/types/model/nodes/VectorStoreNode.d.ts +22 -0
- package/dist/types/model/nodes/WaitForEventNode.d.ts +21 -0
- package/dist/types/native/BaseDir.d.ts +29 -0
- package/dist/types/native/BrowserNativeApi.d.ts +11 -0
- package/dist/types/native/NativeApi.d.ts +17 -0
- package/dist/types/plugins/aidon/index.d.ts +2 -0
- package/dist/types/plugins/aidon/nodes/ChatAidonNode.d.ts +3 -0
- package/dist/types/plugins/aidon/plugin.d.ts +2 -0
- package/dist/types/plugins/anthropic/anthropic.d.ts +216 -0
- package/dist/types/plugins/anthropic/fetchEventSource.d.ts +11 -0
- package/dist/types/plugins/anthropic/index.d.ts +2 -0
- package/dist/types/plugins/anthropic/nodes/ChatAnthropicNode.d.ts +30 -0
- package/dist/types/plugins/anthropic/plugin.d.ts +2 -0
- package/dist/types/plugins/assemblyAi/LemurActionItemsNode.d.ts +6 -0
- package/dist/types/plugins/assemblyAi/LemurQaNode.d.ts +22 -0
- package/dist/types/plugins/assemblyAi/LemurSummaryNode.d.ts +8 -0
- package/dist/types/plugins/assemblyAi/LemurTaskNode.d.ts +8 -0
- package/dist/types/plugins/assemblyAi/TranscribeAudioNode.d.ts +7 -0
- package/dist/types/plugins/assemblyAi/index.d.ts +2 -0
- package/dist/types/plugins/assemblyAi/lemurHelpers.d.ts +67 -0
- package/dist/types/plugins/assemblyAi/plugin.d.ts +2 -0
- package/dist/types/plugins/autoevals/AutoEvalsNode.d.ts +8 -0
- package/dist/types/plugins/autoevals/index.d.ts +2 -0
- package/dist/types/plugins/autoevals/plugin.d.ts +2 -0
- package/dist/types/plugins/gentrace/index.d.ts +2 -0
- package/dist/types/plugins/gentrace/plugin.d.ts +5 -0
- package/dist/types/plugins/google/google.d.ts +60 -0
- package/dist/types/plugins/google/index.d.ts +2 -0
- package/dist/types/plugins/google/nodes/ChatGoogleNode.d.ts +27 -0
- package/dist/types/plugins/google/plugin.d.ts +2 -0
- package/dist/types/plugins/huggingface/index.d.ts +2 -0
- package/dist/types/plugins/huggingface/nodes/ChatHuggingFace.d.ts +24 -0
- package/dist/types/plugins/huggingface/nodes/TextToImageHuggingFace.d.ts +20 -0
- package/dist/types/plugins/huggingface/plugin.d.ts +2 -0
- package/dist/types/plugins/openai/handleOpenaiError.d.ts +1 -0
- package/dist/types/plugins/openai/index.d.ts +2 -0
- package/dist/types/plugins/openai/nodes/AttachAssistantFileNode.d.ts +10 -0
- package/dist/types/plugins/openai/nodes/CreateAssistantNode.d.ts +26 -0
- package/dist/types/plugins/openai/nodes/CreateThreadMessageNode.d.ts +15 -0
- package/dist/types/plugins/openai/nodes/CreateThreadNode.d.ts +13 -0
- package/dist/types/plugins/openai/nodes/DeleteAssistantNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/DeleteThreadNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/GetAssistantNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/GetOpenAIFileNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/GetThreadNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/ListAssistantsNode.d.ts +14 -0
- package/dist/types/plugins/openai/nodes/ListOpenAIFilesNode.d.ts +8 -0
- package/dist/types/plugins/openai/nodes/ListThreadMessagesNode.d.ts +16 -0
- package/dist/types/plugins/openai/nodes/RunThreadNode.d.ts +28 -0
- package/dist/types/plugins/openai/nodes/ThreadMessageNode.d.ts +14 -0
- package/dist/types/plugins/openai/nodes/UploadFileNode.d.ts +7 -0
- package/dist/types/plugins/openai/plugin.d.ts +2 -0
- package/dist/types/plugins/pinecone/PineconeVectorDatabase.d.ts +9 -0
- package/dist/types/plugins/pinecone/index.d.ts +2 -0
- package/dist/types/plugins/pinecone/plugin.d.ts +2 -0
- package/dist/types/plugins.d.ts +20 -0
- package/dist/types/recording/ExecutionRecorder.d.ts +25 -0
- package/dist/types/recording/RecordedEvents.d.ts +100 -0
- package/dist/types/utils/assertNever.d.ts +1 -0
- package/dist/types/utils/base64.d.ts +2 -0
- package/dist/types/utils/chatMessageToOpenAIChatCompletionMessage.d.ts +3 -0
- package/dist/types/utils/coerceType.d.ts +6 -0
- package/dist/types/utils/compatibility.d.ts +3 -0
- package/dist/types/utils/copyToClipboard.d.ts +1 -0
- package/dist/types/utils/defaults.d.ts +2 -0
- package/dist/types/utils/errors.d.ts +2 -0
- package/dist/types/utils/expectType.d.ts +3 -0
- package/dist/types/utils/fetchEventSource.d.ts +12 -0
- package/dist/types/utils/genericUtilFunctions.d.ts +21 -0
- package/dist/types/utils/getPluginConfig.d.ts +2 -0
- package/dist/types/utils/handleEscapeCharacters.d.ts +2 -0
- package/dist/types/utils/index.d.ts +14 -0
- package/dist/types/utils/inputs.d.ts +3 -0
- package/dist/types/utils/interpolation.d.ts +1 -0
- package/dist/types/utils/misc.d.ts +1 -0
- package/dist/types/utils/newId.d.ts +1 -0
- package/dist/types/utils/openai.d.ts +739 -0
- package/dist/types/utils/outputs.d.ts +3 -0
- package/dist/types/utils/serialization/serialization.d.ts +12 -0
- package/dist/types/utils/serialization/serializationUtils.d.ts +6 -0
- package/dist/types/utils/serialization/serialization_v1.d.ts +3 -0
- package/dist/types/utils/serialization/serialization_v2.d.ts +3 -0
- package/dist/types/utils/serialization/serialization_v3.d.ts +19 -0
- package/dist/types/utils/serialization/serialization_v4.d.ts +9 -0
- package/dist/types/utils/symbols.d.ts +3 -0
- package/dist/types/utils/time.d.ts +22 -0
- package/dist/types/utils/typeSafety.d.ts +37 -0
- package/package.json +97 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { ChatNodeImpl, Rivet, globalRivetNodeRegistry, } from '../../../index.js';
|
|
2
|
+
import { omit } from 'lodash-es';
|
|
3
|
+
import { dedent } from 'ts-dedent';
|
|
4
|
+
import { coerceTypeOptional } from '../../../utils/coerceType.js';
|
|
5
|
+
import { pluginNodeDefinition } from '../../../model/NodeDefinition.js';
|
|
6
|
+
// Temporary
|
|
7
|
+
const cache = new Map();
|
|
8
|
+
const registry = globalRivetNodeRegistry;
|
|
9
|
+
;
|
|
10
|
+
class ChatAidonNodeImpl extends ChatNodeImpl {
|
|
11
|
+
create() {
|
|
12
|
+
const chatNode = {
|
|
13
|
+
...this.chartNode,
|
|
14
|
+
type: 'chatAidon',
|
|
15
|
+
title: 'Chat (Aidon)',
|
|
16
|
+
};
|
|
17
|
+
return chatNode;
|
|
18
|
+
}
|
|
19
|
+
removeInvalidInputs(inputs) {
|
|
20
|
+
if (!inputs['toolSchemas']) {
|
|
21
|
+
const { ['functions']: _, ...rest } = inputs;
|
|
22
|
+
return rest;
|
|
23
|
+
}
|
|
24
|
+
return inputs;
|
|
25
|
+
}
|
|
26
|
+
convertToolSchemaToSchemaDetail(toolSchema) {
|
|
27
|
+
const { name, description, parameters } = toolSchema;
|
|
28
|
+
// Assuming the structure of parameters from the example
|
|
29
|
+
const { headers, requestInBody, routeMap, url } = parameters;
|
|
30
|
+
return {
|
|
31
|
+
title: name,
|
|
32
|
+
description,
|
|
33
|
+
url,
|
|
34
|
+
headers,
|
|
35
|
+
routeMap,
|
|
36
|
+
requestInBody
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
extractPath(schemaDetail, functionName, parsedArgs) {
|
|
40
|
+
const pathTemplate = Object.keys(schemaDetail.routeMap).find(key => schemaDetail.routeMap[key] === functionName);
|
|
41
|
+
if (!pathTemplate) {
|
|
42
|
+
throw new Error(`Path for function ${functionName} not found`);
|
|
43
|
+
}
|
|
44
|
+
const path = pathTemplate.replace(/:(\w+)/g, (_, paramName) => {
|
|
45
|
+
const value = parsedArgs.parameters[paramName];
|
|
46
|
+
if (!value) {
|
|
47
|
+
throw new Error(`Parameter ${paramName} not found for function ${functionName}`);
|
|
48
|
+
}
|
|
49
|
+
return encodeURIComponent(value);
|
|
50
|
+
});
|
|
51
|
+
if (!path) {
|
|
52
|
+
throw new Error(`Path for function ${functionName} not found`);
|
|
53
|
+
}
|
|
54
|
+
return path;
|
|
55
|
+
}
|
|
56
|
+
async callToolGet(parsedArgs, schemaDetail, path, data) {
|
|
57
|
+
const queryParams = new URLSearchParams(parsedArgs.parameters).toString();
|
|
58
|
+
const fullUrl = schemaDetail.url + path + (queryParams ? "?" + queryParams : "");
|
|
59
|
+
let headers = {};
|
|
60
|
+
// Check if custom headers are set
|
|
61
|
+
const customHeaders = schemaDetail.headers;
|
|
62
|
+
if (customHeaders && typeof customHeaders === "string") {
|
|
63
|
+
headers = JSON.parse(customHeaders);
|
|
64
|
+
}
|
|
65
|
+
const response = await fetch(fullUrl, {
|
|
66
|
+
method: "GET",
|
|
67
|
+
headers: headers
|
|
68
|
+
});
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
data = {
|
|
71
|
+
error: response.statusText
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
data = await response.json();
|
|
76
|
+
}
|
|
77
|
+
return data;
|
|
78
|
+
}
|
|
79
|
+
async callToolPost(schemaDetail, path, parsedArgs, data) {
|
|
80
|
+
let headers = {
|
|
81
|
+
"Content-Type": "application/json"
|
|
82
|
+
};
|
|
83
|
+
// Check if custom headers are set
|
|
84
|
+
const customHeaders = schemaDetail.headers; // Moved this line up to the loop
|
|
85
|
+
// Check if custom headers are set and are of type string
|
|
86
|
+
if (customHeaders && typeof customHeaders === "string") {
|
|
87
|
+
let parsedCustomHeaders = JSON.parse(customHeaders);
|
|
88
|
+
headers = {
|
|
89
|
+
...headers,
|
|
90
|
+
...parsedCustomHeaders
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const fullUrl = schemaDetail.url + path;
|
|
94
|
+
const bodyContent = parsedArgs.requestBody || parsedArgs;
|
|
95
|
+
const requestInit = {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers,
|
|
98
|
+
body: JSON.stringify(bodyContent) // Use the extracted requestBody or the entire parsedArgs
|
|
99
|
+
};
|
|
100
|
+
const response = await fetch(fullUrl, requestInit);
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
data = {
|
|
103
|
+
error: response.statusText
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
data = await response.json();
|
|
108
|
+
}
|
|
109
|
+
return data;
|
|
110
|
+
}
|
|
111
|
+
// Override the process function
|
|
112
|
+
async process(inputs, context) {
|
|
113
|
+
//make sure not to include functions if we have no way to run them after.
|
|
114
|
+
inputs = this.removeInvalidInputs(inputs);
|
|
115
|
+
// Call the parent class's process method to do its job
|
|
116
|
+
let outputs = await super.process(inputs, context);
|
|
117
|
+
const funcCallOutput = outputs['function-call'] ?? outputs['function-calls'];
|
|
118
|
+
const funcCalls = funcCallOutput?.type === 'object[]'
|
|
119
|
+
? funcCallOutput.value
|
|
120
|
+
: undefined; // coerceTypeOptional(functionCallOutput, 'string[]');
|
|
121
|
+
if (funcCalls !== undefined) {
|
|
122
|
+
const messages = outputs['all-messages'];
|
|
123
|
+
const toolSchemas = coerceTypeOptional(inputs['toolSchemas'], 'gpt-function[]');
|
|
124
|
+
const functionCalls = funcCalls.map((functionCall) => ({
|
|
125
|
+
name: functionCall.name,
|
|
126
|
+
arguments: functionCall.arguments,
|
|
127
|
+
id: functionCall.id,
|
|
128
|
+
}));
|
|
129
|
+
//call the tool(s) to get the results to add to the message
|
|
130
|
+
for (const functionCall of functionCalls) {
|
|
131
|
+
// Find the schema detail that contains the function name
|
|
132
|
+
const toolSchema = toolSchemas.find(detail => detail.name == functionCall.name);
|
|
133
|
+
if (!toolSchema) {
|
|
134
|
+
throw new Error(`Function ${functionCall.name} not found in any schema`);
|
|
135
|
+
}
|
|
136
|
+
const schemaDetail = this.convertToolSchemaToSchemaDetail(toolSchema);
|
|
137
|
+
const path = this.extractPath(schemaDetail, functionCall.name, functionCall.arguments);
|
|
138
|
+
// Determine if the request should be in the body or as a query
|
|
139
|
+
let data = {};
|
|
140
|
+
if (schemaDetail.requestInBody) {
|
|
141
|
+
// If the type is set to body
|
|
142
|
+
data = await this.callToolPost(schemaDetail, path, functionCall.arguments, data);
|
|
143
|
+
}
|
|
144
|
+
else { // If the type is set to query
|
|
145
|
+
data = await this.callToolGet(functionCall.arguments, schemaDetail, path, data);
|
|
146
|
+
}
|
|
147
|
+
messages['value'].push({
|
|
148
|
+
type: "function",
|
|
149
|
+
name: functionCall.id,
|
|
150
|
+
message: JSON.stringify(data)
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
inputs = omit(inputs, ['functions', 'prompt']);
|
|
154
|
+
inputs['prompt'] = messages;
|
|
155
|
+
outputs = await super.process(inputs, context);
|
|
156
|
+
}
|
|
157
|
+
return outputs;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const createPluginNodeImpl = (chatNode) => {
|
|
161
|
+
const impl = new ChatAidonNodeImpl(chatNode);
|
|
162
|
+
return {
|
|
163
|
+
create() {
|
|
164
|
+
return impl.create();
|
|
165
|
+
},
|
|
166
|
+
getInputDefinitions(data) {
|
|
167
|
+
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
168
|
+
const inputs = impl.getInputDefinitions();
|
|
169
|
+
if (data.enableFunctionUse) {
|
|
170
|
+
inputs.push({
|
|
171
|
+
dataType: ['gpt-function', 'gpt-function[]'],
|
|
172
|
+
id: 'toolSchemas',
|
|
173
|
+
title: 'Tool Schemas',
|
|
174
|
+
description: 'The schemas required to run the tool(s) specified in the functions input keyed by function name.',
|
|
175
|
+
coerced: true,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return inputs;
|
|
179
|
+
},
|
|
180
|
+
getOutputDefinitions(data) {
|
|
181
|
+
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
182
|
+
const outputs = impl.getOutputDefinitions();
|
|
183
|
+
return outputs.filter(output => {
|
|
184
|
+
return !(output.id === 'function-calls');
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
getEditors(data) {
|
|
188
|
+
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
189
|
+
return impl.getEditors();
|
|
190
|
+
},
|
|
191
|
+
getBody(data) {
|
|
192
|
+
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
193
|
+
return impl.getBody();
|
|
194
|
+
},
|
|
195
|
+
getUIData() {
|
|
196
|
+
return {
|
|
197
|
+
infoBoxBody: dedent `
|
|
198
|
+
Makes a call to an Aidon chat model. The settings contains many options for tweaking the model's behavior.
|
|
199
|
+
`,
|
|
200
|
+
infoBoxTitle: 'Chat (Aidon) Node',
|
|
201
|
+
contextMenuTitle: 'Chat (Aidon)',
|
|
202
|
+
group: ['AI'],
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
async process(data, inputData, context) {
|
|
206
|
+
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
207
|
+
return impl.process(inputData, context);
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
export function chatAidonNode() {
|
|
212
|
+
const chatNode = registry.create('chat');
|
|
213
|
+
const chatAidonNodeImpl = createPluginNodeImpl(chatNode);
|
|
214
|
+
return pluginNodeDefinition(chatAidonNodeImpl, 'Chat Aidon Node');
|
|
215
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import fetchEventSource from './fetchEventSource.js';
|
|
2
|
+
export const anthropicModels = {
|
|
3
|
+
'claude-instant-1': {
|
|
4
|
+
maxTokens: 100_000,
|
|
5
|
+
cost: {
|
|
6
|
+
prompt: 0.00163,
|
|
7
|
+
completion: 0.00551,
|
|
8
|
+
},
|
|
9
|
+
displayName: 'Claude Instant',
|
|
10
|
+
},
|
|
11
|
+
'claude-instant-1.2': {
|
|
12
|
+
maxTokens: 100_000,
|
|
13
|
+
cost: {
|
|
14
|
+
prompt: 0.8e-6,
|
|
15
|
+
completion: 2.4e-6,
|
|
16
|
+
},
|
|
17
|
+
displayName: 'Claude Instant 1.2',
|
|
18
|
+
},
|
|
19
|
+
'claude-2': {
|
|
20
|
+
maxTokens: 100_000,
|
|
21
|
+
cost: {
|
|
22
|
+
prompt: 8e-6,
|
|
23
|
+
completion: 24e-6,
|
|
24
|
+
},
|
|
25
|
+
displayName: 'Claude 2',
|
|
26
|
+
},
|
|
27
|
+
'claude-2.1': {
|
|
28
|
+
maxTokens: 200_000,
|
|
29
|
+
cost: {
|
|
30
|
+
prompt: 8e-6,
|
|
31
|
+
completion: 24e-6,
|
|
32
|
+
},
|
|
33
|
+
displayName: 'Claude 2.1',
|
|
34
|
+
},
|
|
35
|
+
'claude-3-haiku-20240307': {
|
|
36
|
+
maxTokens: 200_000,
|
|
37
|
+
cost: {
|
|
38
|
+
prompt: 0.25e-6,
|
|
39
|
+
completion: 1.25e-6,
|
|
40
|
+
},
|
|
41
|
+
displayName: 'Claude 3 Haiku',
|
|
42
|
+
},
|
|
43
|
+
'claude-3-sonnet-20240229': {
|
|
44
|
+
maxTokens: 200_000,
|
|
45
|
+
cost: {
|
|
46
|
+
prompt: 3e-6,
|
|
47
|
+
completion: 15e-6,
|
|
48
|
+
},
|
|
49
|
+
displayName: 'Claude 3 Sonnet',
|
|
50
|
+
},
|
|
51
|
+
'claude-3-opus-20240229': {
|
|
52
|
+
maxTokens: 200_000,
|
|
53
|
+
cost: {
|
|
54
|
+
prompt: 15e-6,
|
|
55
|
+
completion: 75e-6,
|
|
56
|
+
},
|
|
57
|
+
displayName: 'Claude 3 Opus',
|
|
58
|
+
},
|
|
59
|
+
'claude-3-5-sonnet-20240620': {
|
|
60
|
+
maxTokens: 200_000,
|
|
61
|
+
cost: {
|
|
62
|
+
prompt: 3e-6,
|
|
63
|
+
completion: 15e-6,
|
|
64
|
+
},
|
|
65
|
+
displayName: 'Claude 3.5 Sonnet',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
export const anthropicModelOptions = Object.entries(anthropicModels).map(([id, { displayName }]) => ({
|
|
69
|
+
value: id,
|
|
70
|
+
label: displayName,
|
|
71
|
+
}));
|
|
72
|
+
export async function* streamChatCompletions({ apiKey, signal, ...rest }) {
|
|
73
|
+
const defaultSignal = new AbortController().signal;
|
|
74
|
+
const response = await fetchEventSource('https://api.anthropic.com/v1/complete', {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: {
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
'x-api-key': apiKey,
|
|
79
|
+
'anthropic-version': '2023-06-01',
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
...rest,
|
|
83
|
+
stream: true,
|
|
84
|
+
}),
|
|
85
|
+
signal: signal ?? defaultSignal,
|
|
86
|
+
});
|
|
87
|
+
let hadChunks = false;
|
|
88
|
+
let nextDataType;
|
|
89
|
+
for await (const chunk of response.events()) {
|
|
90
|
+
hadChunks = true;
|
|
91
|
+
if (chunk === '[DONE]') {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
else if (/\[\w+\]/.test(chunk)) {
|
|
95
|
+
nextDataType = chunk.slice(1, -1);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
let data;
|
|
99
|
+
try {
|
|
100
|
+
data = JSON.parse(chunk);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
console.error('JSON parse failed on chunk: ', chunk);
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
yield data;
|
|
107
|
+
}
|
|
108
|
+
if (!hadChunks) {
|
|
109
|
+
const responseJson = await response.json();
|
|
110
|
+
throw new AnthropicError(`No chunks received. Response: ${JSON.stringify(responseJson)}`, response, responseJson);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export async function callMessageApi({ apiKey, signal, tools, ...rest }) {
|
|
114
|
+
const defaultSignal = new AbortController().signal;
|
|
115
|
+
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
|
116
|
+
method: 'POST',
|
|
117
|
+
headers: {
|
|
118
|
+
'Content-Type': 'application/json',
|
|
119
|
+
'x-api-key': apiKey,
|
|
120
|
+
'anthropic-version': '2023-06-01',
|
|
121
|
+
'anthropic-beta': tools ? 'tools-2024-04-04' : 'messages-2023-12-15',
|
|
122
|
+
},
|
|
123
|
+
body: JSON.stringify({
|
|
124
|
+
...rest,
|
|
125
|
+
tools,
|
|
126
|
+
stream: false,
|
|
127
|
+
}),
|
|
128
|
+
signal: signal ?? defaultSignal,
|
|
129
|
+
});
|
|
130
|
+
const responseJson = await response.json();
|
|
131
|
+
if (response.status !== 200) {
|
|
132
|
+
throw new AnthropicError(responseJson?.error?.message ?? 'Request failed', response, responseJson);
|
|
133
|
+
}
|
|
134
|
+
return responseJson;
|
|
135
|
+
}
|
|
136
|
+
export async function* streamMessageApi({ apiKey, signal, ...rest }) {
|
|
137
|
+
// Use the Messages API for Claude 3 models
|
|
138
|
+
const defaultSignal = new AbortController().signal;
|
|
139
|
+
const response = await fetchEventSource('https://api.anthropic.com/v1/messages', {
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: {
|
|
142
|
+
'Content-Type': 'application/json',
|
|
143
|
+
'x-api-key': apiKey,
|
|
144
|
+
'anthropic-version': '2023-06-01',
|
|
145
|
+
'anthropic-beta': 'messages-2023-12-15',
|
|
146
|
+
},
|
|
147
|
+
body: JSON.stringify({
|
|
148
|
+
...rest,
|
|
149
|
+
stream: true,
|
|
150
|
+
}),
|
|
151
|
+
signal: signal ?? defaultSignal,
|
|
152
|
+
});
|
|
153
|
+
let hadChunks = false;
|
|
154
|
+
let nextDataType;
|
|
155
|
+
for await (const chunk of response.events()) {
|
|
156
|
+
hadChunks = true;
|
|
157
|
+
if (chunk === '[message_stop]') {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
else if (/\[\w+\]/.test(chunk)) {
|
|
161
|
+
nextDataType = chunk.slice(1, -1);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
let data;
|
|
165
|
+
try {
|
|
166
|
+
data = JSON.parse(chunk);
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
console.error('JSON parse failed on chunk: ', chunk);
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
yield data;
|
|
173
|
+
}
|
|
174
|
+
if (!hadChunks) {
|
|
175
|
+
const responseJson = await response.json();
|
|
176
|
+
throw new AnthropicError(`No chunks received. Response: ${JSON.stringify(responseJson)}`, response, responseJson);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export class AnthropicError extends Error {
|
|
180
|
+
response;
|
|
181
|
+
responseJson;
|
|
182
|
+
constructor(message, response, responseJson) {
|
|
183
|
+
super(message);
|
|
184
|
+
this.response = response;
|
|
185
|
+
this.responseJson = responseJson;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// https://github.com/openai/openai-node/issues/18#issuecomment-1518715285
|
|
2
|
+
export class EventSourceResponse extends Response {
|
|
3
|
+
name;
|
|
4
|
+
streams;
|
|
5
|
+
constructor(body, init) {
|
|
6
|
+
if (body == null) {
|
|
7
|
+
super(null, init);
|
|
8
|
+
this.name = 'EventSourceResponse';
|
|
9
|
+
this.streams = null;
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const [bodyForString, bodyForEvents] = body.tee();
|
|
13
|
+
const streams = createEventStream(bodyForEvents);
|
|
14
|
+
// By passing our transformed stream into the Response constructor, we prevent anyone
|
|
15
|
+
// from accidentally accessing the raw response.body stream.
|
|
16
|
+
super(bodyForString, init);
|
|
17
|
+
this.name = 'EventSourceResponse';
|
|
18
|
+
this.streams = streams;
|
|
19
|
+
}
|
|
20
|
+
async *events() {
|
|
21
|
+
if (this.streams == null) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const reader = this.streams.eventStream.getReader();
|
|
25
|
+
try {
|
|
26
|
+
while (true) {
|
|
27
|
+
const { done, value } = await this.raceWithTimeout(reader.read());
|
|
28
|
+
if (done) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
yield value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
reader.releaseLock();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async raceWithTimeout(promise, timeout = 5000) {
|
|
39
|
+
// eslint-disable-next-line no-async-promise-executor -- Error handled correctly
|
|
40
|
+
return new Promise(async (resolve, reject) => {
|
|
41
|
+
const timer = setTimeout(() => {
|
|
42
|
+
reject(new Error('Timeout: API response took too long.'));
|
|
43
|
+
}, timeout);
|
|
44
|
+
try {
|
|
45
|
+
const result = await promise;
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
resolve(result);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
reject(error);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export default async function fetchEventSource(url, init) {
|
|
57
|
+
const headers = {
|
|
58
|
+
...init?.headers,
|
|
59
|
+
accept: 'text/event-stream',
|
|
60
|
+
};
|
|
61
|
+
const response = await fetch(url, {
|
|
62
|
+
...init,
|
|
63
|
+
headers,
|
|
64
|
+
});
|
|
65
|
+
return new EventSourceResponse(response.body, response);
|
|
66
|
+
}
|
|
67
|
+
class LineSplitter {
|
|
68
|
+
separator;
|
|
69
|
+
buffer = '';
|
|
70
|
+
constructor(separator = /\n+/) {
|
|
71
|
+
this.separator = separator;
|
|
72
|
+
}
|
|
73
|
+
transform(chunk, controller) {
|
|
74
|
+
this.buffer += chunk;
|
|
75
|
+
const lines = this.buffer.split(this.separator);
|
|
76
|
+
this.buffer = lines.pop() ?? '';
|
|
77
|
+
for (const line of lines) {
|
|
78
|
+
controller.enqueue(line);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
flush(controller) {
|
|
82
|
+
if (this.buffer.length > 0) {
|
|
83
|
+
controller.enqueue(this.buffer);
|
|
84
|
+
this.buffer = '';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createEventStream(body) {
|
|
89
|
+
if (body == null) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const textStream = body.pipeThrough(new TextDecoderStream());
|
|
93
|
+
const eventStream = textStream.pipeThrough(new TransformStream(new LineSplitter())).pipeThrough(new TransformStream({
|
|
94
|
+
transform(line, controller) {
|
|
95
|
+
if (line.startsWith('data: ')) {
|
|
96
|
+
const data = line.slice(6).trim();
|
|
97
|
+
controller.enqueue(data);
|
|
98
|
+
}
|
|
99
|
+
else if (line.startsWith('event: ')) {
|
|
100
|
+
const event = line.slice(7).trim();
|
|
101
|
+
controller.enqueue(`[${event}]`);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
}));
|
|
105
|
+
return { eventStream, textStream };
|
|
106
|
+
}
|