@valerypopoff/rivet2-core 2.0.2 → 2.0.3
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/dist/cjs/bundle.cjs +576 -325
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/model/GraphProcessor.js +50 -22
- package/dist/esm/model/NodeExecutionPlanner.js +3 -3
- package/dist/esm/model/Nodes.js +3 -0
- package/dist/esm/model/chat/aiSdkStreaming.js +23 -2
- package/dist/esm/model/chat-v2/aiSdkBridge.js +49 -2
- package/dist/esm/model/chat-v2/chatV2FeatureCompatibility.js +2 -1
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +24 -4
- package/dist/esm/model/chat-v2/chatV2ResponseFormat.js +3 -0
- package/dist/esm/model/chat-v2/llmChatV2NodeRuntime.js +1 -0
- package/dist/esm/model/nodes/CodeNode.js +8 -8
- package/dist/esm/model/nodes/DidRunNode.js +85 -0
- package/dist/esm/model/nodes/ExtractJsonNode.js +44 -26
- package/dist/esm/model/nodes/LLMChatV2Node.js +16 -0
- package/dist/types/model/NodeExecutionPlanner.d.ts +1 -1
- package/dist/types/model/Nodes.d.ts +3 -2
- package/dist/types/model/chat/aiSdkStreaming.d.ts +4 -1
- package/dist/types/model/chat-v2/chatV2FeatureCompatibility.d.ts +2 -2
- package/dist/types/model/chat-v2/chatV2ResponseFormat.d.ts +2 -0
- package/dist/types/model/chat-v2/chatV2Types.d.ts +5 -0
- package/dist/types/model/nodes/DidRunNode.d.ts +15 -0
- package/package.json +1 -1
|
@@ -13,7 +13,7 @@ import { replayExecutionRecording } from './RecordingPlayer.js';
|
|
|
13
13
|
import { didLoopControllerBreak, LOOP_NOT_BROKEN_SENTINEL } from './loopControllerBreak.js';
|
|
14
14
|
import { buildNodeProcessContext } from './ProcessContextBuilder.js';
|
|
15
15
|
import { processSplitRunNode } from './SplitRunProcessor.js';
|
|
16
|
-
import {
|
|
16
|
+
import { getInputNodesTo, getMissingRequiredInputs, getOutputNodesFrom, getStartNodes, getWaitingForInputNode, hasErroredInputNode, } from './NodeExecutionPlanner.js';
|
|
17
17
|
import { wireSubprocessorEvents, wireSubprocessorLifecycle } from './SubprocessorBridge.js';
|
|
18
18
|
import { emitDetached } from '../utils/emitDetached.js';
|
|
19
19
|
// eslint-disable-next-line import/no-cycle -- There has to be a cycle because CodeRunner needs to import the entirety of Rivet
|
|
@@ -551,9 +551,9 @@ export class GraphProcessor {
|
|
|
551
551
|
#hasErroredInputNode(node, inputNodes, trace = false) {
|
|
552
552
|
return hasErroredInputNode(this.#executionState, node, inputNodes, trace ? (message) => this.#emitTraceEvent(message) : undefined);
|
|
553
553
|
}
|
|
554
|
-
/** Returns
|
|
555
|
-
#
|
|
556
|
-
return
|
|
554
|
+
/** Returns required inputs without connections. */
|
|
555
|
+
#getMissingRequiredInputs(node) {
|
|
556
|
+
return getMissingRequiredInputs(this.#executionState, node);
|
|
557
557
|
}
|
|
558
558
|
/** Accumulates cost from a node's output. */
|
|
559
559
|
#accumulateCost(output) {
|
|
@@ -576,10 +576,7 @@ export class GraphProcessor {
|
|
|
576
576
|
if (this.#hasErroredInputNode(node, inputNodes)) {
|
|
577
577
|
return;
|
|
578
578
|
}
|
|
579
|
-
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
this.#emitTraceEvent(`Node ${node.title} has required inputs nodes: ${inputNodes.map((n) => n.title).join(', ')}`);
|
|
579
|
+
this.#emitTraceEvent(`Node ${node.title} has input nodes: ${inputNodes.map((n) => n.title).join(', ')}`);
|
|
583
580
|
const attachedData = this.#getAttachedDataTo(node);
|
|
584
581
|
if (node.type === 'raceInputs' || attachedData.races) {
|
|
585
582
|
for (const inputNode of inputNodes) {
|
|
@@ -611,6 +608,10 @@ export class GraphProcessor {
|
|
|
611
608
|
if (this.#shouldSkipNodeProcessing(node, inputNodes)) {
|
|
612
609
|
return;
|
|
613
610
|
}
|
|
611
|
+
const attachedData = this.#getAttachedDataTo(node);
|
|
612
|
+
if (this.#shouldSkipCompletedRaceNode(node, attachedData)) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
614
615
|
const inputValues = this.#getInputValuesForNode(node);
|
|
615
616
|
if (this.#excludedDueToControlFlow(node, inputValues, nanoid(), LOOP_NOT_BROKEN_SENTINEL)) {
|
|
616
617
|
this.#emitTraceEvent(`Node ${node.title} is excluded due to control flow`);
|
|
@@ -621,7 +622,15 @@ export class GraphProcessor {
|
|
|
621
622
|
this.#emitTraceEvent(`Node ${node.title} is waiting for input node ${waitingForInputNode}`);
|
|
622
623
|
return;
|
|
623
624
|
}
|
|
624
|
-
|
|
625
|
+
if (this.#excludedDueToControlFlow(node, inputValues, nanoid())) {
|
|
626
|
+
this.#emitTraceEvent(`Node ${node.title} is excluded due to control flow`);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const missingRequiredInputs = this.#getMissingRequiredInputs(node);
|
|
630
|
+
if (missingRequiredInputs.length > 0) {
|
|
631
|
+
this.#excludeNodeWithMissingRequiredInputs(node, inputValues, missingRequiredInputs);
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
625
634
|
if (this.#beginNodeProcessing(node, attachedData) === false) {
|
|
626
635
|
return;
|
|
627
636
|
}
|
|
@@ -669,25 +678,36 @@ export class GraphProcessor {
|
|
|
669
678
|
if (this.#hasErroredInputNode(node, inputNodes, true)) {
|
|
670
679
|
return true;
|
|
671
680
|
}
|
|
672
|
-
if (!this.#areRequiredInputsConnected(node)) {
|
|
673
|
-
this.#emitTraceEvent(`Node ${node.title} has required inputs nodes: ${inputNodes.map((n) => n.title).join(', ')}`);
|
|
674
|
-
return true;
|
|
675
|
-
}
|
|
676
681
|
return false;
|
|
677
682
|
}
|
|
683
|
+
#excludeNodeWithMissingRequiredInputs(node, inputValues, missingRequiredInputs) {
|
|
684
|
+
const missingInputNames = missingRequiredInputs.map((input) => input.title || input.id).join(', ');
|
|
685
|
+
const processId = nanoid();
|
|
686
|
+
this.#emitTraceEvent(`Excluding node ${node.title} because required inputs are not connected: ${missingInputNames}`);
|
|
687
|
+
this.#excludeNode(node, processId, inputValues, 'missing required input');
|
|
688
|
+
}
|
|
678
689
|
#beginNodeProcessing(node, attachedData) {
|
|
679
|
-
if (attachedData
|
|
680
|
-
this.#emitTraceEvent(`Node ${node.title} is part of a race that was completed`);
|
|
690
|
+
if (this.#shouldSkipCompletedRaceNode(node, attachedData)) {
|
|
681
691
|
return false;
|
|
682
692
|
}
|
|
683
693
|
this.#currentlyProcessing.add(node.id);
|
|
684
694
|
if (node.type === 'loopController') {
|
|
685
695
|
this.#loopControllersSeen.add(node.id);
|
|
686
696
|
}
|
|
697
|
+
this.#registerNodeInActiveLoop(node, attachedData);
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
#shouldSkipCompletedRaceNode(node, attachedData) {
|
|
701
|
+
if (attachedData.races?.completed) {
|
|
702
|
+
this.#emitTraceEvent(`Node ${node.title} is part of a race that was completed`);
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
707
|
+
#registerNodeInActiveLoop(node, attachedData) {
|
|
687
708
|
if (attachedData.loopInfo && attachedData.loopInfo.loopControllerId !== node.id) {
|
|
688
709
|
attachedData.loopInfo.nodes.add(node.id);
|
|
689
710
|
}
|
|
690
|
-
return true;
|
|
691
711
|
}
|
|
692
712
|
#handleLoopControllerPostProcess(node, attachedData) {
|
|
693
713
|
if (node.type !== 'loopController') {
|
|
@@ -1050,16 +1070,14 @@ export class GraphProcessor {
|
|
|
1050
1070
|
#excludedDueToControlFlow(node, inputValues, processId, typeOfExclusion = undefined) {
|
|
1051
1071
|
if (node.disabled) {
|
|
1052
1072
|
this.#emitTraceEvent(`Excluding node ${node.title} because it's disabled`);
|
|
1053
|
-
this.#
|
|
1054
|
-
this.#markAsExcluded(node, processId, inputValues, 'disabled');
|
|
1073
|
+
this.#excludeNode(node, processId, inputValues, 'disabled');
|
|
1055
1074
|
return true;
|
|
1056
1075
|
}
|
|
1057
1076
|
if (node.isConditional && typeOfExclusion === undefined) {
|
|
1058
1077
|
const ifValue = coerceTypeOptional(inputValues[IF_PORT.id], 'boolean');
|
|
1059
1078
|
if (ifValue === false) {
|
|
1060
1079
|
this.#emitTraceEvent(`Excluding node ${node.title} because if port is false`);
|
|
1061
|
-
this.#
|
|
1062
|
-
this.#markAsExcluded(node, processId, inputValues, 'if port is false');
|
|
1080
|
+
this.#excludeNode(node, processId, inputValues, 'if port is false');
|
|
1063
1081
|
return true;
|
|
1064
1082
|
}
|
|
1065
1083
|
}
|
|
@@ -1081,13 +1099,23 @@ export class GraphProcessor {
|
|
|
1081
1099
|
if (inputIsExcludedValue) {
|
|
1082
1100
|
this.#emitTraceEvent(`Excluding node ${node.title} because of control flow. Input is has excluded value: ${controlFlowExcludedValues[0]?.[0]}`);
|
|
1083
1101
|
}
|
|
1084
|
-
this.#
|
|
1085
|
-
this.#markAsExcluded(node, processId, inputValues, 'input is excluded value');
|
|
1102
|
+
this.#excludeNode(node, processId, inputValues, 'input is excluded value');
|
|
1086
1103
|
}
|
|
1087
1104
|
return true;
|
|
1088
1105
|
}
|
|
1089
1106
|
return false;
|
|
1090
1107
|
}
|
|
1108
|
+
#excludeNode(node, processId, inputValues, reason) {
|
|
1109
|
+
const attachedData = this.#getAttachedDataTo(node);
|
|
1110
|
+
this.#registerNodeInActiveLoop(node, attachedData);
|
|
1111
|
+
this.#visitedNodes.add(node.id);
|
|
1112
|
+
this.#markAsExcluded(node, processId, inputValues, reason);
|
|
1113
|
+
this.#currentlyProcessing.delete(node.id);
|
|
1114
|
+
this.#remainingNodes.delete(node.id);
|
|
1115
|
+
const outputNodes = getOutputNodesFrom(this.#executionState, node);
|
|
1116
|
+
this.#propagateAttachedDataToOutputNodes(node, attachedData, outputNodes.connectionsToNodes);
|
|
1117
|
+
this.#queueOutputNodes(node, outputNodes.nodes);
|
|
1118
|
+
}
|
|
1091
1119
|
#markAsExcluded(node, processId, inputValues, reason) {
|
|
1092
1120
|
const outputs = {};
|
|
1093
1121
|
for (const output of this.#definitions[node.id].outputs) {
|
|
@@ -48,11 +48,11 @@ export function hasErroredInputNode(state, node, inputNodes, onTrace) {
|
|
|
48
48
|
}
|
|
49
49
|
return false;
|
|
50
50
|
}
|
|
51
|
-
export function
|
|
51
|
+
export function getMissingRequiredInputs(state, node) {
|
|
52
52
|
const connections = state.connections[node.id] ?? [];
|
|
53
|
-
return state.definitions[node.id].inputs.
|
|
53
|
+
return state.definitions[node.id].inputs.filter((input) => {
|
|
54
54
|
const connectionToInput = connections.find((conn) => conn.inputId === input.id && conn.inputNodeId === node.id);
|
|
55
|
-
return
|
|
55
|
+
return input.required && !connectionToInput;
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
export function getWaitingForInputNode(state, node, inputNodes, inputValues) {
|
package/dist/esm/model/Nodes.js
CHANGED
|
@@ -56,6 +56,8 @@ import { contextNode } from './nodes/ContextNode.js';
|
|
|
56
56
|
export * from './nodes/ContextNode.js';
|
|
57
57
|
import { coalesceNode } from './nodes/CoalesceNode.js';
|
|
58
58
|
export * from './nodes/CoalesceNode.js';
|
|
59
|
+
import { didRunNode } from './nodes/DidRunNode.js';
|
|
60
|
+
export * from './nodes/DidRunNode.js';
|
|
59
61
|
import { passthroughNode } from './nodes/PassthroughNode.js';
|
|
60
62
|
export * from './nodes/PassthroughNode.js';
|
|
61
63
|
import { popNode } from './nodes/PopNode.js';
|
|
@@ -205,6 +207,7 @@ export const registerBuiltInNodes = (registry) => {
|
|
|
205
207
|
.register(raiseEventNode)
|
|
206
208
|
.register(contextNode)
|
|
207
209
|
.register(coalesceNode)
|
|
210
|
+
.register(didRunNode)
|
|
208
211
|
.register(passthroughNode)
|
|
209
212
|
.register(popNode)
|
|
210
213
|
.register(setGlobalNode)
|
|
@@ -1,12 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
function buildResponseText(textBlocks, dedupeDuplicateTextBlocks) {
|
|
2
|
+
const blockTexts = Array.from(textBlocks.values()).filter((text) => text.length > 0);
|
|
3
|
+
if (blockTexts.length === 0) {
|
|
4
|
+
return '';
|
|
5
|
+
}
|
|
6
|
+
if (dedupeDuplicateTextBlocks && blockTexts.length > 1) {
|
|
7
|
+
const longestText = blockTexts.reduce((longest, text) => (text.length > longest.length ? text : longest));
|
|
8
|
+
if (blockTexts.every((text) => longestText.startsWith(text))) {
|
|
9
|
+
return longestText;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return blockTexts.join('');
|
|
13
|
+
}
|
|
14
|
+
export async function consumeAiSdkStream(fullStream, onPartialOutputs, options = {}) {
|
|
2
15
|
let responseText = '';
|
|
3
16
|
let reasoning = '';
|
|
4
17
|
const functionCalls = [];
|
|
5
18
|
let usage;
|
|
19
|
+
const textBlocks = new Map();
|
|
6
20
|
for await (const part of fullStream) {
|
|
7
21
|
switch (part.type) {
|
|
22
|
+
case 'text-start': {
|
|
23
|
+
if (!textBlocks.has(part.id)) {
|
|
24
|
+
textBlocks.set(part.id, '');
|
|
25
|
+
}
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
8
28
|
case 'text-delta': {
|
|
9
|
-
|
|
29
|
+
textBlocks.set(part.id, `${textBlocks.get(part.id) ?? ''}${part.text}`);
|
|
30
|
+
responseText = buildResponseText(textBlocks, !!options.dedupeDuplicateTextBlocks);
|
|
10
31
|
onPartialOutputs(responseText, functionCalls);
|
|
11
32
|
break;
|
|
12
33
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { streamText } from 'ai';
|
|
2
2
|
import { consumeAiSdkStream } from '../chat/aiSdkStreaming.js';
|
|
3
|
+
import { isChatV2StructuredResponseFormat } from './chatV2ResponseFormat.js';
|
|
3
4
|
function keepPromiseHandled(value) {
|
|
4
5
|
const promise = Promise.resolve(value);
|
|
5
6
|
void promise.catch(() => undefined);
|
|
@@ -22,6 +23,9 @@ function defaultStreamExecutor(args) {
|
|
|
22
23
|
finishReason: 'finishReason' in result
|
|
23
24
|
? keepPromiseHandled(Promise.resolve(result.finishReason).then((value) => (value == null ? undefined : String(value))))
|
|
24
25
|
: undefined,
|
|
26
|
+
output: args.output != null && 'output' in result
|
|
27
|
+
? keepPromiseHandled(Promise.resolve(result.output))
|
|
28
|
+
: undefined,
|
|
25
29
|
providerMetadata: 'providerMetadata' in result
|
|
26
30
|
? keepPromiseHandled(Promise.resolve(result.providerMetadata))
|
|
27
31
|
: undefined,
|
|
@@ -34,6 +38,38 @@ function defaultStreamExecutor(args) {
|
|
|
34
38
|
async function resolveOptionalValue(value) {
|
|
35
39
|
return value == null ? undefined : await value;
|
|
36
40
|
}
|
|
41
|
+
async function resolveOptionalStructuredOutput(value) {
|
|
42
|
+
if (value == null) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
return await value;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function collapseRepeatedStructuredJsonText(responseText) {
|
|
53
|
+
if (responseText.length === 0 || responseText.length % 2 !== 0) {
|
|
54
|
+
return responseText;
|
|
55
|
+
}
|
|
56
|
+
const midpoint = responseText.length / 2;
|
|
57
|
+
const firstHalf = responseText.slice(0, midpoint);
|
|
58
|
+
const firstNonWhitespaceCharacter = firstHalf.trimStart()[0];
|
|
59
|
+
if (firstHalf !== responseText.slice(midpoint)) {
|
|
60
|
+
return responseText;
|
|
61
|
+
}
|
|
62
|
+
if (firstNonWhitespaceCharacter !== '{' && firstNonWhitespaceCharacter !== '[') {
|
|
63
|
+
return responseText;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
JSON.parse(firstHalf);
|
|
67
|
+
return firstHalf;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return responseText;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
37
73
|
async function executeStream(options, executor) {
|
|
38
74
|
const args = {
|
|
39
75
|
model: options.model,
|
|
@@ -67,14 +103,25 @@ async function executeStream(options, executor) {
|
|
|
67
103
|
args.abortSignal = options.abortSignal;
|
|
68
104
|
const handle = await executor(args);
|
|
69
105
|
markOptionalPromiseHandled(handle.finishReason);
|
|
106
|
+
markOptionalPromiseHandled(handle.output);
|
|
70
107
|
markOptionalPromiseHandled(handle.providerMetadata);
|
|
71
108
|
markOptionalPromiseHandled(handle.requestStatus);
|
|
72
109
|
markOptionalPromiseHandled(handle.usage);
|
|
110
|
+
const isStructuredOutput = isChatV2StructuredResponseFormat(options.responseFormat);
|
|
73
111
|
const streamed = await consumeAiSdkStream(handle.fullStream, (text, functionCalls) => {
|
|
74
|
-
options.onPartialOutput?.({
|
|
112
|
+
options.onPartialOutput?.({
|
|
113
|
+
text: isStructuredOutput ? collapseRepeatedStructuredJsonText(text) : text,
|
|
114
|
+
functionCalls,
|
|
115
|
+
});
|
|
116
|
+
}, {
|
|
117
|
+
dedupeDuplicateTextBlocks: isStructuredOutput,
|
|
75
118
|
});
|
|
119
|
+
const responseText = isStructuredOutput
|
|
120
|
+
? collapseRepeatedStructuredJsonText(streamed.responseText)
|
|
121
|
+
: streamed.responseText;
|
|
76
122
|
return {
|
|
77
|
-
responseText
|
|
123
|
+
responseText,
|
|
124
|
+
structuredOutput: options.responseOutput != null ? await resolveOptionalStructuredOutput(handle.output) : undefined,
|
|
78
125
|
functionCalls: streamed.functionCalls,
|
|
79
126
|
usage: streamed.usage ?? (await resolveOptionalValue(handle.usage)),
|
|
80
127
|
reasoning: streamed.reasoning,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isChatV2StructuredResponseFormat, } from './chatV2ResponseFormat.js';
|
|
1
2
|
export const LLM_CHAT_V2_TOOL_RESPONSE_FORMAT_CONFLICT_COPY = {
|
|
2
3
|
title: '"Tool use" conflicts with "Structured outputs"',
|
|
3
4
|
paragraphs: [
|
|
@@ -6,7 +7,7 @@ export const LLM_CHAT_V2_TOOL_RESPONSE_FORMAT_CONFLICT_COPY = {
|
|
|
6
7
|
],
|
|
7
8
|
};
|
|
8
9
|
export function isLLMChatV2StructuredResponseFormat(responseFormat) {
|
|
9
|
-
return responseFormat
|
|
10
|
+
return isChatV2StructuredResponseFormat(responseFormat);
|
|
10
11
|
}
|
|
11
12
|
export function hasLLMChatV2ToolResponseFormatConflict(data) {
|
|
12
13
|
return !!data.useToolCalling && isLLMChatV2StructuredResponseFormat(data.responseFormat);
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { inferType } from '../../utils/coerceType.js';
|
|
1
2
|
import { coercePromptToChatMessages, prependSystemPrompt } from '../chat/chatMessages.js';
|
|
2
3
|
import { createAssistantMessagesOutput } from '../chat/streamChatResponse.js';
|
|
3
4
|
import { streamChatV2 } from './aiSdkBridge.js';
|
|
4
5
|
import { chatMessagesToModelMessages } from './messageConverter.js';
|
|
5
6
|
import { calculateChatV2Cost } from './modelRegistry.js';
|
|
7
|
+
import { isChatV2StructuredResponseFormat } from './chatV2ResponseFormat.js';
|
|
6
8
|
import { chatV2ToolsToAiSdk } from './toolConverter.js';
|
|
7
9
|
import { getChatV2ProviderErrorStatusCode, isChatV2ProviderApiCallError, isChatV2ProviderFetchError, normalizeChatV2ProviderError, } from './chatV2Errors.js';
|
|
8
10
|
import { normalizeLLMChatV2RetryCooldownMs, normalizeLLMChatV2RetryCount, waitForLLMChatV2RetryCooldown, } from './chatV2Retry.js';
|
|
@@ -57,9 +59,24 @@ function normalizeUsage(usage, options) {
|
|
|
57
59
|
totalCost: calculateChatV2Cost(options.provider, options.modelId, promptTokens, completionTokens),
|
|
58
60
|
};
|
|
59
61
|
}
|
|
60
|
-
function
|
|
62
|
+
function tryParseStructuredResponseText(response) {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(response);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function buildResponseOutput(response, structuredOutput, responseFormat) {
|
|
71
|
+
if (!isChatV2StructuredResponseFormat(responseFormat)) {
|
|
72
|
+
return { type: 'string', value: response };
|
|
73
|
+
}
|
|
74
|
+
const parsedOutput = structuredOutput !== undefined ? structuredOutput : tryParseStructuredResponseText(response);
|
|
75
|
+
return parsedOutput !== undefined ? inferType(parsedOutput) : { type: 'string', value: response };
|
|
76
|
+
}
|
|
77
|
+
function buildCommonOutputs(requestMessages, response, structuredOutput, functionCalls, usage, reasoning, requestStatus, responseError, requestStatuses, requestErrors, options) {
|
|
61
78
|
const outputs = {
|
|
62
|
-
['response']:
|
|
79
|
+
['response']: buildResponseOutput(response, structuredOutput, options.responseFormat),
|
|
63
80
|
['in-messages']: { type: 'chat-message[]', value: requestMessages },
|
|
64
81
|
['all-messages']: createAssistantMessagesOutput(requestMessages, response, functionCalls, {
|
|
65
82
|
functionCallMode: options.functionCallMode,
|
|
@@ -335,6 +352,7 @@ export async function runChatV2Pipeline(options) {
|
|
|
335
352
|
stopSequences: options.stopSequences,
|
|
336
353
|
seed: options.seed,
|
|
337
354
|
responseOutput: options.responseOutput,
|
|
355
|
+
responseFormat: options.responseFormat,
|
|
338
356
|
providerOptions: options.providerOptions,
|
|
339
357
|
toolChoice: options.toolChoice,
|
|
340
358
|
abortSignal: options.context.signal,
|
|
@@ -342,13 +360,14 @@ export async function runChatV2Pipeline(options) {
|
|
|
342
360
|
onPartialOutput: options.emitPartialOutputs === false
|
|
343
361
|
? undefined
|
|
344
362
|
: ({ text, functionCalls }) => {
|
|
345
|
-
options.context.onPartialOutputs?.(buildCommonOutputs(requestMessages, text, functionCalls, undefined, '', undefined, undefined, [], [], {
|
|
363
|
+
options.context.onPartialOutputs?.(buildCommonOutputs(requestMessages, text, undefined, functionCalls, undefined, '', undefined, undefined, [], [], {
|
|
346
364
|
outputUsage: false,
|
|
347
365
|
outputReasoning: false,
|
|
348
366
|
outputRequestStatus: false,
|
|
349
367
|
includeFunctionCalls: options.includeFunctionCalls,
|
|
350
368
|
functionCallMode: options.functionCallMode,
|
|
351
369
|
retryOnNon200: false,
|
|
370
|
+
responseFormat: undefined,
|
|
352
371
|
}));
|
|
353
372
|
},
|
|
354
373
|
}, options).catch((caughtError) => {
|
|
@@ -375,13 +394,14 @@ export async function runChatV2Pipeline(options) {
|
|
|
375
394
|
const responseError = streamed.responseError
|
|
376
395
|
? normalizeProviderFailureMessage(streamed.responseError, options)
|
|
377
396
|
: undefined;
|
|
378
|
-
const commonOutputs = buildCommonOutputs(requestMessages, streamed.result.responseText, streamed.result.functionCalls, usage, streamed.result.reasoning, streamed.result.requestStatus, responseError, requestStatuses, requestErrors, {
|
|
397
|
+
const commonOutputs = buildCommonOutputs(requestMessages, streamed.result.responseText, streamed.result.structuredOutput, streamed.result.functionCalls, usage, streamed.result.reasoning, streamed.result.requestStatus, responseError, requestStatuses, requestErrors, {
|
|
379
398
|
outputUsage: options.outputUsage,
|
|
380
399
|
outputReasoning: options.outputReasoning,
|
|
381
400
|
outputRequestStatus: options.outputRequestStatus,
|
|
382
401
|
includeFunctionCalls: options.includeFunctionCalls,
|
|
383
402
|
functionCallMode: options.functionCallMode,
|
|
384
403
|
retryOnNon200: options.retryOnNon200,
|
|
404
|
+
responseFormat: options.responseFormat,
|
|
385
405
|
});
|
|
386
406
|
const allMessagesOutput = commonOutputs['all-messages'];
|
|
387
407
|
if (allMessagesOutput?.type !== 'chat-message[]') {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Output, jsonSchema } from 'ai';
|
|
2
2
|
import { coerceTypeOptional } from '../../utils/coerceType.js';
|
|
3
3
|
import { getInputOrData } from '../../utils/inputs.js';
|
|
4
|
+
export function isChatV2StructuredResponseFormat(responseFormat) {
|
|
5
|
+
return responseFormat === 'json' || responseFormat === 'json_schema';
|
|
6
|
+
}
|
|
4
7
|
function getResponseSchema(inputs) {
|
|
5
8
|
const responseSchemaInput = inputs['responseSchema'];
|
|
6
9
|
if (responseSchemaInput?.type === 'gpt-function') {
|
|
@@ -48,6 +48,7 @@ export async function resolveLLMChatV2RuntimeConfig(params) {
|
|
|
48
48
|
additionalTools: resolveLLMChatV2BuiltInTools(data, context, providerConfig, apiKey),
|
|
49
49
|
...generationParameters,
|
|
50
50
|
responseOutput: createChatV2ResponseOutput(responseFormatParameters),
|
|
51
|
+
responseFormat: responseFormatParameters?.responseFormat,
|
|
51
52
|
outputUsage: data.outputUsage,
|
|
52
53
|
outputReasoning: data.outputReasoning,
|
|
53
54
|
outputRequestStatus: data.outputRequestStatus,
|
|
@@ -109,35 +109,35 @@ export class CodeNodeImpl extends NodeImpl {
|
|
|
109
109
|
},
|
|
110
110
|
{
|
|
111
111
|
type: 'group',
|
|
112
|
-
label: 'Runtime
|
|
112
|
+
label: 'Runtime permissions',
|
|
113
113
|
defaultOpen: true,
|
|
114
114
|
editors: [
|
|
115
115
|
{
|
|
116
116
|
type: 'toggle',
|
|
117
|
-
label: 'fetch',
|
|
117
|
+
label: 'Allow "fetch"',
|
|
118
118
|
dataKey: 'allowFetch',
|
|
119
119
|
},
|
|
120
120
|
{
|
|
121
121
|
type: 'toggle',
|
|
122
|
-
label: 'Rivet',
|
|
122
|
+
label: 'Allow "Rivet"',
|
|
123
123
|
dataKey: 'allowRivet',
|
|
124
124
|
},
|
|
125
125
|
{
|
|
126
126
|
type: 'toggle',
|
|
127
|
-
label: 'console',
|
|
127
|
+
label: 'Allow "console"',
|
|
128
128
|
dataKey: 'allowConsole',
|
|
129
129
|
},
|
|
130
130
|
{
|
|
131
131
|
type: 'toggle',
|
|
132
|
-
label: 'require',
|
|
132
|
+
label: 'Allow "require"',
|
|
133
133
|
dataKey: 'allowRequire',
|
|
134
|
-
helperMessage: '
|
|
134
|
+
helperMessage: 'Only available with the Node executor',
|
|
135
135
|
},
|
|
136
136
|
{
|
|
137
137
|
type: 'toggle',
|
|
138
|
-
label: 'process',
|
|
138
|
+
label: 'Allow "process"',
|
|
139
139
|
dataKey: 'allowProcess',
|
|
140
|
-
helperMessage: '
|
|
140
|
+
helperMessage: 'Only available with the Node executor',
|
|
141
141
|
},
|
|
142
142
|
],
|
|
143
143
|
},
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { dedent } from 'ts-dedent';
|
|
3
|
+
import {} from '../NodeBase.js';
|
|
4
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
5
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
6
|
+
import {} from '../GraphProcessor.js';
|
|
7
|
+
export class DidRunNodeImpl extends NodeImpl {
|
|
8
|
+
static create() {
|
|
9
|
+
return {
|
|
10
|
+
type: 'didRun',
|
|
11
|
+
title: 'Did Run',
|
|
12
|
+
id: nanoid(),
|
|
13
|
+
data: {},
|
|
14
|
+
visualData: {
|
|
15
|
+
x: 0,
|
|
16
|
+
y: 0,
|
|
17
|
+
width: 250,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
getInputDefinitions(connections) {
|
|
22
|
+
const inputCount = this.#getInputPortCount(connections);
|
|
23
|
+
const inputs = [];
|
|
24
|
+
for (let i = 1; i <= inputCount; i++) {
|
|
25
|
+
inputs.push({
|
|
26
|
+
dataType: 'any',
|
|
27
|
+
id: `input${i}`,
|
|
28
|
+
title: `Input ${i}`,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return inputs;
|
|
32
|
+
}
|
|
33
|
+
getOutputDefinitions() {
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
dataType: 'boolean',
|
|
37
|
+
id: 'ran',
|
|
38
|
+
title: 'Ran',
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
static getUIData() {
|
|
43
|
+
return {
|
|
44
|
+
infoBoxBody: dedent `
|
|
45
|
+
Outputs true when all connected inputs have run. Falsy or empty values still count as having run, so this is useful for adapting "did this branch run at all?" into a boolean If port.
|
|
46
|
+
`,
|
|
47
|
+
infoBoxTitle: 'Did Run Node',
|
|
48
|
+
contextMenuTitle: 'Did Run',
|
|
49
|
+
group: ['Logic'],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
getBody() {
|
|
53
|
+
return 'Outputs true when all connected inputs have run. Falsy or empty values still count';
|
|
54
|
+
}
|
|
55
|
+
#getInputPortCount(connections) {
|
|
56
|
+
const inputNodeId = this.chartNode.id;
|
|
57
|
+
const inputConnections = connections.filter((connection) => connection.inputNodeId === inputNodeId && connection.inputId.startsWith('input'));
|
|
58
|
+
let maxInputNumber = 0;
|
|
59
|
+
for (const connection of inputConnections) {
|
|
60
|
+
const inputNumber = parseInt(connection.inputId.replace('input', ''), 10);
|
|
61
|
+
if (inputNumber > maxInputNumber) {
|
|
62
|
+
maxInputNumber = inputNumber;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return maxInputNumber + 1;
|
|
66
|
+
}
|
|
67
|
+
async process(inputData) {
|
|
68
|
+
const hasDynamicInput = Object.keys(inputData).some((key) => key.startsWith('input'));
|
|
69
|
+
if (!hasDynamicInput) {
|
|
70
|
+
return {
|
|
71
|
+
['ran']: {
|
|
72
|
+
type: 'control-flow-excluded',
|
|
73
|
+
value: undefined,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
['ran']: {
|
|
79
|
+
type: 'boolean',
|
|
80
|
+
value: true,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export const didRunNode = nodeDefinition(DidRunNodeImpl, 'Did Run');
|
|
@@ -3,7 +3,6 @@ import { nanoid } from 'nanoid/non-secure';
|
|
|
3
3
|
import { NodeImpl } from '../NodeImpl.js';
|
|
4
4
|
import { nodeDefinition } from '../NodeDefinition.js';
|
|
5
5
|
import {} from '../DataValue.js';
|
|
6
|
-
import { expectType } from '../../utils/index.js';
|
|
7
6
|
import { dedent } from 'ts-dedent';
|
|
8
7
|
export class ExtractJsonNodeImpl extends NodeImpl {
|
|
9
8
|
static create() {
|
|
@@ -25,7 +24,7 @@ export class ExtractJsonNodeImpl extends NodeImpl {
|
|
|
25
24
|
{
|
|
26
25
|
id: 'input',
|
|
27
26
|
title: 'Input',
|
|
28
|
-
dataType: '
|
|
27
|
+
dataType: 'any',
|
|
29
28
|
required: true,
|
|
30
29
|
coerced: false,
|
|
31
30
|
},
|
|
@@ -48,9 +47,9 @@ export class ExtractJsonNodeImpl extends NodeImpl {
|
|
|
48
47
|
static getUIData() {
|
|
49
48
|
return {
|
|
50
49
|
infoBoxBody: dedent `
|
|
51
|
-
Finds and parses the first JSON object in
|
|
50
|
+
Finds and parses the first JSON object in input text, or passes through an already-structured input object.
|
|
52
51
|
|
|
53
|
-
Outputs the parsed object.
|
|
52
|
+
Outputs the parsed or passed-through object.
|
|
54
53
|
`,
|
|
55
54
|
infoBoxTitle: 'Extract JSON Node',
|
|
56
55
|
contextMenuTitle: 'Extract JSON',
|
|
@@ -58,19 +57,29 @@ export class ExtractJsonNodeImpl extends NodeImpl {
|
|
|
58
57
|
};
|
|
59
58
|
}
|
|
60
59
|
async process(inputs) {
|
|
61
|
-
const
|
|
60
|
+
const input = inputs['input'];
|
|
61
|
+
if (isStructuredJsonInput(input)) {
|
|
62
|
+
return createMatchedOutput(input.value);
|
|
63
|
+
}
|
|
64
|
+
let inputString;
|
|
65
|
+
if (input?.type === 'any') {
|
|
66
|
+
if (isJsonOutputValue(input.value)) {
|
|
67
|
+
return createMatchedOutput(input.value);
|
|
68
|
+
}
|
|
69
|
+
if (typeof input.value !== 'string') {
|
|
70
|
+
throw new Error(`Expected value of type string or object but got any`);
|
|
71
|
+
}
|
|
72
|
+
inputString = input.value;
|
|
73
|
+
}
|
|
74
|
+
else if (input?.type === 'string') {
|
|
75
|
+
inputString = input.value;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
throw new Error(`Expected value of type string or object but got ${input?.type}`);
|
|
79
|
+
}
|
|
62
80
|
try {
|
|
63
81
|
const parsed = JSON.parse(inputString);
|
|
64
|
-
return
|
|
65
|
-
['output']: {
|
|
66
|
-
type: 'object',
|
|
67
|
-
value: parsed,
|
|
68
|
-
},
|
|
69
|
-
['noMatch']: {
|
|
70
|
-
type: 'control-flow-excluded',
|
|
71
|
-
value: undefined,
|
|
72
|
-
},
|
|
73
|
-
};
|
|
82
|
+
return createMatchedOutput(parsed);
|
|
74
83
|
}
|
|
75
84
|
catch (_err) {
|
|
76
85
|
// Fall back to more manual parsing
|
|
@@ -91,7 +100,7 @@ export class ExtractJsonNodeImpl extends NodeImpl {
|
|
|
91
100
|
? lastBracket
|
|
92
101
|
: lastSquareBracket;
|
|
93
102
|
const substring = inputString.substring(firstIndex, lastIndex + 1);
|
|
94
|
-
let jsonObject
|
|
103
|
+
let jsonObject;
|
|
95
104
|
try {
|
|
96
105
|
jsonObject = JSON.parse(substring);
|
|
97
106
|
}
|
|
@@ -107,16 +116,25 @@ export class ExtractJsonNodeImpl extends NodeImpl {
|
|
|
107
116
|
},
|
|
108
117
|
};
|
|
109
118
|
}
|
|
110
|
-
return
|
|
111
|
-
['output']: {
|
|
112
|
-
type: 'object',
|
|
113
|
-
value: jsonObject,
|
|
114
|
-
},
|
|
115
|
-
['noMatch']: {
|
|
116
|
-
type: 'control-flow-excluded',
|
|
117
|
-
value: undefined,
|
|
118
|
-
},
|
|
119
|
-
};
|
|
119
|
+
return createMatchedOutput(jsonObject);
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
export const extractJsonNode = nodeDefinition(ExtractJsonNodeImpl, 'Extract JSON');
|
|
123
|
+
function isStructuredJsonInput(value) {
|
|
124
|
+
return value?.type === 'object' || value?.type === 'object[]' || value?.type === 'any[]';
|
|
125
|
+
}
|
|
126
|
+
function isJsonOutputValue(value) {
|
|
127
|
+
return value != null && typeof value === 'object';
|
|
128
|
+
}
|
|
129
|
+
function createMatchedOutput(value) {
|
|
130
|
+
return {
|
|
131
|
+
['output']: {
|
|
132
|
+
type: 'object',
|
|
133
|
+
value: value,
|
|
134
|
+
},
|
|
135
|
+
['noMatch']: {
|
|
136
|
+
type: 'control-flow-excluded',
|
|
137
|
+
value: undefined,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|