@valerypopoff/rivet2-core 2.0.0
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 +15 -0
- package/dist/cjs/bundle.cjs +28772 -0
- package/dist/cjs/bundle.cjs.map +7 -0
- package/dist/esm/api/createProcessor.js +116 -0
- package/dist/esm/api/looseDataValue.js +16 -0
- package/dist/esm/api/processSettings.js +16 -0
- package/dist/esm/api/streaming.js +116 -0
- package/dist/esm/exports.js +48 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/integrations/AudioProvider.js +1 -0
- package/dist/esm/integrations/CodeRunner.js +44 -0
- package/dist/esm/integrations/DatasetProvider.js +92 -0
- package/dist/esm/integrations/EmbeddingGenerator.js +1 -0
- package/dist/esm/integrations/GptTokenizerTokenizer.js +67 -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/mcp/MCPBase.js +87 -0
- package/dist/esm/integrations/mcp/MCPProvider.js +23 -0
- package/dist/esm/integrations/mcp/MCPUtils.js +33 -0
- package/dist/esm/integrations/openai/OpenAIEmbeddingGenerator.js +23 -0
- package/dist/esm/model/CycleDetector.js +43 -0
- package/dist/esm/model/DataValue.js +188 -0
- package/dist/esm/model/Dataset.js +1 -0
- package/dist/esm/model/EditorDefinition.js +1 -0
- package/dist/esm/model/ExecutorProtocol.js +1 -0
- package/dist/esm/model/GraphPreprocessor.js +97 -0
- package/dist/esm/model/GraphProcessor.js +1136 -0
- package/dist/esm/model/NodeBase.js +11 -0
- package/dist/esm/model/NodeBodySpec.js +1 -0
- package/dist/esm/model/NodeDefinition.js +12 -0
- package/dist/esm/model/NodeExecutionPlanner.js +82 -0
- package/dist/esm/model/NodeGraph.js +14 -0
- package/dist/esm/model/NodeImpl.js +57 -0
- package/dist/esm/model/NodeRegistration.js +146 -0
- package/dist/esm/model/Nodes.js +279 -0
- package/dist/esm/model/PluginLoadSpec.js +1 -0
- package/dist/esm/model/ProcessContext.js +1 -0
- package/dist/esm/model/ProcessContextBuilder.js +33 -0
- package/dist/esm/model/Project.js +2 -0
- package/dist/esm/model/ProjectReferenceLoader.js +1 -0
- package/dist/esm/model/RecordingPlayer.js +262 -0
- package/dist/esm/model/RegistryAssembly.js +53 -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/SplitRunProcessor.js +98 -0
- package/dist/esm/model/SubprocessorBridge.js +82 -0
- package/dist/esm/model/chat/aiSdkMessages.js +105 -0
- package/dist/esm/model/chat/aiSdkStreaming.js +38 -0
- package/dist/esm/model/chat/aiSdkTools.js +10 -0
- package/dist/esm/model/chat/chatCost.js +33 -0
- package/dist/esm/model/chat/chatMessages.js +47 -0
- package/dist/esm/model/chat/openAIChatRequest.js +110 -0
- package/dist/esm/model/chat/openAIChatRuntime.js +220 -0
- package/dist/esm/model/chat/streamChatResponse.js +97 -0
- package/dist/esm/model/chat/tokenBudget.js +16 -0
- package/dist/esm/model/chat-v2/aiSdkBridge.js +88 -0
- package/dist/esm/model/chat-v2/chatV2EditorCache.js +107 -0
- package/dist/esm/model/chat-v2/chatV2Errors.js +255 -0
- package/dist/esm/model/chat-v2/chatV2FeatureCompatibility.js +16 -0
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +387 -0
- package/dist/esm/model/chat-v2/chatV2ResponseFormat.js +57 -0
- package/dist/esm/model/chat-v2/chatV2Retry.js +41 -0
- package/dist/esm/model/chat-v2/chatV2RuntimeOptions.js +242 -0
- package/dist/esm/model/chat-v2/chatV2Shared.js +285 -0
- package/dist/esm/model/chat-v2/chatV2Types.js +1 -0
- package/dist/esm/model/chat-v2/index.js +10 -0
- package/dist/esm/model/chat-v2/llmChatV2NodeData.js +56 -0
- package/dist/esm/model/chat-v2/llmChatV2NodeEditors.js +475 -0
- package/dist/esm/model/chat-v2/llmChatV2NodeRuntime.js +90 -0
- package/dist/esm/model/chat-v2/messageConverter.js +173 -0
- package/dist/esm/model/chat-v2/modelRegistry.js +23 -0
- package/dist/esm/model/chat-v2/providerOptions.js +202 -0
- package/dist/esm/model/chat-v2/toolContinuation.js +108 -0
- package/dist/esm/model/chat-v2/toolConverter.js +4 -0
- package/dist/esm/model/interpolationInputDefinition.js +36 -0
- package/dist/esm/model/loopControllerBreak.js +4 -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 +209 -0
- package/dist/esm/model/nodes/AssemblePromptNode.js +151 -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 +135 -0
- package/dist/esm/model/nodes/ChatLoopNode.js +150 -0
- package/dist/esm/model/nodes/ChatNode.js +56 -0
- package/dist/esm/model/nodes/ChatNodeBase.js +912 -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 +197 -0
- package/dist/esm/model/nodes/CommentNode.js +71 -0
- package/dist/esm/model/nodes/CompareNode.js +144 -0
- package/dist/esm/model/nodes/ContextNode.js +99 -0
- package/dist/esm/model/nodes/CreateDatasetNode.js +71 -0
- package/dist/esm/model/nodes/CronNode.js +248 -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 +175 -0
- package/dist/esm/model/nodes/DestructureNode.js +110 -0
- package/dist/esm/model/nodes/DocumentNode.js +183 -0
- package/dist/esm/model/nodes/EvaluateNode.js +141 -0
- package/dist/esm/model/nodes/ExpressionNode.js +143 -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 +159 -0
- package/dist/esm/model/nodes/ExtractRegexNode.js +200 -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/GraphInputNode.js +132 -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 +592 -0
- package/dist/esm/model/nodes/IfElseNode.js +138 -0
- package/dist/esm/model/nodes/IfNode.js +129 -0
- package/dist/esm/model/nodes/ImageNode.js +107 -0
- package/dist/esm/model/nodes/JSFilterNode.js +69 -0
- package/dist/esm/model/nodes/JSMapNode.js +69 -0
- package/dist/esm/model/nodes/JoinNode.js +135 -0
- package/dist/esm/model/nodes/LLMChatV2Node.js +234 -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/LoopUntilNode.js +214 -0
- package/dist/esm/model/nodes/MCPDiscoveryNode.js +214 -0
- package/dist/esm/model/nodes/MCPGetPromptNode.js +234 -0
- package/dist/esm/model/nodes/MCPToolCallNode.js +262 -0
- package/dist/esm/model/nodes/MatchNode.js +160 -0
- package/dist/esm/model/nodes/NumberNode.js +86 -0
- package/dist/esm/model/nodes/ObjectNode.js +152 -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 +248 -0
- package/dist/esm/model/nodes/RaceInputsNode.js +85 -0
- package/dist/esm/model/nodes/RaiseEventNode.js +84 -0
- package/dist/esm/model/nodes/RandomNumberNode.js +117 -0
- package/dist/esm/model/nodes/ReadAllFilesNode.js +210 -0
- package/dist/esm/model/nodes/ReadDirectoryNode.js +171 -0
- package/dist/esm/model/nodes/ReadFileNode.js +114 -0
- package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +199 -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 +99 -0
- package/dist/esm/model/nodes/SplitNode.js +101 -0
- package/dist/esm/model/nodes/SubGraphNode.js +180 -0
- package/dist/esm/model/nodes/TextNode.js +125 -0
- package/dist/esm/model/nodes/ToJsonNode.js +78 -0
- package/dist/esm/model/nodes/ToMarkdownTableNode.js +119 -0
- package/dist/esm/model/nodes/ToTreeNode.js +133 -0
- package/dist/esm/model/nodes/ToYamlNode.js +68 -0
- package/dist/esm/model/nodes/ToolNode.js +172 -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 +109 -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/model/nodes/codeNodeErrorDiagnostics.js +104 -0
- package/dist/esm/model/nodes/jsListCallbackHelpers.js +196 -0
- package/dist/esm/model/nodes/jsValueInterpolation.js +163 -0
- package/dist/esm/model/nodes/toolCallDelegation.js +159 -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/anthropic/anthropic.js +208 -0
- package/dist/esm/plugins/anthropic/fetchEventSource.js +107 -0
- package/dist/esm/plugins/anthropic/index.js +2 -0
- package/dist/esm/plugins/anthropic/nodes/ChatAnthropicNode.js +767 -0
- package/dist/esm/plugins/anthropic/plugin.js +26 -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 +126 -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 +3 -0
- package/dist/esm/plugins/gentrace/plugin.js +195 -0
- package/dist/esm/plugins/google/google.js +165 -0
- package/dist/esm/plugins/google/index.js +2 -0
- package/dist/esm/plugins/google/nodes/ChatGoogleNode.js +584 -0
- package/dist/esm/plugins/google/plugin.js +39 -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 +143 -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 +29 -0
- package/dist/esm/recording/ExecutionRecorder.js +273 -0
- package/dist/esm/recording/RecordedEvents.js +1 -0
- package/dist/esm/utils/assertNever.js +3 -0
- package/dist/esm/utils/base64.js +38 -0
- package/dist/esm/utils/chatMessageToOpenAIChatCompletionMessage.js +73 -0
- package/dist/esm/utils/coerceType.js +326 -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/emitDetached.js +12 -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 +17 -0
- package/dist/esm/utils/inputs.js +16 -0
- package/dist/esm/utils/interpolation.js +286 -0
- package/dist/esm/utils/misc.js +1 -0
- package/dist/esm/utils/newId.js +4 -0
- package/dist/esm/utils/openai.js +206 -0
- package/dist/esm/utils/orderedStringPortIds.js +27 -0
- package/dist/esm/utils/outputs.js +14 -0
- package/dist/esm/utils/pQueueCompat.js +12 -0
- package/dist/esm/utils/paths.js +80 -0
- package/dist/esm/utils/providerStreamParsing.js +14 -0
- package/dist/esm/utils/runtimeLogging.js +121 -0
- package/dist/esm/utils/serialization/serialization.js +77 -0
- package/dist/esm/utils/serialization/serializationHelpers.js +63 -0
- package/dist/esm/utils/serialization/serializationUtils.js +112 -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 +100 -0
- package/dist/esm/utils/serialization/serialization_v4.js +156 -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/esm/vendor/fnv1a.js +80 -0
- package/dist/types/api/createProcessor.d.ts +56 -0
- package/dist/types/api/looseDataValue.d.ts +4 -0
- package/dist/types/api/processSettings.d.ts +4 -0
- package/dist/types/api/streaming.d.ts +56 -0
- package/dist/types/exports.d.ts +49 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/integrations/AudioProvider.d.ts +4 -0
- package/dist/types/integrations/CodeRunner.d.ts +19 -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/mcp/MCPBase.d.ts +18 -0
- package/dist/types/integrations/mcp/MCPProvider.d.ts +153 -0
- package/dist/types/integrations/mcp/MCPUtils.d.ts +9 -0
- package/dist/types/integrations/openai/OpenAIEmbeddingGenerator.d.ts +10 -0
- package/dist/types/model/CycleDetector.d.ts +2 -0
- package/dist/types/model/DataValue.d.ts +160 -0
- package/dist/types/model/Dataset.d.ts +19 -0
- package/dist/types/model/EditorDefinition.d.ts +176 -0
- package/dist/types/model/ExecutorProtocol.d.ts +216 -0
- package/dist/types/model/GraphPreprocessor.d.ts +24 -0
- package/dist/types/model/GraphProcessor.d.ts +210 -0
- package/dist/types/model/NodeBase.d.ts +116 -0
- package/dist/types/model/NodeBodySpec.d.ts +19 -0
- package/dist/types/model/NodeDefinition.d.ts +13 -0
- package/dist/types/model/NodeExecutionPlanner.d.ts +28 -0
- package/dist/types/model/NodeGraph.d.ts +15 -0
- package/dist/types/model/NodeImpl.d.ts +56 -0
- package/dist/types/model/NodeRegistration.d.ts +29 -0
- package/dist/types/model/Nodes.d.ts +112 -0
- package/dist/types/model/PluginLoadSpec.d.ts +17 -0
- package/dist/types/model/ProcessContext.d.ts +110 -0
- package/dist/types/model/ProcessContextBuilder.d.ts +41 -0
- package/dist/types/model/Project.d.ts +31 -0
- package/dist/types/model/ProjectReferenceLoader.d.ts +5 -0
- package/dist/types/model/RecordingPlayer.d.ts +27 -0
- package/dist/types/model/RegistryAssembly.d.ts +42 -0
- package/dist/types/model/RivetPlugin.d.ts +51 -0
- package/dist/types/model/RivetUIContext.d.ts +27 -0
- package/dist/types/model/Settings.d.ts +20 -0
- package/dist/types/model/SplitRunProcessor.d.ts +31 -0
- package/dist/types/model/SubprocessorBridge.d.ts +13 -0
- package/dist/types/model/chat/aiSdkMessages.d.ts +3 -0
- package/dist/types/model/chat/aiSdkStreaming.d.ts +9 -0
- package/dist/types/model/chat/aiSdkTools.d.ts +3 -0
- package/dist/types/model/chat/chatCost.d.ts +30 -0
- package/dist/types/model/chat/chatMessages.d.ts +5 -0
- package/dist/types/model/chat/openAIChatRequest.d.ts +39 -0
- package/dist/types/model/chat/openAIChatRuntime.d.ts +61 -0
- package/dist/types/model/chat/streamChatResponse.d.ts +27 -0
- package/dist/types/model/chat/tokenBudget.d.ts +3 -0
- package/dist/types/model/chat-v2/aiSdkBridge.d.ts +2 -0
- package/dist/types/model/chat-v2/chatV2EditorCache.d.ts +25 -0
- package/dist/types/model/chat-v2/chatV2Errors.d.ts +23 -0
- package/dist/types/model/chat-v2/chatV2FeatureCompatibility.d.ts +12 -0
- package/dist/types/model/chat-v2/chatV2Pipeline.d.ts +2 -0
- package/dist/types/model/chat-v2/chatV2ResponseFormat.d.ts +24 -0
- package/dist/types/model/chat-v2/chatV2Retry.d.ts +5 -0
- package/dist/types/model/chat-v2/chatV2RuntimeOptions.d.ts +12 -0
- package/dist/types/model/chat-v2/chatV2Shared.d.ts +56 -0
- package/dist/types/model/chat-v2/chatV2Types.d.ts +110 -0
- package/dist/types/model/chat-v2/index.d.ts +10 -0
- package/dist/types/model/chat-v2/llmChatV2NodeData.d.ts +73 -0
- package/dist/types/model/chat-v2/llmChatV2NodeEditors.d.ts +4 -0
- package/dist/types/model/chat-v2/llmChatV2NodeRuntime.d.ts +23 -0
- package/dist/types/model/chat-v2/messageConverter.d.ts +8 -0
- package/dist/types/model/chat-v2/modelRegistry.d.ts +296 -0
- package/dist/types/model/chat-v2/providerOptions.d.ts +59 -0
- package/dist/types/model/chat-v2/toolContinuation.d.ts +15 -0
- package/dist/types/model/chat-v2/toolConverter.d.ts +3 -0
- package/dist/types/model/interpolationInputDefinition.d.ts +16 -0
- package/dist/types/model/loopControllerBreak.d.ts +3 -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 +23 -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/ChatLoopNode.d.ts +21 -0
- package/dist/types/model/nodes/ChatNode.d.ts +17 -0
- package/dist/types/model/nodes/ChatNodeBase.d.ts +85 -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 +27 -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/CronNode.d.ts +34 -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 +28 -0
- package/dist/types/model/nodes/DestructureNode.d.ts +19 -0
- package/dist/types/model/nodes/DocumentNode.d.ts +28 -0
- package/dist/types/model/nodes/EvaluateNode.d.ts +19 -0
- package/dist/types/model/nodes/ExpressionNode.d.ts +21 -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 +22 -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/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 +32 -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/JSFilterNode.d.ts +19 -0
- package/dist/types/model/nodes/JSMapNode.d.ts +19 -0
- package/dist/types/model/nodes/JoinNode.d.ts +21 -0
- package/dist/types/model/nodes/LLMChatV2Node.d.ts +20 -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/LoopUntilNode.d.ts +32 -0
- package/dist/types/model/nodes/MCPDiscoveryNode.d.ts +9 -0
- package/dist/types/model/nodes/MCPGetPromptNode.d.ts +23 -0
- package/dist/types/model/nodes/MCPToolCallNode.d.ts +26 -0
- package/dist/types/model/nodes/MatchNode.d.ts +20 -0
- package/dist/types/model/nodes/NumberNode.d.ts +21 -0
- package/dist/types/model/nodes/ObjectNode.d.ts +19 -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 +25 -0
- package/dist/types/model/nodes/RaceInputsNode.d.ts +16 -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/ReadAllFilesNode.d.ts +30 -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/ReferencedGraphAliasNode.d.ts +31 -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 +19 -0
- package/dist/types/model/nodes/ToJsonNode.d.ts +18 -0
- package/dist/types/model/nodes/ToMarkdownTableNode.d.ts +19 -0
- package/dist/types/model/nodes/ToTreeNode.d.ts +21 -0
- package/dist/types/model/nodes/ToYamlNode.d.ts +13 -0
- package/dist/types/model/nodes/ToolNode.d.ts +25 -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 +20 -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/model/nodes/codeNodeErrorDiagnostics.d.ts +9 -0
- package/dist/types/model/nodes/jsListCallbackHelpers.d.ts +23 -0
- package/dist/types/model/nodes/jsValueInterpolation.d.ts +11 -0
- package/dist/types/model/nodes/toolCallDelegation.d.ts +29 -0
- package/dist/types/native/BaseDir.d.ts +29 -0
- package/dist/types/native/BrowserNativeApi.d.ts +9 -0
- package/dist/types/native/NativeApi.d.ts +17 -0
- package/dist/types/plugins/anthropic/anthropic.d.ts +315 -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 +40 -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 +10 -0
- package/dist/types/plugins/google/google.d.ts +129 -0
- package/dist/types/plugins/google/index.d.ts +2 -0
- package/dist/types/plugins/google/nodes/ChatGoogleNode.d.ts +35 -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 +22 -0
- package/dist/types/recording/ExecutionRecorder.d.ts +27 -0
- package/dist/types/recording/RecordedEvents.d.ts +114 -0
- package/dist/types/utils/assertNever.d.ts +1 -0
- package/dist/types/utils/base64.d.ts +3 -0
- package/dist/types/utils/chatMessageToOpenAIChatCompletionMessage.d.ts +5 -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/emitDetached.d.ts +11 -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 +17 -0
- package/dist/types/utils/inputs.d.ts +3 -0
- package/dist/types/utils/interpolation.d.ts +26 -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 +784 -0
- package/dist/types/utils/orderedStringPortIds.d.ts +10 -0
- package/dist/types/utils/outputs.d.ts +3 -0
- package/dist/types/utils/pQueueCompat.d.ts +10 -0
- package/dist/types/utils/paths.d.ts +8 -0
- package/dist/types/utils/providerStreamParsing.d.ts +1 -0
- package/dist/types/utils/runtimeLogging.d.ts +10 -0
- package/dist/types/utils/serialization/serialization.d.ts +12 -0
- package/dist/types/utils/serialization/serializationHelpers.d.ts +27 -0
- package/dist/types/utils/serialization/serializationUtils.d.ts +16 -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/dist/types/vendor/fnv1a.d.ts +9 -0
- package/package.json +77 -0
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
import {} from '../../../index.js';
|
|
2
|
+
import { anthropicModelOptions, anthropicModels, streamChatCompletions, AnthropicError, streamMessageApi, } from '../anthropic.js';
|
|
3
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
4
|
+
import { dedent } from 'ts-dedent';
|
|
5
|
+
import retry from 'p-retry';
|
|
6
|
+
import { coerceType, coerceTypeOptional } from '../../../utils/coerceType.js';
|
|
7
|
+
import { getError } from '../../../utils/errors.js';
|
|
8
|
+
import { pluginNodeDefinition } from '../../../model/NodeDefinition.js';
|
|
9
|
+
import { assertNever } from '../../../utils/assertNever.js';
|
|
10
|
+
import { isNotNull } from '../../../utils/genericUtilFunctions.js';
|
|
11
|
+
import { uint8ArrayToBase64 } from '../../../utils/base64.js';
|
|
12
|
+
import { getInputOrData, cleanHeaders } from '../../../utils/inputs.js';
|
|
13
|
+
import { coercePromptToChatMessages } from '../../../model/chat/chatMessages.js';
|
|
14
|
+
import { clampMaxTokensToModelLimit, setRequestAndResponseTokenOutputs } from '../../../model/chat/tokenBudget.js';
|
|
15
|
+
import { createAssistantMessagesOutput } from '../../../model/chat/streamChatResponse.js';
|
|
16
|
+
import { logRuntimeDebug, summarizeErrorForLog } from '../../../utils/runtimeLogging.js';
|
|
17
|
+
// Temporary
|
|
18
|
+
const cache = new Map();
|
|
19
|
+
export const ChatAnthropicNodeImpl = {
|
|
20
|
+
create() {
|
|
21
|
+
const chartNode = {
|
|
22
|
+
type: 'chatAnthropic',
|
|
23
|
+
title: 'Chat (Anthropic, Legacy)',
|
|
24
|
+
id: nanoid(),
|
|
25
|
+
visualData: {
|
|
26
|
+
x: 0,
|
|
27
|
+
y: 0,
|
|
28
|
+
width: 275,
|
|
29
|
+
},
|
|
30
|
+
data: {
|
|
31
|
+
model: 'claude-sonnet-4-20250514',
|
|
32
|
+
useModelInput: false,
|
|
33
|
+
temperature: 0.5,
|
|
34
|
+
useTemperatureInput: false,
|
|
35
|
+
top_p: 1,
|
|
36
|
+
useTopPInput: false,
|
|
37
|
+
top_k: undefined,
|
|
38
|
+
useTopKInput: false,
|
|
39
|
+
useTopP: false,
|
|
40
|
+
useUseTopPInput: false,
|
|
41
|
+
maxTokens: 1024,
|
|
42
|
+
useMaxTokensInput: false,
|
|
43
|
+
useStop: false,
|
|
44
|
+
stop: '',
|
|
45
|
+
useStopInput: false,
|
|
46
|
+
cache: false,
|
|
47
|
+
useAsGraphPartialOutput: true,
|
|
48
|
+
enableToolUse: false,
|
|
49
|
+
endpoint: '',
|
|
50
|
+
useEndpointInput: false,
|
|
51
|
+
overrideModel: undefined,
|
|
52
|
+
useOverrideModelInput: false,
|
|
53
|
+
enableCitations: false,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
return chartNode;
|
|
57
|
+
},
|
|
58
|
+
getInputDefinitions(data) {
|
|
59
|
+
const inputs = [];
|
|
60
|
+
if (data.model.startsWith('claude-3')) {
|
|
61
|
+
inputs.push({
|
|
62
|
+
dataType: 'string',
|
|
63
|
+
id: 'system',
|
|
64
|
+
title: 'System Prompt',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (data.useModelInput) {
|
|
68
|
+
inputs.push({
|
|
69
|
+
id: 'model',
|
|
70
|
+
title: 'Model',
|
|
71
|
+
dataType: 'string',
|
|
72
|
+
required: false,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (data.useTemperatureInput) {
|
|
76
|
+
inputs.push({
|
|
77
|
+
dataType: 'number',
|
|
78
|
+
id: 'temperature',
|
|
79
|
+
title: 'Temperature',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (data.useTopPInput) {
|
|
83
|
+
inputs.push({
|
|
84
|
+
dataType: 'number',
|
|
85
|
+
id: 'top_p',
|
|
86
|
+
title: 'Top P',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (data.useUseTopPInput) {
|
|
90
|
+
inputs.push({
|
|
91
|
+
dataType: 'boolean',
|
|
92
|
+
id: 'useTopP',
|
|
93
|
+
title: 'Use Top P',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (data.useMaxTokensInput) {
|
|
97
|
+
inputs.push({
|
|
98
|
+
dataType: 'number',
|
|
99
|
+
id: 'maxTokens',
|
|
100
|
+
title: 'Max Tokens',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (data.useStopInput) {
|
|
104
|
+
inputs.push({
|
|
105
|
+
dataType: 'string',
|
|
106
|
+
id: 'stop',
|
|
107
|
+
title: 'Stop',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
inputs.push({
|
|
111
|
+
dataType: ['chat-message', 'chat-message[]'],
|
|
112
|
+
id: 'prompt',
|
|
113
|
+
title: 'Prompt',
|
|
114
|
+
});
|
|
115
|
+
if (data.enableToolUse) {
|
|
116
|
+
inputs.push({
|
|
117
|
+
dataType: ['gpt-function', 'gpt-function[]'],
|
|
118
|
+
id: 'tools',
|
|
119
|
+
title: 'Tools',
|
|
120
|
+
description: 'Tools to use in the model. To connect multiple tools, use an Array node.',
|
|
121
|
+
coerced: false,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (data.useHeadersInput) {
|
|
125
|
+
inputs.push({
|
|
126
|
+
dataType: 'object',
|
|
127
|
+
id: 'headers',
|
|
128
|
+
title: 'Headers',
|
|
129
|
+
description: 'Additional headers to send to the API.',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return inputs;
|
|
133
|
+
},
|
|
134
|
+
getOutputDefinitions(data) {
|
|
135
|
+
const outputs = [];
|
|
136
|
+
outputs.push({
|
|
137
|
+
dataType: 'string',
|
|
138
|
+
id: 'response',
|
|
139
|
+
title: 'Response',
|
|
140
|
+
});
|
|
141
|
+
if (data.enableCitations) {
|
|
142
|
+
outputs.push({
|
|
143
|
+
dataType: 'object[]',
|
|
144
|
+
id: 'citations',
|
|
145
|
+
title: 'Citations',
|
|
146
|
+
description: 'Citations from the response, if any.',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (data.enableToolUse) {
|
|
150
|
+
outputs.push({
|
|
151
|
+
dataType: 'object[]',
|
|
152
|
+
id: 'function-calls',
|
|
153
|
+
title: 'Tool Calls',
|
|
154
|
+
description: 'The tool calls that were made, if any.',
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
outputs.push({
|
|
158
|
+
dataType: 'chat-message[]',
|
|
159
|
+
id: 'all-messages',
|
|
160
|
+
title: 'All Messages',
|
|
161
|
+
description: 'All messages, with the response appended.',
|
|
162
|
+
});
|
|
163
|
+
return outputs;
|
|
164
|
+
},
|
|
165
|
+
getBody(data) {
|
|
166
|
+
const modelName = data.overrideModel
|
|
167
|
+
? data.overrideModel
|
|
168
|
+
: anthropicModels[data.model]?.displayName ?? 'Unknown Model';
|
|
169
|
+
return dedent `
|
|
170
|
+
${modelName}
|
|
171
|
+
${data.useTopP
|
|
172
|
+
? `Top P: ${data.useTopPInput ? '(Using Input)' : data.top_p}`
|
|
173
|
+
: `Temperature: ${data.useTemperatureInput ? '(Using Input)' : data.temperature}`}
|
|
174
|
+
Max Tokens: ${data.maxTokens}
|
|
175
|
+
${data.useStop ? `Stop: ${data.useStopInput ? '(Using Input)' : data.stop}` : ''}
|
|
176
|
+
`;
|
|
177
|
+
},
|
|
178
|
+
getEditors() {
|
|
179
|
+
return [
|
|
180
|
+
{
|
|
181
|
+
type: 'group',
|
|
182
|
+
label: 'Parameters',
|
|
183
|
+
defaultOpen: true,
|
|
184
|
+
editors: [
|
|
185
|
+
{
|
|
186
|
+
type: 'dropdown',
|
|
187
|
+
label: 'Model',
|
|
188
|
+
dataKey: 'model',
|
|
189
|
+
useInputToggleDataKey: 'useModelInput',
|
|
190
|
+
options: anthropicModelOptions,
|
|
191
|
+
disableIf: (d) => !!d.overrideModel?.trim(),
|
|
192
|
+
helperMessage: (d) => (!!d.overrideModel ? `Model is overridden to: ${d.overrideModel}` : ''),
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
type: 'number',
|
|
196
|
+
label: 'Temperature',
|
|
197
|
+
dataKey: 'temperature',
|
|
198
|
+
useInputToggleDataKey: 'useTemperatureInput',
|
|
199
|
+
min: 0,
|
|
200
|
+
max: 2,
|
|
201
|
+
step: 0.1,
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
type: 'number',
|
|
205
|
+
label: 'Top P',
|
|
206
|
+
dataKey: 'top_p',
|
|
207
|
+
useInputToggleDataKey: 'useTopPInput',
|
|
208
|
+
min: 0,
|
|
209
|
+
max: 1,
|
|
210
|
+
step: 0.1,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
type: 'toggle',
|
|
214
|
+
label: 'Use Top P',
|
|
215
|
+
dataKey: 'useTopP',
|
|
216
|
+
useInputToggleDataKey: 'useUseTopPInput',
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
type: 'number',
|
|
220
|
+
label: 'Max Tokens',
|
|
221
|
+
dataKey: 'maxTokens',
|
|
222
|
+
useInputToggleDataKey: 'useMaxTokensInput',
|
|
223
|
+
min: 0,
|
|
224
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
225
|
+
step: 1,
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
type: 'string',
|
|
229
|
+
label: 'Stop',
|
|
230
|
+
dataKey: 'stop',
|
|
231
|
+
useInputToggleDataKey: 'useStopInput',
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
type: 'group',
|
|
237
|
+
label: 'Tools',
|
|
238
|
+
editors: [
|
|
239
|
+
{
|
|
240
|
+
type: 'toggle',
|
|
241
|
+
label: 'Enable Tool Use (disables streaming)',
|
|
242
|
+
dataKey: 'enableToolUse',
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
type: 'toggle',
|
|
246
|
+
label: 'Enable Citations',
|
|
247
|
+
dataKey: 'enableCitations',
|
|
248
|
+
},
|
|
249
|
+
],
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
type: 'group',
|
|
253
|
+
label: 'Advanced',
|
|
254
|
+
editors: [
|
|
255
|
+
{
|
|
256
|
+
type: 'toggle',
|
|
257
|
+
label: 'Cache (same inputs, same outputs)',
|
|
258
|
+
dataKey: 'cache',
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
type: 'toggle',
|
|
262
|
+
label: 'Use for subgraph partial output',
|
|
263
|
+
dataKey: 'useAsGraphPartialOutput',
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
type: 'string',
|
|
267
|
+
label: 'Endpoint',
|
|
268
|
+
dataKey: 'endpoint',
|
|
269
|
+
useInputToggleDataKey: 'useEndpointInput',
|
|
270
|
+
helperMessage: 'Overrides the Anthropic API endpoint. Leave blank to use the default configured endpoint in settings, or https://api.anthropic.com/v1 if none is configured.',
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
type: 'string',
|
|
274
|
+
label: 'Override Model',
|
|
275
|
+
dataKey: 'overrideModel',
|
|
276
|
+
useInputToggleDataKey: 'useOverrideModelInput',
|
|
277
|
+
helperMessage: 'Overrides the AI model used for the chat node to this value.',
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
type: 'keyValuePair',
|
|
281
|
+
label: 'Headers',
|
|
282
|
+
dataKey: 'headers',
|
|
283
|
+
useInputToggleDataKey: 'useHeadersInput',
|
|
284
|
+
keyPlaceholder: 'Header',
|
|
285
|
+
helperMessage: 'Additional headers to send to the API.',
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
},
|
|
289
|
+
];
|
|
290
|
+
},
|
|
291
|
+
getUIData() {
|
|
292
|
+
return {
|
|
293
|
+
infoBoxBody: dedent `
|
|
294
|
+
Legacy Anthropic chat node.
|
|
295
|
+
|
|
296
|
+
For new work, prefer \`LLM Chat\`, which keeps provider selection inside one shared node.
|
|
297
|
+
`,
|
|
298
|
+
infoBoxTitle: 'Chat (Anthropic) Node (Legacy)',
|
|
299
|
+
contextMenuTitle: 'Chat (Anthropic, Legacy)',
|
|
300
|
+
group: ['AI'],
|
|
301
|
+
};
|
|
302
|
+
},
|
|
303
|
+
async process(data, inputs, context) {
|
|
304
|
+
const output = {};
|
|
305
|
+
const rawModel = getInputOrData(data, inputs, 'model');
|
|
306
|
+
const overrideModel = getInputOrData(data, inputs, 'overrideModel');
|
|
307
|
+
const model = (overrideModel || rawModel);
|
|
308
|
+
const temperature = data.useTemperatureInput
|
|
309
|
+
? coerceTypeOptional(inputs['temperature'], 'number') ?? data.temperature
|
|
310
|
+
: data.temperature;
|
|
311
|
+
const topP = data.useTopPInput ? coerceTypeOptional(inputs['top_p'], 'number') ?? data.top_p : data.top_p;
|
|
312
|
+
const useTopP = data.useUseTopPInput
|
|
313
|
+
? coerceTypeOptional(inputs['useTopP'], 'boolean') ?? data.useTopP
|
|
314
|
+
: data.useTopP;
|
|
315
|
+
const stop = data.useStopInput
|
|
316
|
+
? data.useStop
|
|
317
|
+
? coerceTypeOptional(inputs['stop'], 'string') ?? data.stop
|
|
318
|
+
: undefined
|
|
319
|
+
: data.stop;
|
|
320
|
+
const tools = data.enableToolUse
|
|
321
|
+
? coerceTypeOptional(inputs['tools'], 'gpt-function[]') ?? []
|
|
322
|
+
: undefined;
|
|
323
|
+
const rivetChatMessages = getChatMessages(inputs);
|
|
324
|
+
const messages = await chatMessagesToClaude3ChatMessages(rivetChatMessages);
|
|
325
|
+
let prompt = messages.reduce((acc, message) => {
|
|
326
|
+
const content = typeof message.content === 'string'
|
|
327
|
+
? message.content
|
|
328
|
+
: message.content
|
|
329
|
+
.filter((c) => c.type === 'text')
|
|
330
|
+
.map((c) => c.text ?? '')
|
|
331
|
+
.join('');
|
|
332
|
+
if (message.role === 'user') {
|
|
333
|
+
return `${acc}\n\nHuman: ${content}`;
|
|
334
|
+
}
|
|
335
|
+
else if (message.role === 'assistant') {
|
|
336
|
+
return `${acc}\n\nAssistant: ${content}`;
|
|
337
|
+
}
|
|
338
|
+
return acc;
|
|
339
|
+
}, '');
|
|
340
|
+
prompt += '\n\nAssistant:';
|
|
341
|
+
// Get the "System" prompt input for Claude 3 models
|
|
342
|
+
const system = data.model.startsWith('claude-3') ? getSystemPrompt(inputs) : undefined;
|
|
343
|
+
let { maxTokens } = data;
|
|
344
|
+
const tokenizerInfo = {
|
|
345
|
+
node: context.node,
|
|
346
|
+
model,
|
|
347
|
+
endpoint: undefined,
|
|
348
|
+
};
|
|
349
|
+
const tokenCountEstimate = await context.tokenizer.getTokenCountForString(prompt, tokenizerInfo);
|
|
350
|
+
const modelInfo = anthropicModels[model] ?? {
|
|
351
|
+
maxTokens: Number.MAX_SAFE_INTEGER,
|
|
352
|
+
cost: {
|
|
353
|
+
prompt: 0,
|
|
354
|
+
completion: 0,
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
maxTokens = clampMaxTokensToModelLimit(output, model, tokenCountEstimate, maxTokens, modelInfo.maxTokens);
|
|
358
|
+
const headersFromData = (data.headers ?? []).reduce((acc, header) => {
|
|
359
|
+
acc[header.key] = header.value;
|
|
360
|
+
return acc;
|
|
361
|
+
}, {});
|
|
362
|
+
const additionalHeaders = data.useHeadersInput
|
|
363
|
+
? coerceTypeOptional(inputs['headers'], 'object') ??
|
|
364
|
+
headersFromData
|
|
365
|
+
: headersFromData;
|
|
366
|
+
const allAdditionalHeaders = cleanHeaders({
|
|
367
|
+
...context.settings.chatNodeHeaders,
|
|
368
|
+
...additionalHeaders,
|
|
369
|
+
});
|
|
370
|
+
try {
|
|
371
|
+
return await retry(async () => {
|
|
372
|
+
const completionOptions = {
|
|
373
|
+
model,
|
|
374
|
+
temperature: useTopP ? undefined : temperature,
|
|
375
|
+
top_p: useTopP ? topP : undefined,
|
|
376
|
+
max_tokens_to_sample: maxTokens ?? modelInfo.maxTokens,
|
|
377
|
+
stop_sequences: stop ? [stop] : undefined,
|
|
378
|
+
prompt,
|
|
379
|
+
};
|
|
380
|
+
const messageOptions = {
|
|
381
|
+
model,
|
|
382
|
+
temperature: useTopP ? undefined : temperature,
|
|
383
|
+
top_p: useTopP ? topP : undefined,
|
|
384
|
+
max_tokens: maxTokens ?? modelInfo.maxTokens,
|
|
385
|
+
stop_sequences: stop ? [stop] : undefined,
|
|
386
|
+
system,
|
|
387
|
+
messages,
|
|
388
|
+
tools: tools
|
|
389
|
+
? tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: tool.parameters }))
|
|
390
|
+
: undefined,
|
|
391
|
+
};
|
|
392
|
+
const useMessageApi = model.startsWith('claude-3') || model.startsWith('claude-sonnet') || model.startsWith('claude-opus');
|
|
393
|
+
const cacheKey = JSON.stringify(useMessageApi ? messageOptions : completionOptions);
|
|
394
|
+
if (data.cache) {
|
|
395
|
+
const cached = cache.get(cacheKey);
|
|
396
|
+
if (cached) {
|
|
397
|
+
return cached;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const startTime = Date.now();
|
|
401
|
+
const apiKey = context.getPluginConfig('anthropicApiKey');
|
|
402
|
+
const defaultApiEndpoint = context.getPluginConfig('anthropicApiEndpoint') || 'https://api.anthropic.com/v1';
|
|
403
|
+
const configuredEndpoint = getInputOrData(data, inputs, 'endpoint');
|
|
404
|
+
const apiEndpoint = configuredEndpoint?.trim() ? configuredEndpoint : defaultApiEndpoint;
|
|
405
|
+
if (useMessageApi) {
|
|
406
|
+
// Use the messages API for Claude 3 models
|
|
407
|
+
const chunks = streamMessageApi({
|
|
408
|
+
apiEndpoint,
|
|
409
|
+
apiKey: apiKey ?? '',
|
|
410
|
+
signal: context.signal,
|
|
411
|
+
beta: 'prompt-caching-2024-07-31',
|
|
412
|
+
additionalHeaders: allAdditionalHeaders,
|
|
413
|
+
...messageOptions,
|
|
414
|
+
});
|
|
415
|
+
// Process the response chunks and update the output
|
|
416
|
+
const responseParts = [];
|
|
417
|
+
let requestTokens = undefined;
|
|
418
|
+
let responseTokens = undefined;
|
|
419
|
+
const citations = [];
|
|
420
|
+
// Track tool calls
|
|
421
|
+
const toolCalls = [];
|
|
422
|
+
let currentToolCall = null;
|
|
423
|
+
let accumulatedJsonString = '';
|
|
424
|
+
for await (const chunk of chunks) {
|
|
425
|
+
let completion = '';
|
|
426
|
+
if (chunk.type === 'content_block_start') {
|
|
427
|
+
if (chunk.content_block.type === 'text') {
|
|
428
|
+
completion = chunk.content_block.text || '';
|
|
429
|
+
}
|
|
430
|
+
else if (chunk.content_block.type === 'tool_use') {
|
|
431
|
+
currentToolCall = {
|
|
432
|
+
id: chunk.content_block.id,
|
|
433
|
+
name: chunk.content_block.name,
|
|
434
|
+
input: chunk.content_block.input || {},
|
|
435
|
+
};
|
|
436
|
+
accumulatedJsonString = '';
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else if (chunk.type === 'content_block_delta') {
|
|
440
|
+
if (chunk.delta.type === 'text_delta') {
|
|
441
|
+
completion = chunk.delta.text;
|
|
442
|
+
}
|
|
443
|
+
else if (chunk.delta.type === 'input_json_delta') {
|
|
444
|
+
if (currentToolCall) {
|
|
445
|
+
accumulatedJsonString += chunk.delta.partial_json || '';
|
|
446
|
+
try {
|
|
447
|
+
// Try to parse the accumulated JSON
|
|
448
|
+
const parsedJson = JSON.parse(accumulatedJsonString);
|
|
449
|
+
currentToolCall.input = parsedJson;
|
|
450
|
+
accumulatedJsonString = '';
|
|
451
|
+
}
|
|
452
|
+
catch (e) {
|
|
453
|
+
// Not valid JSON yet, keep accumulating
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
else if (chunk.delta.type === 'citations_delta') {
|
|
458
|
+
citations.push(chunk.delta.citation);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
else if (chunk.type === 'content_block_stop') {
|
|
462
|
+
if (currentToolCall) {
|
|
463
|
+
if (accumulatedJsonString) {
|
|
464
|
+
try {
|
|
465
|
+
const parsedJson = JSON.parse(accumulatedJsonString);
|
|
466
|
+
currentToolCall.input = parsedJson;
|
|
467
|
+
}
|
|
468
|
+
catch (e) {
|
|
469
|
+
logRuntimeDebug('Failed to parse Anthropic tool call JSON input', {
|
|
470
|
+
accumulatedJsonLength: accumulatedJsonString.length,
|
|
471
|
+
error: summarizeErrorForLog(e),
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
toolCalls.push({ ...currentToolCall });
|
|
476
|
+
currentToolCall = null;
|
|
477
|
+
accumulatedJsonString = '';
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (chunk.type === 'message_start' && chunk.message?.usage?.input_tokens) {
|
|
481
|
+
requestTokens = chunk.message.usage.input_tokens;
|
|
482
|
+
}
|
|
483
|
+
else if (chunk.type === 'message_delta' && chunk.delta?.usage?.output_tokens) {
|
|
484
|
+
responseTokens = chunk.delta.usage.output_tokens;
|
|
485
|
+
}
|
|
486
|
+
if (completion) {
|
|
487
|
+
responseParts.push(completion);
|
|
488
|
+
}
|
|
489
|
+
output['response'] = {
|
|
490
|
+
type: 'string',
|
|
491
|
+
value: responseParts.join('').trim(),
|
|
492
|
+
};
|
|
493
|
+
if (toolCalls.length > 0) {
|
|
494
|
+
output['function-calls'] = {
|
|
495
|
+
type: 'object[]',
|
|
496
|
+
value: toolCalls.map((tool) => ({
|
|
497
|
+
id: tool.id,
|
|
498
|
+
name: tool.name,
|
|
499
|
+
arguments: tool.input,
|
|
500
|
+
})),
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
output['function-calls'] = {
|
|
505
|
+
type: 'control-flow-excluded',
|
|
506
|
+
value: undefined,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
output['citations'] = {
|
|
510
|
+
type: 'object[]',
|
|
511
|
+
value: citations,
|
|
512
|
+
};
|
|
513
|
+
// Format function calls for the ChatMessage interface
|
|
514
|
+
const functionCalls = toolCalls.map((tool) => ({
|
|
515
|
+
name: tool.name,
|
|
516
|
+
arguments: typeof tool.input === 'object' ? JSON.stringify(tool.input) : tool.input,
|
|
517
|
+
id: tool.id,
|
|
518
|
+
}));
|
|
519
|
+
output['all-messages'] = createAssistantMessagesOutput(rivetChatMessages, responseParts.join('').trim(), functionCalls.length > 0
|
|
520
|
+
? functionCalls.map((call) => ({
|
|
521
|
+
type: 'function',
|
|
522
|
+
id: call.id,
|
|
523
|
+
name: call.name,
|
|
524
|
+
arguments: typeof call.arguments === 'string' ? call.arguments : JSON.stringify(call.arguments),
|
|
525
|
+
}))
|
|
526
|
+
: undefined, { functionCallMode: 'only' });
|
|
527
|
+
context.onPartialOutputs?.(output);
|
|
528
|
+
}
|
|
529
|
+
// Final validation
|
|
530
|
+
if (responseParts.length === 0 && toolCalls.length === 0) {
|
|
531
|
+
throw new Error('No response or tool calls received from Anthropic');
|
|
532
|
+
}
|
|
533
|
+
const responseTokenCount = responseTokens ?? (await context.tokenizer.getTokenCountForString(responseParts.join(''), tokenizerInfo));
|
|
534
|
+
setRequestAndResponseTokenOutputs(output, requestTokens ?? tokenCountEstimate, responseTokenCount);
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
// Use the normal chat completion method for non-Claude 3 models
|
|
538
|
+
const chunks = streamChatCompletions({
|
|
539
|
+
apiEndpoint,
|
|
540
|
+
apiKey: apiKey ?? '',
|
|
541
|
+
signal: context.signal,
|
|
542
|
+
additionalHeaders: allAdditionalHeaders,
|
|
543
|
+
...completionOptions,
|
|
544
|
+
});
|
|
545
|
+
// Process the response chunks and update the output
|
|
546
|
+
const responseParts = [];
|
|
547
|
+
for await (const chunk of chunks) {
|
|
548
|
+
if (!chunk.completion) {
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
responseParts.push(chunk.completion);
|
|
552
|
+
output['response'] = {
|
|
553
|
+
type: 'string',
|
|
554
|
+
value: responseParts.join('').trim(),
|
|
555
|
+
};
|
|
556
|
+
context.onPartialOutputs?.(output);
|
|
557
|
+
}
|
|
558
|
+
if (responseParts.length === 0) {
|
|
559
|
+
throw new Error('No response from Anthropic');
|
|
560
|
+
}
|
|
561
|
+
output['all-messages'] = createAssistantMessagesOutput(rivetChatMessages, responseParts.join('').trim(), undefined, { functionCallMode: 'only' });
|
|
562
|
+
const responseTokenCount = await context.tokenizer.getTokenCountForString(responseParts.join(''), tokenizerInfo);
|
|
563
|
+
setRequestAndResponseTokenOutputs(output, tokenCountEstimate, responseTokenCount);
|
|
564
|
+
}
|
|
565
|
+
const cost = getCostForTokens({
|
|
566
|
+
requestTokens: output['requestTokens']?.value,
|
|
567
|
+
responseTokens: output['responseTokens']?.value,
|
|
568
|
+
}, model);
|
|
569
|
+
if (cost != null) {
|
|
570
|
+
output['cost'] = { type: 'number', value: cost };
|
|
571
|
+
}
|
|
572
|
+
const endTime = Date.now();
|
|
573
|
+
const duration = endTime - startTime;
|
|
574
|
+
output['duration'] = { type: 'number', value: duration };
|
|
575
|
+
Object.freeze(output);
|
|
576
|
+
cache.set(cacheKey, output);
|
|
577
|
+
return output;
|
|
578
|
+
}, {
|
|
579
|
+
forever: true,
|
|
580
|
+
retries: 10000,
|
|
581
|
+
maxRetryTime: 1000 * 60 * 5,
|
|
582
|
+
factor: 2.5,
|
|
583
|
+
minTimeout: 500,
|
|
584
|
+
maxTimeout: 5000,
|
|
585
|
+
randomize: true,
|
|
586
|
+
signal: context.signal,
|
|
587
|
+
onFailedAttempt(err) {
|
|
588
|
+
context.trace(`ChatAnthropicNode failed, retrying: ${err.toString()}`);
|
|
589
|
+
if (context.signal.aborted) {
|
|
590
|
+
throw new Error('Aborted');
|
|
591
|
+
}
|
|
592
|
+
if (err instanceof AnthropicError) {
|
|
593
|
+
if (err.response.status >= 400 && err.response.status < 500) {
|
|
594
|
+
const errorMessage = typeof err.responseJson === 'object' &&
|
|
595
|
+
err.responseJson != null &&
|
|
596
|
+
'error' in err.responseJson &&
|
|
597
|
+
typeof err.responseJson.error === 'object' &&
|
|
598
|
+
err.responseJson.error != null &&
|
|
599
|
+
'message' in err.responseJson.error &&
|
|
600
|
+
typeof err.responseJson.error.message === 'string'
|
|
601
|
+
? err.responseJson.error.message
|
|
602
|
+
: undefined;
|
|
603
|
+
if (errorMessage) {
|
|
604
|
+
throw new Error(errorMessage);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
catch (error) {
|
|
612
|
+
context.trace(getError(error).stack ?? 'Missing stack');
|
|
613
|
+
throw new Error(`Error processing ChatAnthropicNode: ${error.message}`);
|
|
614
|
+
}
|
|
615
|
+
},
|
|
616
|
+
};
|
|
617
|
+
export const chatAnthropicNode = pluginNodeDefinition(ChatAnthropicNodeImpl, 'Chat (Anthropic, Legacy)');
|
|
618
|
+
export function getSystemPrompt(inputs) {
|
|
619
|
+
const systemInput = inputs['system'];
|
|
620
|
+
const system = coerceTypeOptional(systemInput, 'string');
|
|
621
|
+
if (system) {
|
|
622
|
+
return [
|
|
623
|
+
{
|
|
624
|
+
type: 'text',
|
|
625
|
+
text: system,
|
|
626
|
+
cache_control: systemInput?.type === 'chat-message'
|
|
627
|
+
? systemInput.value.isCacheBreakpoint
|
|
628
|
+
? { type: 'ephemeral' }
|
|
629
|
+
: null
|
|
630
|
+
: null,
|
|
631
|
+
},
|
|
632
|
+
];
|
|
633
|
+
}
|
|
634
|
+
const prompt = inputs['prompt'];
|
|
635
|
+
if (prompt && prompt.type === 'chat-message[]') {
|
|
636
|
+
const systemMessages = prompt.value.filter((message) => message.type === 'system');
|
|
637
|
+
if (systemMessages.length) {
|
|
638
|
+
const converted = systemMessages.map((message) => {
|
|
639
|
+
return {
|
|
640
|
+
type: 'text',
|
|
641
|
+
text: coerceType({ type: 'chat-message', value: message }, 'string'),
|
|
642
|
+
cache_control: message.isCacheBreakpoint ? { type: 'ephemeral' } : null,
|
|
643
|
+
};
|
|
644
|
+
});
|
|
645
|
+
return converted;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return undefined;
|
|
649
|
+
}
|
|
650
|
+
function getChatMessages(inputs) {
|
|
651
|
+
const prompt = inputs['prompt'];
|
|
652
|
+
return coercePromptToChatMessages(prompt, { requirePrompt: true });
|
|
653
|
+
}
|
|
654
|
+
export async function chatMessagesToClaude3ChatMessages(chatMessages) {
|
|
655
|
+
const messages = (await Promise.all(chatMessages.map(chatMessageToClaude3ChatMessage))).filter(isNotNull);
|
|
656
|
+
// Combine sequential tool_result messages into a single user message with multiple content items
|
|
657
|
+
const combinedMessages = messages.reduce((acc, message) => {
|
|
658
|
+
if (message.role === 'user' &&
|
|
659
|
+
Array.isArray(message.content) &&
|
|
660
|
+
message.content.length === 1 &&
|
|
661
|
+
message.content[0].type === 'tool_result') {
|
|
662
|
+
const last = acc.at(-1);
|
|
663
|
+
if (last?.role === 'user' && Array.isArray(last.content) && last.content.every((c) => c.type === 'tool_result')) {
|
|
664
|
+
const content = last.content.concat(message.content);
|
|
665
|
+
return [...acc.slice(0, -1), { ...last, content }];
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return [...acc, message];
|
|
669
|
+
}, []);
|
|
670
|
+
return combinedMessages;
|
|
671
|
+
}
|
|
672
|
+
async function chatMessageToClaude3ChatMessage(message) {
|
|
673
|
+
if (message.type === 'system') {
|
|
674
|
+
return undefined;
|
|
675
|
+
}
|
|
676
|
+
if (message.type === 'function') {
|
|
677
|
+
// Interpret function messages as user messages with tool_result content items (making Claude API more similar to OpenAI's)
|
|
678
|
+
const content = (Array.isArray(message.message) ? message.message : [message.message])
|
|
679
|
+
.map((m) => (typeof m === 'string' ? { type: 'text', text: m } : undefined))
|
|
680
|
+
.filter(isNotNull);
|
|
681
|
+
return {
|
|
682
|
+
role: 'user',
|
|
683
|
+
content: [
|
|
684
|
+
{
|
|
685
|
+
type: 'tool_result',
|
|
686
|
+
tool_use_id: message.name,
|
|
687
|
+
content: content.length === 1 ? content[0].text : content,
|
|
688
|
+
cache_control: message.isCacheBreakpoint ? { type: 'ephemeral' } : null,
|
|
689
|
+
},
|
|
690
|
+
],
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
const content = Array.isArray(message.message)
|
|
694
|
+
? await Promise.all(message.message.map((part) => chatMessageContentToClaude3ChatMessage(part)))
|
|
695
|
+
: [await chatMessageContentToClaude3ChatMessage(message.message)];
|
|
696
|
+
if (message.type === 'assistant' && message.function_calls) {
|
|
697
|
+
content.push(...message.function_calls.map((fc) => ({
|
|
698
|
+
type: 'tool_use',
|
|
699
|
+
id: fc.id,
|
|
700
|
+
name: fc.name,
|
|
701
|
+
input: JSON.parse(fc.arguments),
|
|
702
|
+
cache_control: message.isCacheBreakpoint ? { type: 'ephemeral' } : null,
|
|
703
|
+
})));
|
|
704
|
+
}
|
|
705
|
+
else if (message.type === 'assistant' && message.function_call) {
|
|
706
|
+
content.push({
|
|
707
|
+
type: 'tool_use',
|
|
708
|
+
id: message.function_call.id,
|
|
709
|
+
name: message.function_call.name,
|
|
710
|
+
input: JSON.parse(message.function_call.arguments),
|
|
711
|
+
cache_control: message.isCacheBreakpoint ? { type: 'ephemeral' } : null,
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
// If the message is a cache breakpoint, cache using the last content item of the message
|
|
715
|
+
if (message.isCacheBreakpoint) {
|
|
716
|
+
content.at(-1).cache_control = { type: 'ephemeral' };
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
role: message.type,
|
|
720
|
+
content,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
async function chatMessageContentToClaude3ChatMessage(content) {
|
|
724
|
+
if (typeof content === 'string') {
|
|
725
|
+
return {
|
|
726
|
+
type: 'text',
|
|
727
|
+
text: content,
|
|
728
|
+
cache_control: null, // set later
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
switch (content.type) {
|
|
732
|
+
case 'image':
|
|
733
|
+
return {
|
|
734
|
+
type: 'image',
|
|
735
|
+
source: {
|
|
736
|
+
type: 'base64',
|
|
737
|
+
media_type: content.mediaType,
|
|
738
|
+
data: (await uint8ArrayToBase64(content.data)) ?? '',
|
|
739
|
+
},
|
|
740
|
+
cache_control: null, // set later
|
|
741
|
+
};
|
|
742
|
+
case 'url':
|
|
743
|
+
throw new Error('unable to convert urls for Claude');
|
|
744
|
+
case 'document':
|
|
745
|
+
return {
|
|
746
|
+
type: 'document',
|
|
747
|
+
source: {
|
|
748
|
+
type: 'base64',
|
|
749
|
+
data: (await uint8ArrayToBase64(content.data)) ?? '',
|
|
750
|
+
media_type: content.mediaType,
|
|
751
|
+
},
|
|
752
|
+
title: content.title?.trim() ? content.title.trim() : undefined,
|
|
753
|
+
context: content.context?.trim() ? content.context.trim() : undefined,
|
|
754
|
+
citations: content.enableCitations ? { enabled: true } : undefined,
|
|
755
|
+
cache_control: null, // set later
|
|
756
|
+
};
|
|
757
|
+
default:
|
|
758
|
+
assertNever(content);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
function getCostForTokens(tokenCounts, model) {
|
|
762
|
+
const modelInfo = anthropicModels[model];
|
|
763
|
+
if (modelInfo == null) {
|
|
764
|
+
return undefined;
|
|
765
|
+
}
|
|
766
|
+
return modelInfo.cost.prompt * tokenCounts.requestTokens + modelInfo.cost.completion * tokenCounts.responseTokens;
|
|
767
|
+
}
|