@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,432 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
3
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
+
import { createExcludedNodeOutputs } from '../NodeExclusionPolicy.js';
|
|
5
|
+
import { formatNodeBodyMarkdownField, formatNodeBodyMarkdownSeparator } from '../nodeBodyMarkdown.js';
|
|
6
|
+
import { createCaughtRunFailureOutputs, getRunFailureOutputDefinitions, shouldCatchRunFailure, withRunSuccessOutputs, } from '../nodeRunFailure.js';
|
|
7
|
+
import { getNextVariadicPortIndex } from './variadicPortIndex.js';
|
|
8
|
+
import { resolveClassifierApiKey, } from '../classifier/credentials.js';
|
|
9
|
+
import { classifierArrayValues, classifierInputDataValue } from '../classifier/json.js';
|
|
10
|
+
import { normalizeClassifierState } from '../classifier/state.js';
|
|
11
|
+
import { CLASSIFIER_LIMITS, classifierPreparationCheck, ClassifierValueBudget } from '../classifier/limits.js';
|
|
12
|
+
import { calculateClassifierUsageCost, classifierProviders, DEFAULT_CLASSIFIER_RETRY_ON_NON_200_COOLDOWN_MS, DEFAULT_CLASSIFIER_RETRY_ON_NON_200_REPEAT_TIMES, getClassifierProvider, normalizeClassifierNon200RetryCooldownMs, normalizeClassifierNon200RetryCount, } from '../classifier/providers.js';
|
|
13
|
+
const QUESTION_TYPES = ['object', 'object[]', 'any', 'any[]'];
|
|
14
|
+
export class ClassifierEvaluateNodeImpl extends NodeImpl {
|
|
15
|
+
static create() {
|
|
16
|
+
return {
|
|
17
|
+
type: 'classifierEvaluate',
|
|
18
|
+
title: 'Classifier Evaluate',
|
|
19
|
+
id: nanoid(),
|
|
20
|
+
visualData: { x: 0, y: 0, width: 260 },
|
|
21
|
+
// Keep the default model in the provider descriptor rather than
|
|
22
|
+
// serializing Jev's model into every new node. A future provider then
|
|
23
|
+
// receives its own default when an author changes the Provider field.
|
|
24
|
+
data: { provider: 'jev', timeoutMs: 30_000, errorOnNon200: true, catchRequestFailed: false },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
getInputDefinitions(connections) {
|
|
28
|
+
const inputs = [
|
|
29
|
+
{
|
|
30
|
+
id: 'state',
|
|
31
|
+
title: 'State',
|
|
32
|
+
dataType: [
|
|
33
|
+
'string',
|
|
34
|
+
'string[]',
|
|
35
|
+
'image',
|
|
36
|
+
'image[]',
|
|
37
|
+
'chat-message',
|
|
38
|
+
'chat-message[]',
|
|
39
|
+
'object',
|
|
40
|
+
'object[]',
|
|
41
|
+
'any',
|
|
42
|
+
'any[]',
|
|
43
|
+
],
|
|
44
|
+
description: 'Shared text, images, or assembled user messages for every question. Image bytes and base64 images are encoded automatically. JSON objects remain structured state.',
|
|
45
|
+
required: false,
|
|
46
|
+
splitRunBehavior: 'preserve-array',
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
if (this.data.useModelInput) {
|
|
50
|
+
inputs.push({ id: 'model', title: 'Model', dataType: 'string', required: true });
|
|
51
|
+
}
|
|
52
|
+
if (this.data.apiKeySource === 'input') {
|
|
53
|
+
inputs.push({ id: 'apiKey', title: 'API Key', dataType: 'string', required: true });
|
|
54
|
+
}
|
|
55
|
+
const count = getNextVariadicPortIndex(connections, this.chartNode.id, 'question', 'strict-positive');
|
|
56
|
+
for (let index = 1; index <= count; index++) {
|
|
57
|
+
inputs.push({
|
|
58
|
+
id: `question${index}`,
|
|
59
|
+
title: `Question ${index}`,
|
|
60
|
+
dataType: QUESTION_TYPES,
|
|
61
|
+
required: false,
|
|
62
|
+
splitRunBehavior: 'preserve-array',
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return inputs;
|
|
66
|
+
}
|
|
67
|
+
getOutputDefinitions() {
|
|
68
|
+
const outputs = [
|
|
69
|
+
{ id: 'answers', title: 'Answers', dataType: 'object' },
|
|
70
|
+
{ id: 'usage', title: 'Usage', dataType: 'object' },
|
|
71
|
+
{
|
|
72
|
+
id: 'cost',
|
|
73
|
+
title: 'Cost',
|
|
74
|
+
dataType: 'number',
|
|
75
|
+
description: 'Estimated USD cost for this successful evaluation. Excluded when pricing cannot be calculated.',
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
if (this.data.outputRequestBody === true) {
|
|
79
|
+
outputs.push({ id: 'requestBody', title: 'Request body', dataType: 'object' });
|
|
80
|
+
}
|
|
81
|
+
if (this.data.outputResponseBody === true) {
|
|
82
|
+
outputs.push({ id: 'responseBody', title: 'Response body', dataType: 'object' });
|
|
83
|
+
}
|
|
84
|
+
return [...outputs, ...getRunFailureOutputDefinitions({ catchRequestFailed: this.data.catchRequestFailed })];
|
|
85
|
+
}
|
|
86
|
+
getEditors() {
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
type: 'group',
|
|
90
|
+
label: 'Model',
|
|
91
|
+
defaultOpen: true,
|
|
92
|
+
editors: [
|
|
93
|
+
{
|
|
94
|
+
type: 'dropdown',
|
|
95
|
+
label: 'Provider',
|
|
96
|
+
dataKey: 'provider',
|
|
97
|
+
defaultValue: 'jev',
|
|
98
|
+
options: classifierProviders.map((provider) => ({ value: provider.id, label: provider.label })),
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
type: 'string',
|
|
102
|
+
label: 'Model',
|
|
103
|
+
dataKey: 'model',
|
|
104
|
+
useInputToggleDataKey: 'useModelInput',
|
|
105
|
+
placeholder: getProviderForDisplay(this.data.provider).defaultModel,
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: 'segmented',
|
|
109
|
+
label: 'API key source',
|
|
110
|
+
ariaLabel: 'API key source',
|
|
111
|
+
dataKey: 'apiKeySource',
|
|
112
|
+
defaultValue: 'configured',
|
|
113
|
+
options: [
|
|
114
|
+
{ value: 'configured', label: 'Automatic' },
|
|
115
|
+
{ value: 'classifier-settings', label: 'Classifier settings' },
|
|
116
|
+
{ value: 'input', label: 'Input port' },
|
|
117
|
+
],
|
|
118
|
+
helperMessage: getApiKeySourceHelperMessage,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
type: 'custom',
|
|
122
|
+
label: 'Configured API key names',
|
|
123
|
+
customEditorId: 'ClassifierCredentialNames',
|
|
124
|
+
hideIf: (data) => data.apiKeySource === 'input' || data.apiKeySource === 'classifier-settings',
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
type: 'group',
|
|
130
|
+
label: 'Outputs',
|
|
131
|
+
editors: [
|
|
132
|
+
{
|
|
133
|
+
type: 'toggle',
|
|
134
|
+
label: 'Output usage details',
|
|
135
|
+
dataKey: 'outputUsage',
|
|
136
|
+
helperMessage: 'Adds estimated totalCost to Usage only for recognized requested/returned models. Unknown pricing is omitted. Provider-specific premiums are not included.',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
type: 'toggle',
|
|
140
|
+
label: 'Output request body',
|
|
141
|
+
dataKey: 'outputRequestBody',
|
|
142
|
+
helperMessage: 'Adds the exact classifier request JSON body sent to the provider. It excludes authorization headers and Rivet-managed API keys.',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
type: 'toggle',
|
|
146
|
+
label: 'Output response body',
|
|
147
|
+
dataKey: 'outputResponseBody',
|
|
148
|
+
helperMessage: 'Adds the complete classifier JSON response returned by the provider after validation. Rivet does not redact or truncate captured content.',
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
type: 'group',
|
|
154
|
+
label: 'Advanced',
|
|
155
|
+
editors: [
|
|
156
|
+
{
|
|
157
|
+
type: 'number',
|
|
158
|
+
label: 'Overall timeout (seconds)',
|
|
159
|
+
dataKey: 'timeoutMs',
|
|
160
|
+
defaultValue: 30,
|
|
161
|
+
storageMultiplier: 1000,
|
|
162
|
+
min: 1,
|
|
163
|
+
max: 600,
|
|
164
|
+
step: 1,
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
type: 'group',
|
|
170
|
+
label: 'Error behavior',
|
|
171
|
+
editors: [
|
|
172
|
+
{
|
|
173
|
+
type: 'toggle',
|
|
174
|
+
label: 'Fail on non-2XX status code',
|
|
175
|
+
dataKey: 'errorOnNon200',
|
|
176
|
+
defaultValue: true,
|
|
177
|
+
helperMessage: 'After retries, throw on a rejected HTTP request. When disabled, unavailable outputs are excluded. Run failed and Run error are available only when Catch all failures is enabled.',
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
type: 'toggle',
|
|
181
|
+
label: 'Catch all failures',
|
|
182
|
+
dataKey: 'catchRequestFailed',
|
|
183
|
+
helperMessage: 'Return any node execution failure through Run failed and Run error instead of stopping the graph. Explicit graph cancellation is never caught.',
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
type: 'toggle',
|
|
187
|
+
label: 'Retry on non-200',
|
|
188
|
+
dataKey: 'retryOnNon200',
|
|
189
|
+
helperMessage: 'Retries non-authentication, non-validation provider HTTP responses. Provider rate-limit retries remain automatic.',
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
type: 'number',
|
|
193
|
+
label: 'Repeat times',
|
|
194
|
+
dataKey: 'retryOnNon200RepeatTimes',
|
|
195
|
+
defaultValue: DEFAULT_CLASSIFIER_RETRY_ON_NON_200_REPEAT_TIMES,
|
|
196
|
+
min: 1,
|
|
197
|
+
step: 1,
|
|
198
|
+
layout: 'inline',
|
|
199
|
+
helperMessage: 'Times to repeat after the initial request',
|
|
200
|
+
hideIf: (data) => !data.retryOnNon200,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
type: 'number',
|
|
204
|
+
label: 'Cooldown, ms',
|
|
205
|
+
dataKey: 'retryOnNon200CooldownMs',
|
|
206
|
+
defaultValue: DEFAULT_CLASSIFIER_RETRY_ON_NON_200_COOLDOWN_MS,
|
|
207
|
+
min: 0,
|
|
208
|
+
step: 1,
|
|
209
|
+
layout: 'inline',
|
|
210
|
+
helperMessage: 'Milliseconds to wait between repeats',
|
|
211
|
+
hideIf: (data) => !data.retryOnNon200,
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
},
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
getBody() {
|
|
218
|
+
const sections = getClassifierEvaluateBodySections(this.data);
|
|
219
|
+
return {
|
|
220
|
+
type: 'markdown',
|
|
221
|
+
disableLinks: true,
|
|
222
|
+
text: sections
|
|
223
|
+
.flatMap((section, index) => [
|
|
224
|
+
...(index === 0 ? [] : [formatNodeBodyMarkdownSeparator()]),
|
|
225
|
+
...section.fields.map((field) => formatNodeBodyMarkdownField(field.label, field.value)),
|
|
226
|
+
])
|
|
227
|
+
.join(''),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
static getUIData() {
|
|
231
|
+
return {
|
|
232
|
+
contextMenuTitle: 'Classifier Evaluate',
|
|
233
|
+
infoBoxTitle: 'Classifier Evaluate',
|
|
234
|
+
infoBoxBody: 'Evaluates shared state and optional images against typed classifier questions. Images are shared evidence for every question.',
|
|
235
|
+
group: ['Classifier'],
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
async process(inputs, context) {
|
|
239
|
+
try {
|
|
240
|
+
context.signal.throwIfAborted();
|
|
241
|
+
const outputs = await this.processRun(inputs, context);
|
|
242
|
+
context.signal.throwIfAborted();
|
|
243
|
+
return withRunSuccessOutputs({ catchRequestFailed: this.data.catchRequestFailed }, outputs);
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
if (!shouldCatchRunFailure(this.data, error, context.signal))
|
|
247
|
+
throw error;
|
|
248
|
+
return this.data.catchRequestFailed === true
|
|
249
|
+
? createCaughtRunFailureOutputs(this.getOutputDefinitions(), error)
|
|
250
|
+
: createExcludedNodeOutputs(this.chartNode, this.getOutputDefinitions());
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async processRun(inputs, context) {
|
|
254
|
+
const timeoutMs = normalizeTimeout(this.data.timeoutMs);
|
|
255
|
+
const deadline = Date.now() + timeoutMs;
|
|
256
|
+
const checkPreparation = classifierPreparationCheck(context.signal, deadline, timeoutMs);
|
|
257
|
+
checkPreparation();
|
|
258
|
+
const provider = getClassifierProvider(this.data.provider);
|
|
259
|
+
if (!provider.browserExecutionSupported && context.executor === 'browser') {
|
|
260
|
+
throw new Error(`${provider.label} cannot run in the Browser executor. Select the Node executor, or run through Studio Server or remote debugging.`);
|
|
261
|
+
}
|
|
262
|
+
const apiKey = resolveClassifierApiKey({
|
|
263
|
+
apiKeyNames: this.data.apiKeyNamesByProvider?.[provider.id] ?? this.data.apiKeyNames,
|
|
264
|
+
apiKeySource: this.data.apiKeySource,
|
|
265
|
+
context,
|
|
266
|
+
defaults: provider.credentialNames,
|
|
267
|
+
inputs,
|
|
268
|
+
providerId: provider.id,
|
|
269
|
+
});
|
|
270
|
+
const graph = context.project?.graphs[context.execution?.graphId];
|
|
271
|
+
if (classifierInputDataValue(inputs, 'images') !== undefined ||
|
|
272
|
+
graph?.connections.some((connection) => connection.inputNodeId === this.id && connection.inputId === 'images')) {
|
|
273
|
+
throw new Error('The separate Images input has been removed. Connect images or an assembled user message to State.');
|
|
274
|
+
}
|
|
275
|
+
const stateInput = classifierInputDataValue(inputs, 'state');
|
|
276
|
+
// First-party providers prepare the Rivet wrapper at their common boundary.
|
|
277
|
+
// Existing custom descriptors still receive the legacy normalized State contract.
|
|
278
|
+
const state = provider.evaluateInput
|
|
279
|
+
? { state: '', stateInput }
|
|
280
|
+
: normalizeClassifierState(stateInput, checkPreparation, provider.maxRequestBytes);
|
|
281
|
+
const legacyBudget = provider.evaluateInput
|
|
282
|
+
? undefined
|
|
283
|
+
: new ClassifierValueBudget(checkPreparation, provider.maxRequestBytes);
|
|
284
|
+
legacyBudget?.inspect(state);
|
|
285
|
+
const modelValue = this.data.useModelInput
|
|
286
|
+
? classifierInputDataValue(inputs, 'model')?.value
|
|
287
|
+
: getStaticModel(this.data.model, provider.defaultModel);
|
|
288
|
+
if (typeof modelValue !== 'string' || modelValue.trim() === '') {
|
|
289
|
+
throw new Error(`${provider.label} model is required.`);
|
|
290
|
+
}
|
|
291
|
+
legacyBudget?.inspect(modelValue);
|
|
292
|
+
const questions = [];
|
|
293
|
+
const flattenWork = { values: 0 };
|
|
294
|
+
const questionInputs = Object.keys(inputs).filter((portId) => /^question\d+$/.test(portId));
|
|
295
|
+
for (const port of questionInputs.sort(compareQuestionPorts)) {
|
|
296
|
+
const input = classifierInputDataValue(inputs, port);
|
|
297
|
+
if (input) {
|
|
298
|
+
// Legacy descriptors do not own the shared preparation boundary.
|
|
299
|
+
legacyBudget?.inspect(input.value);
|
|
300
|
+
flattenQuestions(input.value, questions, checkPreparation, new Set(), 0, flattenWork);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (questions.length === 0)
|
|
304
|
+
throw new Error('Classifier Evaluate requires at least one question.');
|
|
305
|
+
const result = await (provider.evaluateInput ?? provider.evaluate).call(provider, {
|
|
306
|
+
apiKey,
|
|
307
|
+
model: modelValue,
|
|
308
|
+
questions,
|
|
309
|
+
retryOnNon200: this.data.retryOnNon200,
|
|
310
|
+
retryOnNon200CooldownMs: this.data.retryOnNon200CooldownMs,
|
|
311
|
+
retryOnNon200RepeatTimes: this.data.retryOnNon200RepeatTimes,
|
|
312
|
+
signal: context.signal,
|
|
313
|
+
...state,
|
|
314
|
+
timeoutMs,
|
|
315
|
+
deadline,
|
|
316
|
+
});
|
|
317
|
+
// Custom providers still own their asynchronous transport, but no late
|
|
318
|
+
// result may become a successful node output after the original deadline.
|
|
319
|
+
checkPreparation();
|
|
320
|
+
const totalCost = calculateClassifierUsageCost(provider, result.response.usage, {
|
|
321
|
+
requestedModel: modelValue,
|
|
322
|
+
responseModel: result.response.model,
|
|
323
|
+
});
|
|
324
|
+
// The provider's parsed response may also be exposed verbatim through the
|
|
325
|
+
// diagnostic output. Do not mutate its Usage object while adding Rivet's
|
|
326
|
+
// calculated accounting detail.
|
|
327
|
+
const usage = !this.data.outputUsage || totalCost === undefined
|
|
328
|
+
? result.response.usage
|
|
329
|
+
: { ...result.response.usage, totalCost };
|
|
330
|
+
const outputs = {
|
|
331
|
+
['answers']: { type: 'object', value: result.response.answers },
|
|
332
|
+
['usage']: { type: 'object', value: usage },
|
|
333
|
+
['cost']: totalCost === undefined
|
|
334
|
+
? { type: 'control-flow-excluded', value: undefined }
|
|
335
|
+
: { type: 'number', value: totalCost },
|
|
336
|
+
};
|
|
337
|
+
if (this.data.outputRequestBody === true) {
|
|
338
|
+
checkPreparation();
|
|
339
|
+
outputs['requestBody'] = { type: 'object', value: result.requestBody };
|
|
340
|
+
checkPreparation();
|
|
341
|
+
}
|
|
342
|
+
if (this.data.outputResponseBody === true) {
|
|
343
|
+
outputs['responseBody'] = { type: 'object', value: result.responseBody };
|
|
344
|
+
}
|
|
345
|
+
return outputs;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
function getProviderForDisplay(id) {
|
|
349
|
+
try {
|
|
350
|
+
return getClassifierProvider(id);
|
|
351
|
+
}
|
|
352
|
+
catch {
|
|
353
|
+
return classifierProviders[0];
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function getStaticModel(model, defaultModel) {
|
|
357
|
+
return typeof model === 'string' && model.trim() !== '' ? model.trim() : defaultModel;
|
|
358
|
+
}
|
|
359
|
+
/** Shared presentation model for the app's Classifier Evaluate card. */
|
|
360
|
+
export function getClassifierEvaluateBodySections(data) {
|
|
361
|
+
const provider = getProviderForDisplay(data.provider);
|
|
362
|
+
const sections = [
|
|
363
|
+
{
|
|
364
|
+
id: 'configuration',
|
|
365
|
+
fields: [
|
|
366
|
+
{ label: 'Provider', value: provider.label },
|
|
367
|
+
{ label: 'Model', value: data.useModelInput ? 'input' : getStaticModel(data.model, provider.defaultModel) },
|
|
368
|
+
],
|
|
369
|
+
},
|
|
370
|
+
];
|
|
371
|
+
if (data.errorOnNon200 !== false || data.catchRequestFailed || data.retryOnNon200) {
|
|
372
|
+
sections.push({
|
|
373
|
+
id: 'error-behavior',
|
|
374
|
+
fields: [
|
|
375
|
+
...(data.errorOnNon200 !== false ? [{ label: 'Throw on non-2XX', value: 'Enabled' }] : []),
|
|
376
|
+
...(data.catchRequestFailed ? [{ label: 'Catch all failures', value: 'Enabled' }] : []),
|
|
377
|
+
...(data.retryOnNon200
|
|
378
|
+
? [
|
|
379
|
+
{ label: 'Retry on non-200', value: 'Enabled' },
|
|
380
|
+
{ label: 'Repeat times', value: `${normalizeClassifierNon200RetryCount(data.retryOnNon200RepeatTimes)}` },
|
|
381
|
+
{
|
|
382
|
+
label: 'Cooldown, ms',
|
|
383
|
+
value: `${normalizeClassifierNon200RetryCooldownMs(data.retryOnNon200CooldownMs)}`,
|
|
384
|
+
},
|
|
385
|
+
]
|
|
386
|
+
: []),
|
|
387
|
+
],
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
return sections;
|
|
391
|
+
}
|
|
392
|
+
function compareQuestionPorts(left, right) {
|
|
393
|
+
return Number(left.slice('question'.length)) - Number(right.slice('question'.length));
|
|
394
|
+
}
|
|
395
|
+
function getApiKeySourceHelperMessage(data) {
|
|
396
|
+
if (data.apiKeySource === 'input')
|
|
397
|
+
return 'Uses the API Key input port instead of a configured provider key.';
|
|
398
|
+
const provider = getProviderForDisplay(data.provider);
|
|
399
|
+
if (data.apiKeySource === 'classifier-settings')
|
|
400
|
+
return `Uses only Settings > Classifier > ${provider.label} API Key on the executor. A missing key fails without fallback.`;
|
|
401
|
+
return 'Automatic checks the named programmatic setting, then the named environment variable, then the saved Classifier key (default names only). OpenAI also accepts its legacy general key; Jev accepts its legacy plugin key last. Resolution happens on the executor, not in this editor.';
|
|
402
|
+
}
|
|
403
|
+
function flattenQuestions(value, target, check, seen = new Set(), depth = 0, work = { values: 0 }) {
|
|
404
|
+
check();
|
|
405
|
+
if (++work.values > CLASSIFIER_LIMITS.values)
|
|
406
|
+
throw new Error('Question inputs have too many expanded values.');
|
|
407
|
+
if (depth > CLASSIFIER_LIMITS.depth)
|
|
408
|
+
throw new Error('Question inputs exceed the maximum nesting depth of 64.');
|
|
409
|
+
if (Array.isArray(value)) {
|
|
410
|
+
if (seen.has(value))
|
|
411
|
+
throw new Error('Question inputs must not contain circular arrays.');
|
|
412
|
+
seen.add(value);
|
|
413
|
+
if (value.length > CLASSIFIER_LIMITS.questions)
|
|
414
|
+
throw new Error('Classifier Evaluate supports at most 1000 questions.');
|
|
415
|
+
for (const item of classifierArrayValues(value, 'Questions'))
|
|
416
|
+
flattenQuestions(item, target, check, seen, depth + 1, work);
|
|
417
|
+
seen.delete(value);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (typeof value !== 'object' || value === null) {
|
|
421
|
+
throw new Error('Question inputs must contain question definition objects or nested arrays of definitions.');
|
|
422
|
+
}
|
|
423
|
+
if (target.length >= CLASSIFIER_LIMITS.questions)
|
|
424
|
+
throw new Error('Classifier Evaluate supports at most 1000 questions.');
|
|
425
|
+
target.push(value);
|
|
426
|
+
}
|
|
427
|
+
function normalizeTimeout(timeoutMs) {
|
|
428
|
+
return typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
429
|
+
? Math.min(timeoutMs, 600_000)
|
|
430
|
+
: 30_000;
|
|
431
|
+
}
|
|
432
|
+
export const classifierEvaluateNode = nodeDefinition(ClassifierEvaluateNodeImpl, 'Classifier Evaluate');
|