@valerypopoff/rivet2-core 2.13.0 → 2.17.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/dist/cjs/bundle.cjs +28167 -23834
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/cjs/interpolationRuntime.cjs +22 -3
- package/dist/cjs/interpolationRuntime.cjs.map +3 -3
- package/dist/cjs/interpolationSyntax.cjs +9 -5
- package/dist/cjs/interpolationSyntax.cjs.map +2 -2
- package/dist/esm/api/createProcessor.js +10 -2
- package/dist/esm/api/processSettings.js +1 -0
- package/dist/esm/exports.js +6 -0
- package/dist/esm/model/GraphBoundaryCache.js +2 -1
- package/dist/esm/model/GraphProcessor.js +278 -22
- package/dist/esm/model/MarkdownSanitizationPolicy.js +2 -0
- package/dist/esm/model/NodeImpl.js +1 -1
- package/dist/esm/model/Nodes.js +16 -0
- package/dist/esm/model/ProjectBundle.js +235 -0
- package/dist/esm/model/SplitRunProcessor.js +28 -7
- package/dist/esm/model/StreamingWatchTopology.js +211 -64
- package/dist/esm/model/SubgraphProjectTarget.js +7 -0
- package/dist/esm/model/chat-v2/aiSdkBridge.js +7 -5
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +13 -8
- package/dist/esm/model/chat-v2/chatV2RuntimeOptions.js +19 -1
- package/dist/esm/model/chat-v2/chatV2Shared.js +2 -1
- package/dist/esm/model/chat-v2/llmChatV2Body.js +11 -8
- package/dist/esm/model/chat-v2/llmChatV2NodeData.js +2 -0
- package/dist/esm/model/chat-v2/llmChatV2NodeEditors.js +15 -1
- package/dist/esm/model/chat-v2/llmChatV2NodeMigration.js +22 -2
- package/dist/esm/model/chat-v2/llmProfile.js +3 -0
- package/dist/esm/model/chat-v2/llmProfileBody.js +2 -2
- package/dist/esm/model/chat-v2/llmProfileFieldRegistry.js +1 -1
- package/dist/esm/model/chat-v2/llmProfileNodeRuntime.js +1 -1
- package/dist/esm/model/chat-v2/temperature.js +9 -0
- package/dist/esm/model/classifier/credentials.js +107 -0
- package/dist/esm/model/classifier/images.js +176 -0
- package/dist/esm/model/classifier/json.js +57 -0
- package/dist/esm/model/classifier/limits.js +270 -0
- package/dist/esm/model/classifier/migration.js +97 -0
- package/dist/esm/model/classifier/openai.js +118 -0
- package/dist/esm/model/classifier/preparation.js +74 -0
- package/dist/esm/model/classifier/providers.js +422 -0
- package/dist/esm/model/classifier/questionHelpers.js +109 -0
- package/dist/esm/model/classifier/questions.js +53 -0
- package/dist/esm/model/classifier/response.js +75 -0
- package/dist/esm/model/classifier/state.js +204 -0
- package/dist/esm/model/classifier/types.js +1 -0
- package/dist/esm/model/nodeBodyMarkdown.js +37 -0
- package/dist/esm/model/nodeRunFailure.js +47 -0
- package/dist/esm/model/nodes/CatchStreamingChunksNode.js +42 -0
- package/dist/esm/model/nodes/ClassifierEvaluateNode.js +432 -0
- package/dist/esm/model/nodes/ClassifierQuestionNode.js +613 -0
- package/dist/esm/model/nodes/CodeNewNode.js +33 -9
- package/dist/esm/model/nodes/GetGlobalNode.js +2 -2
- package/dist/esm/model/nodes/HttpCallNode.js +44 -75
- package/dist/esm/model/nodes/ImageNode.js +44 -3
- package/dist/esm/model/nodes/LLMChatV2Node.js +27 -15
- package/dist/esm/model/nodes/LLMProfileNode.js +6 -12
- package/dist/esm/model/nodes/MatchCaseNode.js +76 -1
- package/dist/esm/model/nodes/MatchCaseNodeBase.js +24 -2
- package/dist/esm/model/nodes/ObjectNode.js +68 -50
- package/dist/esm/model/nodes/PassthroughNode.js +1 -1
- package/dist/esm/model/nodes/PromptNode.js +2 -1
- package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +4 -1
- package/dist/esm/model/nodes/SetGlobalNode.js +5 -3
- package/dist/esm/model/nodes/StopWatchingStreamingOutputNode.js +8 -8
- package/dist/esm/model/nodes/StreamValueNode.js +38 -0
- package/dist/esm/model/nodes/SubGraphNode.js +196 -35
- package/dist/esm/model/nodes/ToBase64Node.js +106 -0
- package/dist/esm/model/nodes/WatchStreamingOutputNode.js +7 -7
- package/dist/esm/model/nodes/codeOutputInference.js +292 -0
- package/dist/esm/recording/ExecutionRecorder.js +26 -0
- package/dist/esm/utils/base64.js +18 -15
- package/dist/esm/utils/errors.js +41 -0
- package/dist/esm/utils/interpolation.js +44 -22
- package/dist/esm/utils/interpolationSyntax.js +15 -6
- package/dist/esm/utils/serialization/serialization.js +7 -2
- package/dist/esm/utils/serialization/serializationUtils.js +6 -0
- package/dist/esm/utils/serialization/serialization_v4.js +15 -3
- package/dist/types/api/createProcessor.d.ts +7 -1
- package/dist/types/exports.d.ts +6 -0
- package/dist/types/model/CycleDetector.d.ts +1 -2
- package/dist/types/model/EditorDefinition.d.ts +24 -0
- package/dist/types/model/ExecutorProtocol.d.ts +2 -0
- package/dist/types/model/GraphBoundaryCache.d.ts +2 -0
- package/dist/types/model/GraphProcessor.d.ts +1 -0
- package/dist/types/model/MarkdownSanitizationPolicy.d.ts +1 -1
- package/dist/types/model/NodeImpl.d.ts +3 -1
- package/dist/types/model/Nodes.d.ts +10 -4
- package/dist/types/model/ProcessContext.d.ts +12 -0
- package/dist/types/model/ProjectBundle.d.ts +54 -0
- package/dist/types/model/ProjectReferenceLoader.d.ts +3 -0
- package/dist/types/model/Settings.d.ts +4 -0
- package/dist/types/model/StreamingWatchTopology.d.ts +17 -1
- package/dist/types/model/SubgraphProjectTarget.d.ts +38 -0
- package/dist/types/model/chat-v2/chatV2Shared.d.ts +1 -1
- package/dist/types/model/chat-v2/llmChatV2NodeData.d.ts +3 -1
- package/dist/types/model/chat-v2/llmChatV2NodeMigration.d.ts +3 -1
- package/dist/types/model/chat-v2/llmProfileFieldRegistry.d.ts +6 -6
- package/dist/types/model/chat-v2/temperature.d.ts +2 -0
- package/dist/types/model/classifier/credentials.d.ts +26 -0
- package/dist/types/model/classifier/images.d.ts +18 -0
- package/dist/types/model/classifier/json.d.ts +19 -0
- package/dist/types/model/classifier/limits.d.ts +42 -0
- package/dist/types/model/classifier/migration.d.ts +18 -0
- package/dist/types/model/classifier/openai.d.ts +5 -0
- package/dist/types/model/classifier/preparation.d.ts +16 -0
- package/dist/types/model/classifier/providers.d.ts +136 -0
- package/dist/types/model/classifier/questionHelpers.d.ts +33 -0
- package/dist/types/model/classifier/questions.d.ts +4 -0
- package/dist/types/model/classifier/response.d.ts +4 -0
- package/dist/types/model/classifier/state.d.ts +37 -0
- package/dist/types/model/classifier/types.d.ts +60 -0
- package/dist/types/model/nodeBodyMarkdown.d.ts +19 -0
- package/dist/types/model/nodeRunFailure.d.ts +13 -0
- package/dist/types/model/nodes/CatchStreamingChunksNode.d.ts +20 -0
- package/dist/types/model/nodes/ClassifierEvaluateNode.d.ts +50 -0
- package/dist/types/model/nodes/ClassifierQuestionNode.d.ts +60 -0
- package/dist/types/model/nodes/CodeNewNode.d.ts +6 -0
- package/dist/types/model/nodes/GetGlobalNode.d.ts +7 -0
- package/dist/types/model/nodes/ImageNode.d.ts +4 -0
- package/dist/types/model/nodes/LLMChatV2Node.d.ts +1 -0
- package/dist/types/model/nodes/MatchCaseNode.d.ts +8 -1
- package/dist/types/model/nodes/MatchCaseNodeBase.d.ts +13 -1
- package/dist/types/model/nodes/ObjectNode.d.ts +6 -0
- package/dist/types/model/nodes/StreamValueNode.d.ts +14 -0
- package/dist/types/model/nodes/SubGraphNode.d.ts +21 -2
- package/dist/types/model/nodes/ToBase64Node.d.ts +12 -0
- package/dist/types/model/nodes/codeOutputInference.d.ts +31 -0
- package/dist/types/recording/ExecutionRecorder.d.ts +8 -2
- package/dist/types/utils/base64.d.ts +1 -1
- package/dist/types/utils/errors.d.ts +4 -0
- package/dist/types/utils/interpolation.d.ts +13 -2
- package/dist/types/utils/interpolationSyntax.d.ts +1 -1
- package/dist/types/utils/serialization/serialization.d.ts +3 -1
- package/dist/types/utils/serialization/serializationUtils.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { prepareClassifierImageText, prepareNativeClassifierImage, inspectClassifierImage, } from './images.js';
|
|
2
|
+
import { snapshotClassifierJson, classifierArrayValues, classifierDataProperty } from './json.js';
|
|
3
|
+
import { ClassifierValueBudget, CLASSIFIER_LIMITS } from './limits.js';
|
|
4
|
+
const record = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
5
|
+
const nativeImage = (value) => record(value) &&
|
|
6
|
+
(classifierDataProperty(value, 'type') === 'image' ||
|
|
7
|
+
('mediaType' in value && classifierDataProperty(value, 'data') instanceof Uint8Array));
|
|
8
|
+
const message = (value) => record(value) && 'type' in value && 'message' in value;
|
|
9
|
+
/** Graph and direct provider calls converge here; no caller can bypass preparation. */
|
|
10
|
+
export function prepareClassifierState(input, budget) {
|
|
11
|
+
if (input.stateInput !== undefined) {
|
|
12
|
+
if (input.state !== '' || input.stateMessages !== undefined)
|
|
13
|
+
throw new Error('Rivet State input and provider State cannot be supplied together.');
|
|
14
|
+
return prepareRivetState(input.stateInput, budget);
|
|
15
|
+
}
|
|
16
|
+
if (input.stateMessages === undefined)
|
|
17
|
+
return { kind: 'json', value: snapshotClassifierJson(input.state, 'State', budget) };
|
|
18
|
+
if (input.state !== '')
|
|
19
|
+
throw new Error('Structured State and multimodal State messages cannot be supplied together.');
|
|
20
|
+
const messages = snapshotClassifierJson(input.stateMessages, 'State', budget);
|
|
21
|
+
const images = inspectStateMessages(messages, budget.check);
|
|
22
|
+
return { kind: 'messages', messages: messages, images };
|
|
23
|
+
}
|
|
24
|
+
/** Standalone compatibility helper uses the same preparation, not a separate validator. */
|
|
25
|
+
export function normalizeClassifierState(input, check, maxBytes) {
|
|
26
|
+
const state = input === undefined
|
|
27
|
+
? { kind: 'json', value: '' }
|
|
28
|
+
: prepareRivetState(input, new ClassifierValueBudget(check, maxBytes));
|
|
29
|
+
return state.kind === 'json' ? { state: state.value } : { state: '', stateMessages: state.messages };
|
|
30
|
+
}
|
|
31
|
+
function prepareRivetState(input, budget) {
|
|
32
|
+
const check = budget.check;
|
|
33
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input))
|
|
34
|
+
throw new Error('State input must contain a Rivet data value.');
|
|
35
|
+
const type = classifierDataProperty(input, 'type');
|
|
36
|
+
const value = classifierDataProperty(input, 'value');
|
|
37
|
+
if (typeof type !== 'string')
|
|
38
|
+
throw new Error('State input type must be a string.');
|
|
39
|
+
if (![
|
|
40
|
+
'string',
|
|
41
|
+
'string[]',
|
|
42
|
+
'image',
|
|
43
|
+
'image[]',
|
|
44
|
+
'chat-message',
|
|
45
|
+
'chat-message[]',
|
|
46
|
+
'object',
|
|
47
|
+
'object[]',
|
|
48
|
+
'any',
|
|
49
|
+
'any[]',
|
|
50
|
+
].includes(type))
|
|
51
|
+
throw new Error(`Unsupported State input type '${type}'.`);
|
|
52
|
+
if (type.endsWith('[]') && !Array.isArray(value))
|
|
53
|
+
throw new Error('State array input must contain an array.');
|
|
54
|
+
if (type === 'string' && typeof value !== 'string')
|
|
55
|
+
throw new Error('State string input must contain a string.');
|
|
56
|
+
if (type === 'object' && !record(value) && !Array.isArray(value))
|
|
57
|
+
throw new Error('State object input must contain a JSON object or array.');
|
|
58
|
+
if ((type === 'image' || type === 'chat-message') && Array.isArray(value))
|
|
59
|
+
throw new Error('State scalar input must not contain an array.');
|
|
60
|
+
// Explicit structured evidence needs just one validating/copying traversal.
|
|
61
|
+
if (type === 'object' || type === 'object[]')
|
|
62
|
+
return { kind: 'json', value: snapshotClassifierJson(value, 'State', budget) };
|
|
63
|
+
// Ambiguous/multimodal inputs must be bounded before image inspection/encoding.
|
|
64
|
+
budget.inspect(value);
|
|
65
|
+
const imageTexts = new Map();
|
|
66
|
+
const textImage = (text) => {
|
|
67
|
+
const image = imageTexts.get(text) ?? prepareClassifierImageText(text, check);
|
|
68
|
+
if (image)
|
|
69
|
+
imageTexts.set(text, image);
|
|
70
|
+
return image;
|
|
71
|
+
};
|
|
72
|
+
const hasContent = (item) => nativeImage(item) || message(item) || (typeof item === 'string' && textImage(item) !== undefined);
|
|
73
|
+
const arrayHasContent = () => {
|
|
74
|
+
for (const item of classifierArrayValues(value, 'State')) {
|
|
75
|
+
check();
|
|
76
|
+
if (hasContent(item))
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
81
|
+
const contentType = ['image', 'image[]', 'string[]', 'chat-message', 'chat-message[]'].includes(type);
|
|
82
|
+
const inferredContent = (type === 'any' || type === 'any[]') && (hasContent(value) || (Array.isArray(value) && arrayHasContent()));
|
|
83
|
+
if (!(contentType || inferredContent || (type === 'string' && textImage(value)))) {
|
|
84
|
+
if (typeof value !== 'string' && !record(value) && !Array.isArray(value))
|
|
85
|
+
throw new Error('State must be text, a JSON object/array, images, or user messages.');
|
|
86
|
+
// Text is already detached. Ambiguous JSON additionally needs plain-data validation.
|
|
87
|
+
return {
|
|
88
|
+
kind: 'json',
|
|
89
|
+
value: typeof value === 'string'
|
|
90
|
+
? value
|
|
91
|
+
: snapshotClassifierJson(value, 'State', new ClassifierValueBudget(check)),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const values = Array.isArray(value) ? value : [value];
|
|
95
|
+
if (type === 'image[]' && values.length > 128)
|
|
96
|
+
throw new Error('State must contain at most 128 images.');
|
|
97
|
+
const messages = [];
|
|
98
|
+
const images = [];
|
|
99
|
+
let partCount = 0;
|
|
100
|
+
const imagePart = (prepare) => {
|
|
101
|
+
if (images.length >= 128)
|
|
102
|
+
throw new Error('State must contain at most 128 images.');
|
|
103
|
+
const image = prepare();
|
|
104
|
+
images.push(image);
|
|
105
|
+
return { type: 'image', dataUrl: image.dataUrl };
|
|
106
|
+
};
|
|
107
|
+
let parts = [];
|
|
108
|
+
const flush = () => {
|
|
109
|
+
if (parts.length) {
|
|
110
|
+
if (messages.length >= CLASSIFIER_LIMITS.messages)
|
|
111
|
+
throw new Error('State has too many messages.');
|
|
112
|
+
messages.push({ parts });
|
|
113
|
+
parts = [];
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const part = (item) => {
|
|
117
|
+
check();
|
|
118
|
+
if (++partCount > CLASSIFIER_LIMITS.parts)
|
|
119
|
+
throw new Error('State has too many content parts.');
|
|
120
|
+
if (typeof item === 'string') {
|
|
121
|
+
const image = textImage(item);
|
|
122
|
+
return image ? imagePart(() => image) : { type: 'text', text: item };
|
|
123
|
+
}
|
|
124
|
+
if (nativeImage(item))
|
|
125
|
+
return imagePart(() => prepareNativeClassifierImage(item, check));
|
|
126
|
+
throw new Error('Multimodal State accepts only text, native images, image data URLs, and user messages.');
|
|
127
|
+
};
|
|
128
|
+
for (const item of classifierArrayValues(values, 'State')) {
|
|
129
|
+
check();
|
|
130
|
+
if (type === 'image' || type === 'image[]') {
|
|
131
|
+
if (++partCount > CLASSIFIER_LIMITS.parts)
|
|
132
|
+
throw new Error('State has too many content parts.');
|
|
133
|
+
parts.push(imagePart(() => prepareNativeClassifierImage(item, check)));
|
|
134
|
+
}
|
|
135
|
+
else if (type === 'string[]' && typeof item !== 'string') {
|
|
136
|
+
throw new Error('State string array must contain only strings.');
|
|
137
|
+
}
|
|
138
|
+
else if (message(item)) {
|
|
139
|
+
if (classifierDataProperty(item, 'type') !== 'user' || 'function_call' in item || 'function_calls' in item)
|
|
140
|
+
throw new Error('Classifier State supports only user messages, not system, developer, assistant, or tool messages.');
|
|
141
|
+
flush();
|
|
142
|
+
if (messages.length >= CLASSIFIER_LIMITS.messages)
|
|
143
|
+
throw new Error('State has too many messages.');
|
|
144
|
+
const contentValue = classifierDataProperty(item, 'message');
|
|
145
|
+
const content = Array.isArray(contentValue) ? contentValue : [contentValue];
|
|
146
|
+
messages.push({
|
|
147
|
+
parts: content.length
|
|
148
|
+
? Array.from(classifierArrayValues(content, 'State'), part)
|
|
149
|
+
: [{ type: 'text', text: '' }],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
if (type === 'chat-message' || type === 'chat-message[]')
|
|
154
|
+
throw new Error('State must contain valid user messages.');
|
|
155
|
+
parts.push(part(item));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
flush();
|
|
159
|
+
return messages.length ? { kind: 'messages', messages, images } : { kind: 'json', value: '' };
|
|
160
|
+
}
|
|
161
|
+
function inspectStateMessages(messages, check) {
|
|
162
|
+
if (!Array.isArray(messages))
|
|
163
|
+
throw new Error('State messages must be an array.');
|
|
164
|
+
if (messages.length > CLASSIFIER_LIMITS.messages)
|
|
165
|
+
throw new Error('State has too many messages.');
|
|
166
|
+
const images = [];
|
|
167
|
+
let parts = 0;
|
|
168
|
+
for (const message of messages) {
|
|
169
|
+
if (!record(message) || !Array.isArray(message.parts))
|
|
170
|
+
throw new Error('State message parts must be an array.');
|
|
171
|
+
for (const part of message.parts) {
|
|
172
|
+
check();
|
|
173
|
+
if (++parts > CLASSIFIER_LIMITS.parts)
|
|
174
|
+
throw new Error('State has too many content parts.');
|
|
175
|
+
if (!record(part))
|
|
176
|
+
throw new Error('Invalid State content part.');
|
|
177
|
+
if (part.type === 'image' && typeof part.dataUrl === 'string') {
|
|
178
|
+
if (images.length >= 128)
|
|
179
|
+
throw new Error('State must contain at most 128 images.');
|
|
180
|
+
images.push({ ...inspectClassifierImage(part.dataUrl, check), dataUrl: part.dataUrl });
|
|
181
|
+
}
|
|
182
|
+
else if (part.type !== 'text' || typeof part.text !== 'string')
|
|
183
|
+
throw new Error('Invalid State content part.');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return images;
|
|
187
|
+
}
|
|
188
|
+
/** System One separates images; numbered placeholders retain caption/image associations. */
|
|
189
|
+
export function systemOneState(input) {
|
|
190
|
+
if (!input.stateMessages)
|
|
191
|
+
return { state: input.state };
|
|
192
|
+
const images = [];
|
|
193
|
+
const state = input.stateMessages
|
|
194
|
+
.map(({ parts }) => parts
|
|
195
|
+
.map((part) => {
|
|
196
|
+
if (part.type === 'text')
|
|
197
|
+
return part.text;
|
|
198
|
+
images.push(part.dataUrl);
|
|
199
|
+
return `[Image ${images.length}]`;
|
|
200
|
+
})
|
|
201
|
+
.join('\n'))
|
|
202
|
+
.join('\n\n');
|
|
203
|
+
return { state, ...(images.length ? { images } : {}) };
|
|
204
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const NODE_BODY_FIELD_LABEL_CLASS = 'rivet-node-body-field-label';
|
|
2
|
+
export const NODE_BODY_FIELD_VALUE_CLASS = 'rivet-node-body-field-value';
|
|
3
|
+
export const NODE_BODY_FIELD_ROW_CLASS = 'rivet-node-body-field-row';
|
|
4
|
+
export const NODE_BODY_TEXT_ROW_CLASS = 'rivet-node-body-text-row';
|
|
5
|
+
export const NODE_BODY_SEPARATOR_CLASS = 'rivet-node-body-separator';
|
|
6
|
+
/**
|
|
7
|
+
* Renders a label/value pair for a Markdown node body without letting authored
|
|
8
|
+
* values become Markdown. Class-based styling is deliberately used instead of
|
|
9
|
+
* inline styles because node-body Markdown is sanitized before it reaches DOM.
|
|
10
|
+
*/
|
|
11
|
+
export function formatNodeBodyMarkdownField(label, value) {
|
|
12
|
+
return `<div class="${NODE_BODY_FIELD_ROW_CLASS}">${formatNodeBodyMarkdownLabel(label)} ${formatNodeBodyMarkdownValue(value)}</div>`;
|
|
13
|
+
}
|
|
14
|
+
/** Renders a sanitized, styled field label without adding an empty value node. */
|
|
15
|
+
export function formatNodeBodyMarkdownLabel(label) {
|
|
16
|
+
return `<span class="${NODE_BODY_FIELD_LABEL_CLASS}">${escapeNodeBodyMarkdownText(label)}:</span>`;
|
|
17
|
+
}
|
|
18
|
+
/** Wraps authored text in a raw HTML span so Markdown punctuation stays literal. */
|
|
19
|
+
export function formatNodeBodyMarkdownValue(value) {
|
|
20
|
+
return `<span class="${NODE_BODY_FIELD_VALUE_CLASS}">${escapeNodeBodyMarkdownText(value)}</span>`;
|
|
21
|
+
}
|
|
22
|
+
/** Renders unlabelled authored text as a standalone Markdown body row. */
|
|
23
|
+
export function formatNodeBodyMarkdownTextRow(value) {
|
|
24
|
+
return `<div class="${NODE_BODY_TEXT_ROW_CLASS}">${formatNodeBodyMarkdownValue(value)}</div>`;
|
|
25
|
+
}
|
|
26
|
+
/** A block separator that does not cause Markdown to create empty paragraphs. */
|
|
27
|
+
export function formatNodeBodyMarkdownSeparator() {
|
|
28
|
+
return `<div class="${NODE_BODY_SEPARATOR_CLASS}"></div>`;
|
|
29
|
+
}
|
|
30
|
+
function escapeNodeBodyMarkdownText(value) {
|
|
31
|
+
return String(value ?? '')
|
|
32
|
+
.replace(/&/g, '&')
|
|
33
|
+
.replace(/</g, '<')
|
|
34
|
+
.replace(/>/g, '>')
|
|
35
|
+
.replace(/"/g, '"')
|
|
36
|
+
.replace(/'/g, ''');
|
|
37
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { getChatV2ProviderErrorStatusCode } from './chat-v2/chatV2Errors.js';
|
|
2
|
+
import { formatCaughtRunError } from '../utils/errors.js';
|
|
3
|
+
export function hasRunFailureOutputs(data) {
|
|
4
|
+
return data.catchRequestFailed === true || data.errorOnNon200 === false;
|
|
5
|
+
}
|
|
6
|
+
export function getRunFailureOutputDefinitions(data) {
|
|
7
|
+
return hasRunFailureOutputs(data)
|
|
8
|
+
? [
|
|
9
|
+
{ id: 'runFailed', title: 'Run failed', dataType: 'boolean' },
|
|
10
|
+
{ id: 'runError', title: 'Run error', dataType: 'string' },
|
|
11
|
+
]
|
|
12
|
+
: [];
|
|
13
|
+
}
|
|
14
|
+
export function shouldCatchRunFailure(data, error, signal) {
|
|
15
|
+
// Only the caller's signal proves graph cancellation. A provider or tool can
|
|
16
|
+
// throw AbortError for its own interrupted request while the graph stays live.
|
|
17
|
+
if (signal.aborted)
|
|
18
|
+
return false;
|
|
19
|
+
if (data.catchRequestFailed === true)
|
|
20
|
+
return true;
|
|
21
|
+
const status = getChatV2ProviderErrorStatusCode(error);
|
|
22
|
+
return data.errorOnNon200 === false && status != null && (status < 200 || status >= 300);
|
|
23
|
+
}
|
|
24
|
+
export function withRunSuccessOutputs(data, outputs) {
|
|
25
|
+
if (!hasRunFailureOutputs(data))
|
|
26
|
+
return outputs;
|
|
27
|
+
return {
|
|
28
|
+
...outputs,
|
|
29
|
+
['runFailed']: { type: 'boolean', value: false },
|
|
30
|
+
['runError']: { type: 'control-flow-excluded', value: undefined },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Never route a partial model answer or fabricated usage through the success branch. */
|
|
34
|
+
export function createCaughtRunFailureOutputs(definitions, error, evidence) {
|
|
35
|
+
const outputs = {};
|
|
36
|
+
for (const definition of definitions) {
|
|
37
|
+
outputs[definition.id] = { type: 'control-flow-excluded', value: undefined };
|
|
38
|
+
}
|
|
39
|
+
for (const id of ['requestBody', 'responseBody', 'llmAttempts', 'llmProfileSummary']) {
|
|
40
|
+
const port = id;
|
|
41
|
+
if (port in outputs && evidence?.[port] != null)
|
|
42
|
+
outputs[port] = evidence[port];
|
|
43
|
+
}
|
|
44
|
+
outputs['runFailed'] = { type: 'boolean', value: true };
|
|
45
|
+
outputs['runError'] = { type: 'string', value: formatCaughtRunError(error) };
|
|
46
|
+
return outputs;
|
|
47
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
3
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
+
export const MAX_CAUGHT_STREAMING_CHUNKS = 1024;
|
|
5
|
+
/** The processor owns this node's early, once-only output. */
|
|
6
|
+
export class CatchStreamingChunksNodeImpl extends NodeImpl {
|
|
7
|
+
static create() {
|
|
8
|
+
return {
|
|
9
|
+
id: nanoid(),
|
|
10
|
+
type: 'catchStreamingChunks',
|
|
11
|
+
title: 'Catch streaming chunks',
|
|
12
|
+
data: { count: 1 },
|
|
13
|
+
visualData: { x: 0, y: 0, width: 220 },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
getInputDefinitions() {
|
|
17
|
+
return [{ id: 'stream', title: 'Streaming Output', dataType: 'any', required: true }];
|
|
18
|
+
}
|
|
19
|
+
getOutputDefinitions() {
|
|
20
|
+
return [{ id: 'value', title: 'Value', dataType: 'any' }];
|
|
21
|
+
}
|
|
22
|
+
static getUIData() {
|
|
23
|
+
return {
|
|
24
|
+
contextMenuTitle: 'Catch streaming chunks',
|
|
25
|
+
group: ['Streaming'],
|
|
26
|
+
infoBoxTitle: 'Catch streaming chunks node',
|
|
27
|
+
infoBoxBody: 'Returns the first N streaming snapshots as one ordinary value. For N=1 it returns the value itself; for N>1 it returns an array. It completes once and never runs a repeated Watch branch.',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
getEditors() {
|
|
31
|
+
return [{ type: 'number', dataKey: 'count', label: 'Number of chunks', min: 1, max: MAX_CAUGHT_STREAMING_CHUNKS }];
|
|
32
|
+
}
|
|
33
|
+
getBody() {
|
|
34
|
+
return `First ${this.data.count ?? 1} ${this.data.count === 1 ? 'chunk' : 'chunks'}`;
|
|
35
|
+
}
|
|
36
|
+
async process(inputs) {
|
|
37
|
+
// GraphProcessor schedules connected instances at their stream boundary.
|
|
38
|
+
// Retain the ordinary final-only behavior for direct node tests.
|
|
39
|
+
return { ['value']: inputs['stream'] };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export const catchStreamingChunksNode = nodeDefinition(CatchStreamingChunksNodeImpl, 'Catch streaming chunks');
|