@valerypopoff/rivet2-core 2.12.0 → 2.15.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 +3188 -592
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/api/createProcessor.js +5 -0
- package/dist/esm/api/processSettings.js +1 -0
- package/dist/esm/exports.js +5 -0
- package/dist/esm/model/GraphInputStream.js +70 -0
- package/dist/esm/model/GraphProcessor.js +264 -29
- package/dist/esm/model/MarkdownSanitizationPolicy.js +2 -0
- package/dist/esm/model/NodeExecutionPlanner.js +4 -4
- package/dist/esm/model/NodeImpl.js +1 -1
- package/dist/esm/model/Nodes.js +9 -0
- package/dist/esm/model/ProcessContextBuilder.js +2 -1
- package/dist/esm/model/StreamingOutputWatch.js +5 -0
- package/dist/esm/model/StreamingWatchTopology.js +228 -0
- package/dist/esm/model/classifier/credentials.js +81 -0
- package/dist/esm/model/classifier/migration.js +97 -0
- package/dist/esm/model/classifier/providers.js +347 -0
- package/dist/esm/model/classifier/questionHelpers.js +125 -0
- package/dist/esm/model/classifier/types.js +1 -0
- package/dist/esm/model/nodeBodyMarkdown.js +37 -0
- package/dist/esm/model/nodes/AssembleMessageNode.js +2 -2
- package/dist/esm/model/nodes/AssemblePromptNode.js +46 -17
- package/dist/esm/model/nodes/ClassifierEvaluateNode.js +392 -0
- package/dist/esm/model/nodes/ClassifierQuestionNode.js +569 -0
- package/dist/esm/model/nodes/CodeNewNode.js +33 -9
- package/dist/esm/model/nodes/GraphInputNode.js +15 -19
- package/dist/esm/model/nodes/LLMChatV2Node.js +3 -12
- package/dist/esm/model/nodes/LLMProfileNode.js +6 -12
- package/dist/esm/model/nodes/MatchCaseNode.js +78 -3
- package/dist/esm/model/nodes/MatchCaseNodeBase.js +24 -2
- package/dist/esm/model/nodes/ObjectNode.js +48 -41
- package/dist/esm/model/nodes/PassthroughNode.js +1 -1
- package/dist/esm/model/nodes/ProjectNameNode.js +37 -0
- package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +1 -0
- package/dist/esm/model/nodes/SubGraphNode.js +1 -0
- package/dist/esm/model/nodes/codeOutputInference.js +292 -0
- package/dist/esm/model/nodes/variadicPortIndex.js +1 -1
- package/dist/esm/utils/serialization/serialization.js +4 -0
- package/dist/types/exports.d.ts +6 -0
- package/dist/types/model/EditorDefinition.d.ts +24 -0
- package/dist/types/model/GraphInputStream.d.ts +23 -0
- package/dist/types/model/GraphProcessor.d.ts +3 -0
- package/dist/types/model/MarkdownSanitizationPolicy.d.ts +1 -1
- package/dist/types/model/NodeExecutionPlanner.d.ts +1 -1
- package/dist/types/model/NodeImpl.d.ts +3 -1
- package/dist/types/model/Nodes.d.ts +7 -4
- package/dist/types/model/ProcessContext.d.ts +3 -0
- package/dist/types/model/ProcessContextBuilder.d.ts +2 -1
- package/dist/types/model/Settings.d.ts +4 -0
- package/dist/types/model/StreamingOutputWatch.d.ts +2 -0
- package/dist/types/model/StreamingWatchTopology.d.ts +26 -0
- package/dist/types/model/classifier/credentials.d.ts +24 -0
- package/dist/types/model/classifier/migration.d.ts +18 -0
- package/dist/types/model/classifier/providers.d.ts +78 -0
- package/dist/types/model/classifier/questionHelpers.d.ts +31 -0
- package/dist/types/model/classifier/types.d.ts +58 -0
- package/dist/types/model/nodeBodyMarkdown.d.ts +19 -0
- package/dist/types/model/nodes/AssemblePromptNode.d.ts +4 -3
- package/dist/types/model/nodes/ClassifierEvaluateNode.d.ts +47 -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/GraphInputNode.d.ts +2 -0
- package/dist/types/model/nodes/MatchCaseNode.d.ts +9 -2
- package/dist/types/model/nodes/MatchCaseNodeBase.d.ts +13 -1
- package/dist/types/model/nodes/ObjectNode.d.ts +5 -0
- package/dist/types/model/nodes/ProjectNameNode.d.ts +12 -0
- package/dist/types/model/nodes/codeOutputInference.d.ts +31 -0
- package/dist/types/model/nodes/variadicPortIndex.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
3
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
+
import { formatNodeBodyMarkdownField, formatNodeBodyMarkdownSeparator } from '../nodeBodyMarkdown.js';
|
|
5
|
+
import { getNextVariadicPortIndex } from './variadicPortIndex.js';
|
|
6
|
+
import { resolveClassifierApiKey, } from '../classifier/credentials.js';
|
|
7
|
+
import { assertClassifierEntry, assertClassifierInstructions } from '../classifier/questionHelpers.js';
|
|
8
|
+
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';
|
|
9
|
+
const QUESTION_TYPES = ['object', 'object[]', 'any', 'any[]'];
|
|
10
|
+
export class ClassifierEvaluateNodeImpl extends NodeImpl {
|
|
11
|
+
static create() {
|
|
12
|
+
return {
|
|
13
|
+
type: 'classifierEvaluate',
|
|
14
|
+
title: 'Classifier Evaluate',
|
|
15
|
+
id: nanoid(),
|
|
16
|
+
visualData: { x: 0, y: 0, width: 260 },
|
|
17
|
+
// Keep the default model in the provider descriptor rather than
|
|
18
|
+
// serializing Jev's model into every new node. A future provider then
|
|
19
|
+
// receives its own default when an author changes the Provider field.
|
|
20
|
+
data: { provider: 'jev', timeoutMs: 30_000 },
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
getInputDefinitions(connections) {
|
|
24
|
+
const inputs = [
|
|
25
|
+
{
|
|
26
|
+
id: 'state',
|
|
27
|
+
title: 'State',
|
|
28
|
+
dataType: ['string', 'object', 'object[]', 'any', 'any[]'],
|
|
29
|
+
required: true,
|
|
30
|
+
splitRunBehavior: 'preserve-array',
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
if (this.data.useModelInput) {
|
|
34
|
+
inputs.push({ id: 'model', title: 'Model', dataType: 'string', required: true });
|
|
35
|
+
}
|
|
36
|
+
if (this.data.apiKeySource === 'input') {
|
|
37
|
+
inputs.push({ id: 'apiKey', title: 'API Key', dataType: 'string', required: true });
|
|
38
|
+
}
|
|
39
|
+
const count = getNextVariadicPortIndex(connections, this.chartNode.id, 'question', 'strict-positive');
|
|
40
|
+
for (let index = 1; index <= count; index++) {
|
|
41
|
+
inputs.push({
|
|
42
|
+
id: `question${index}`,
|
|
43
|
+
title: `Question ${index}`,
|
|
44
|
+
dataType: QUESTION_TYPES,
|
|
45
|
+
required: false,
|
|
46
|
+
splitRunBehavior: 'preserve-array',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return inputs;
|
|
50
|
+
}
|
|
51
|
+
getOutputDefinitions() {
|
|
52
|
+
const outputs = [
|
|
53
|
+
{ id: 'answers', title: 'Answers', dataType: 'object' },
|
|
54
|
+
{ id: 'usage', title: 'Usage', dataType: 'object' },
|
|
55
|
+
];
|
|
56
|
+
if (this.data.outputRequestBody === true) {
|
|
57
|
+
outputs.push({ id: 'requestBody', title: 'Classifier request body', dataType: 'object' });
|
|
58
|
+
}
|
|
59
|
+
if (this.data.outputResponseBody === true) {
|
|
60
|
+
outputs.push({ id: 'responseBody', title: 'Classifier response body', dataType: 'object' });
|
|
61
|
+
}
|
|
62
|
+
return outputs;
|
|
63
|
+
}
|
|
64
|
+
getEditors() {
|
|
65
|
+
return [
|
|
66
|
+
{
|
|
67
|
+
type: 'dropdown',
|
|
68
|
+
label: 'Provider',
|
|
69
|
+
dataKey: 'provider',
|
|
70
|
+
defaultValue: 'jev',
|
|
71
|
+
options: classifierProviders.map((provider) => ({ value: provider.id, label: provider.label })),
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: 'string',
|
|
75
|
+
label: 'Model',
|
|
76
|
+
dataKey: 'model',
|
|
77
|
+
useInputToggleDataKey: 'useModelInput',
|
|
78
|
+
placeholder: 'jev-latest',
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: 'segmented',
|
|
82
|
+
label: 'API key source',
|
|
83
|
+
ariaLabel: 'API key source',
|
|
84
|
+
dataKey: 'apiKeySource',
|
|
85
|
+
defaultValue: 'configured',
|
|
86
|
+
options: [
|
|
87
|
+
{ value: 'configured', label: 'Configured key' },
|
|
88
|
+
{ value: 'input', label: 'Input port' },
|
|
89
|
+
],
|
|
90
|
+
helperMessage: getApiKeySourceHelperMessage,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
type: 'custom',
|
|
94
|
+
label: 'Configured API key names',
|
|
95
|
+
customEditorId: 'ClassifierCredentialNames',
|
|
96
|
+
hideIf: (data) => data.apiKeySource === 'input',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
type: 'group',
|
|
100
|
+
label: 'Outputs',
|
|
101
|
+
editors: [
|
|
102
|
+
{
|
|
103
|
+
type: 'toggle',
|
|
104
|
+
label: 'Output usage details',
|
|
105
|
+
dataKey: 'outputUsage',
|
|
106
|
+
helperMessage: 'Adds totalCost to Usage when the selected provider has fixed token pricing. Jev input tokens cost $0.042 / MTok; output tokens are free.',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: 'toggle',
|
|
110
|
+
label: 'Output request body',
|
|
111
|
+
dataKey: 'outputRequestBody',
|
|
112
|
+
helperMessage: 'Adds the exact classifier request JSON body sent to the provider. It excludes authorization headers and Rivet-managed API keys.',
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: 'toggle',
|
|
116
|
+
label: 'Output response body',
|
|
117
|
+
dataKey: 'outputResponseBody',
|
|
118
|
+
helperMessage: 'Adds the complete classifier JSON response returned by the provider after validation. Rivet does not redact or truncate captured content.',
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
type: 'group',
|
|
124
|
+
label: 'Advanced',
|
|
125
|
+
editors: [
|
|
126
|
+
{
|
|
127
|
+
type: 'number',
|
|
128
|
+
label: 'Overall timeout (seconds)',
|
|
129
|
+
dataKey: 'timeoutMs',
|
|
130
|
+
defaultValue: 30,
|
|
131
|
+
storageMultiplier: 1000,
|
|
132
|
+
min: 1,
|
|
133
|
+
max: 600,
|
|
134
|
+
step: 1,
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
type: 'group',
|
|
140
|
+
label: 'Error behavior',
|
|
141
|
+
editors: [
|
|
142
|
+
{
|
|
143
|
+
type: 'toggle',
|
|
144
|
+
label: 'Retry on non-200',
|
|
145
|
+
dataKey: 'retryOnNon200',
|
|
146
|
+
helperMessage: 'Retries non-authentication, non-validation provider HTTP responses. Jev rate-limit retries remain automatic.',
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
type: 'number',
|
|
150
|
+
label: 'Repeat times',
|
|
151
|
+
dataKey: 'retryOnNon200RepeatTimes',
|
|
152
|
+
defaultValue: DEFAULT_CLASSIFIER_RETRY_ON_NON_200_REPEAT_TIMES,
|
|
153
|
+
min: 1,
|
|
154
|
+
step: 1,
|
|
155
|
+
layout: 'inline',
|
|
156
|
+
helperMessage: 'Times to repeat after the initial request',
|
|
157
|
+
hideIf: (data) => !data.retryOnNon200,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
type: 'number',
|
|
161
|
+
label: 'Cooldown, ms',
|
|
162
|
+
dataKey: 'retryOnNon200CooldownMs',
|
|
163
|
+
defaultValue: DEFAULT_CLASSIFIER_RETRY_ON_NON_200_COOLDOWN_MS,
|
|
164
|
+
min: 0,
|
|
165
|
+
step: 1,
|
|
166
|
+
layout: 'inline',
|
|
167
|
+
helperMessage: 'Milliseconds to wait between repeats',
|
|
168
|
+
hideIf: (data) => !data.retryOnNon200,
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
},
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
getBody() {
|
|
175
|
+
const sections = getClassifierEvaluateBodySections(this.data);
|
|
176
|
+
return {
|
|
177
|
+
type: 'markdown',
|
|
178
|
+
disableLinks: true,
|
|
179
|
+
text: sections
|
|
180
|
+
.flatMap((section, index) => [
|
|
181
|
+
...(index === 0 ? [] : [formatNodeBodyMarkdownSeparator()]),
|
|
182
|
+
...section.fields.map((field) => formatNodeBodyMarkdownField(field.label, field.value)),
|
|
183
|
+
])
|
|
184
|
+
.join(''),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
static getUIData() {
|
|
188
|
+
return {
|
|
189
|
+
contextMenuTitle: 'Classifier Evaluate',
|
|
190
|
+
infoBoxTitle: 'Classifier Evaluate',
|
|
191
|
+
infoBoxBody: 'Evaluates one shared state against a batch of independent typed classifier questions in one request.',
|
|
192
|
+
group: ['Classifier'],
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
async process(inputs, context) {
|
|
196
|
+
const provider = getClassifierProvider(this.data.provider);
|
|
197
|
+
if (!provider.browserExecutionSupported && context.executor === 'browser') {
|
|
198
|
+
throw new Error(`${provider.label} cannot run in the Browser executor. Select the Node executor, or run through Studio Server or remote debugging.`);
|
|
199
|
+
}
|
|
200
|
+
const apiKey = resolveClassifierApiKey({
|
|
201
|
+
apiKeyNames: this.data.apiKeyNamesByProvider?.[provider.id] ?? this.data.apiKeyNames,
|
|
202
|
+
apiKeySource: this.data.apiKeySource,
|
|
203
|
+
context,
|
|
204
|
+
defaults: provider.credentialNames,
|
|
205
|
+
inputs,
|
|
206
|
+
providerId: provider.id,
|
|
207
|
+
});
|
|
208
|
+
const state = inputs['state']?.value;
|
|
209
|
+
assertClassifierState(state);
|
|
210
|
+
const modelValue = this.data.useModelInput
|
|
211
|
+
? inputs['model']?.value
|
|
212
|
+
: getStaticModel(this.data.model, provider.defaultModel);
|
|
213
|
+
if (typeof modelValue !== 'string' || modelValue.trim() === '') {
|
|
214
|
+
throw new Error(`${provider.label} model is required.`);
|
|
215
|
+
}
|
|
216
|
+
const questions = [];
|
|
217
|
+
const questionInputs = Object.entries(inputs).filter(([portId]) => /^question\d+$/.test(portId));
|
|
218
|
+
for (const [, input] of questionInputs.sort(compareQuestionPorts)) {
|
|
219
|
+
if (input)
|
|
220
|
+
flattenQuestions(input.value, questions);
|
|
221
|
+
}
|
|
222
|
+
if (questions.length === 0)
|
|
223
|
+
throw new Error('Classifier Evaluate requires at least one question.');
|
|
224
|
+
const ids = new Set();
|
|
225
|
+
for (const question of questions) {
|
|
226
|
+
validateQuestion(question);
|
|
227
|
+
if (ids.has(question.questionId)) {
|
|
228
|
+
throw new Error(`Question ID '${question.questionId}' is duplicated in this evaluation.`);
|
|
229
|
+
}
|
|
230
|
+
ids.add(question.questionId);
|
|
231
|
+
}
|
|
232
|
+
const result = await provider.evaluate({
|
|
233
|
+
apiKey,
|
|
234
|
+
model: modelValue,
|
|
235
|
+
questions,
|
|
236
|
+
retryOnNon200: this.data.retryOnNon200,
|
|
237
|
+
retryOnNon200CooldownMs: this.data.retryOnNon200CooldownMs,
|
|
238
|
+
retryOnNon200RepeatTimes: this.data.retryOnNon200RepeatTimes,
|
|
239
|
+
signal: context.signal,
|
|
240
|
+
state,
|
|
241
|
+
timeoutMs: normalizeTimeout(this.data.timeoutMs),
|
|
242
|
+
});
|
|
243
|
+
const totalCost = this.data.outputUsage ? calculateClassifierUsageCost(provider, result.response.usage) : undefined;
|
|
244
|
+
// The provider's parsed response may also be exposed verbatim through the
|
|
245
|
+
// diagnostic output. Do not mutate its Usage object while adding Rivet's
|
|
246
|
+
// calculated accounting detail.
|
|
247
|
+
const usage = totalCost === undefined ? result.response.usage : { ...result.response.usage, totalCost };
|
|
248
|
+
const outputs = {
|
|
249
|
+
['answers']: { type: 'object', value: result.response.answers },
|
|
250
|
+
['usage']: { type: 'object', value: usage },
|
|
251
|
+
};
|
|
252
|
+
if (this.data.outputRequestBody === true) {
|
|
253
|
+
outputs['requestBody'] = { type: 'object', value: result.requestBody };
|
|
254
|
+
}
|
|
255
|
+
if (this.data.outputResponseBody === true) {
|
|
256
|
+
outputs['responseBody'] = { type: 'object', value: result.responseBody };
|
|
257
|
+
}
|
|
258
|
+
return outputs;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function getProviderForDisplay(id) {
|
|
262
|
+
try {
|
|
263
|
+
return getClassifierProvider(id);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return classifierProviders[0];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function getStaticModel(model, defaultModel) {
|
|
270
|
+
return typeof model === 'string' && model.trim() !== '' ? model.trim() : defaultModel;
|
|
271
|
+
}
|
|
272
|
+
/** Shared presentation model for the app's Classifier Evaluate card. */
|
|
273
|
+
export function getClassifierEvaluateBodySections(data) {
|
|
274
|
+
const provider = getProviderForDisplay(data.provider);
|
|
275
|
+
const sections = [
|
|
276
|
+
{
|
|
277
|
+
id: 'configuration',
|
|
278
|
+
fields: [
|
|
279
|
+
{ label: 'Provider', value: provider.label },
|
|
280
|
+
{ label: 'Model', value: data.useModelInput ? 'input' : getStaticModel(data.model, provider.defaultModel) },
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
];
|
|
284
|
+
if (data.retryOnNon200) {
|
|
285
|
+
sections.push({
|
|
286
|
+
id: 'error-behavior',
|
|
287
|
+
fields: [
|
|
288
|
+
{ label: 'Retry on non-200', value: 'Enabled' },
|
|
289
|
+
{ label: 'Repeat times', value: `${normalizeClassifierNon200RetryCount(data.retryOnNon200RepeatTimes)}` },
|
|
290
|
+
{ label: 'Cooldown, ms', value: `${normalizeClassifierNon200RetryCooldownMs(data.retryOnNon200CooldownMs)}` },
|
|
291
|
+
],
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return sections;
|
|
295
|
+
}
|
|
296
|
+
function compareQuestionPorts([left], [right]) {
|
|
297
|
+
return Number(left.slice('question'.length)) - Number(right.slice('question'.length));
|
|
298
|
+
}
|
|
299
|
+
function getApiKeySourceHelperMessage(data) {
|
|
300
|
+
if (data.apiKeySource === 'input')
|
|
301
|
+
return 'Uses the API Key input port instead of a configured provider key.';
|
|
302
|
+
const provider = getProviderForDisplay(data.provider);
|
|
303
|
+
return data.apiKeyNamesByProvider?.[provider.id] == null && data.apiKeyNames == null
|
|
304
|
+
? `Configured key checks ${provider.credentialNames.programmaticName} or ${provider.credentialNames.environmentVariableName}, including Settings > Classifier > ${provider.label} API Key.`
|
|
305
|
+
: 'Configured key checks the named programmatic setting first, then the named environment variable.';
|
|
306
|
+
}
|
|
307
|
+
function flattenQuestions(value, target) {
|
|
308
|
+
if (Array.isArray(value)) {
|
|
309
|
+
for (const item of value)
|
|
310
|
+
flattenQuestions(item, target);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (typeof value !== 'object' || value === null) {
|
|
314
|
+
throw new Error('Question inputs must contain question definition objects or nested arrays of definitions.');
|
|
315
|
+
}
|
|
316
|
+
target.push(value);
|
|
317
|
+
}
|
|
318
|
+
function validateQuestion(question) {
|
|
319
|
+
if (typeof question.questionId !== 'string' || question.questionId.trim() === '') {
|
|
320
|
+
throw new Error('Every classifier question must have a non-empty Question ID.');
|
|
321
|
+
}
|
|
322
|
+
if (!['choice', 'score', 'noul'].includes(question.type)) {
|
|
323
|
+
throw new Error(`Question '${question.questionId}' has an unsupported type.`);
|
|
324
|
+
}
|
|
325
|
+
assertClassifierInstructions(question.instructions, `Question '${question.questionId}' instructions`);
|
|
326
|
+
if (question.type === 'choice') {
|
|
327
|
+
const criteria = question.criteria;
|
|
328
|
+
if (typeof criteria !== 'object' || criteria === null || Array.isArray(criteria)) {
|
|
329
|
+
throw new Error(`Choice question '${question.questionId}' requires object criteria.`);
|
|
330
|
+
}
|
|
331
|
+
const keys = Object.keys(criteria);
|
|
332
|
+
if (keys.length < 2 || keys.length > 255) {
|
|
333
|
+
throw new Error(`Choice question '${question.questionId}' requires 2 to 255 options.`);
|
|
334
|
+
}
|
|
335
|
+
for (const key of keys)
|
|
336
|
+
assertClassifierEntry(criteria[key], `Question '${question.questionId}' criteria.${key}`);
|
|
337
|
+
}
|
|
338
|
+
else if (question.type === 'score') {
|
|
339
|
+
const criteria = question.criteria;
|
|
340
|
+
if (!Array.isArray(criteria) || criteria.length < 2 || criteria.length > 10) {
|
|
341
|
+
throw new Error(`Score question '${question.questionId}' requires 2 to 10 levels.`);
|
|
342
|
+
}
|
|
343
|
+
criteria.forEach((entry, index) => assertClassifierEntry(entry, `Question '${question.questionId}' criteria[${index}]`));
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
const criteria = question.criteria;
|
|
347
|
+
if (criteria !== undefined) {
|
|
348
|
+
if (typeof criteria !== 'object' ||
|
|
349
|
+
criteria === null ||
|
|
350
|
+
Array.isArray(criteria) ||
|
|
351
|
+
!Object.prototype.hasOwnProperty.call(criteria, 'true') ||
|
|
352
|
+
!Object.prototype.hasOwnProperty.call(criteria, 'false')) {
|
|
353
|
+
throw new Error(`Noul question '${question.questionId}' criteria must contain true and false entries.`);
|
|
354
|
+
}
|
|
355
|
+
assertClassifierEntry(criteria.true, `Question '${question.questionId}' criteria.true`);
|
|
356
|
+
assertClassifierEntry(criteria.false, `Question '${question.questionId}' criteria.false`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function assertClassifierState(value) {
|
|
361
|
+
if (typeof value === 'string')
|
|
362
|
+
return;
|
|
363
|
+
if (typeof value !== 'object' || value === null)
|
|
364
|
+
throw new Error('State must be a string, JSON object, or JSON array.');
|
|
365
|
+
assertJsonCompatible(value, 'State', new Set());
|
|
366
|
+
}
|
|
367
|
+
function assertJsonCompatible(value, label, seen) {
|
|
368
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
369
|
+
return;
|
|
370
|
+
if (typeof value === 'number') {
|
|
371
|
+
if (!Number.isFinite(value))
|
|
372
|
+
throw new Error(`${label} contains a non-finite number.`);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (typeof value !== 'object')
|
|
376
|
+
throw new Error(`${label} is not JSON-compatible.`);
|
|
377
|
+
if (seen.has(value))
|
|
378
|
+
throw new Error(`${label} contains a circular reference.`);
|
|
379
|
+
seen.add(value);
|
|
380
|
+
if (Array.isArray(value))
|
|
381
|
+
value.forEach((item, index) => assertJsonCompatible(item, `${label}[${index}]`, seen));
|
|
382
|
+
else
|
|
383
|
+
for (const [key, item] of Object.entries(value))
|
|
384
|
+
assertJsonCompatible(item, `${label}.${key}`, seen);
|
|
385
|
+
seen.delete(value);
|
|
386
|
+
}
|
|
387
|
+
function normalizeTimeout(timeoutMs) {
|
|
388
|
+
return typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
389
|
+
? Math.min(timeoutMs, 600_000)
|
|
390
|
+
: 30_000;
|
|
391
|
+
}
|
|
392
|
+
export const classifierEvaluateNode = nodeDefinition(ClassifierEvaluateNodeImpl, 'Classifier Evaluate');
|