@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.
Files changed (134) hide show
  1. package/dist/cjs/bundle.cjs +28167 -23834
  2. package/dist/cjs/bundle.cjs.map +4 -4
  3. package/dist/cjs/interpolationRuntime.cjs +22 -3
  4. package/dist/cjs/interpolationRuntime.cjs.map +3 -3
  5. package/dist/cjs/interpolationSyntax.cjs +9 -5
  6. package/dist/cjs/interpolationSyntax.cjs.map +2 -2
  7. package/dist/esm/api/createProcessor.js +10 -2
  8. package/dist/esm/api/processSettings.js +1 -0
  9. package/dist/esm/exports.js +6 -0
  10. package/dist/esm/model/GraphBoundaryCache.js +2 -1
  11. package/dist/esm/model/GraphProcessor.js +278 -22
  12. package/dist/esm/model/MarkdownSanitizationPolicy.js +2 -0
  13. package/dist/esm/model/NodeImpl.js +1 -1
  14. package/dist/esm/model/Nodes.js +16 -0
  15. package/dist/esm/model/ProjectBundle.js +235 -0
  16. package/dist/esm/model/SplitRunProcessor.js +28 -7
  17. package/dist/esm/model/StreamingWatchTopology.js +211 -64
  18. package/dist/esm/model/SubgraphProjectTarget.js +7 -0
  19. package/dist/esm/model/chat-v2/aiSdkBridge.js +7 -5
  20. package/dist/esm/model/chat-v2/chatV2Pipeline.js +13 -8
  21. package/dist/esm/model/chat-v2/chatV2RuntimeOptions.js +19 -1
  22. package/dist/esm/model/chat-v2/chatV2Shared.js +2 -1
  23. package/dist/esm/model/chat-v2/llmChatV2Body.js +11 -8
  24. package/dist/esm/model/chat-v2/llmChatV2NodeData.js +2 -0
  25. package/dist/esm/model/chat-v2/llmChatV2NodeEditors.js +15 -1
  26. package/dist/esm/model/chat-v2/llmChatV2NodeMigration.js +22 -2
  27. package/dist/esm/model/chat-v2/llmProfile.js +3 -0
  28. package/dist/esm/model/chat-v2/llmProfileBody.js +2 -2
  29. package/dist/esm/model/chat-v2/llmProfileFieldRegistry.js +1 -1
  30. package/dist/esm/model/chat-v2/llmProfileNodeRuntime.js +1 -1
  31. package/dist/esm/model/chat-v2/temperature.js +9 -0
  32. package/dist/esm/model/classifier/credentials.js +107 -0
  33. package/dist/esm/model/classifier/images.js +176 -0
  34. package/dist/esm/model/classifier/json.js +57 -0
  35. package/dist/esm/model/classifier/limits.js +270 -0
  36. package/dist/esm/model/classifier/migration.js +97 -0
  37. package/dist/esm/model/classifier/openai.js +118 -0
  38. package/dist/esm/model/classifier/preparation.js +74 -0
  39. package/dist/esm/model/classifier/providers.js +422 -0
  40. package/dist/esm/model/classifier/questionHelpers.js +109 -0
  41. package/dist/esm/model/classifier/questions.js +53 -0
  42. package/dist/esm/model/classifier/response.js +75 -0
  43. package/dist/esm/model/classifier/state.js +204 -0
  44. package/dist/esm/model/classifier/types.js +1 -0
  45. package/dist/esm/model/nodeBodyMarkdown.js +37 -0
  46. package/dist/esm/model/nodeRunFailure.js +47 -0
  47. package/dist/esm/model/nodes/CatchStreamingChunksNode.js +42 -0
  48. package/dist/esm/model/nodes/ClassifierEvaluateNode.js +432 -0
  49. package/dist/esm/model/nodes/ClassifierQuestionNode.js +613 -0
  50. package/dist/esm/model/nodes/CodeNewNode.js +33 -9
  51. package/dist/esm/model/nodes/GetGlobalNode.js +2 -2
  52. package/dist/esm/model/nodes/HttpCallNode.js +44 -75
  53. package/dist/esm/model/nodes/ImageNode.js +44 -3
  54. package/dist/esm/model/nodes/LLMChatV2Node.js +27 -15
  55. package/dist/esm/model/nodes/LLMProfileNode.js +6 -12
  56. package/dist/esm/model/nodes/MatchCaseNode.js +76 -1
  57. package/dist/esm/model/nodes/MatchCaseNodeBase.js +24 -2
  58. package/dist/esm/model/nodes/ObjectNode.js +68 -50
  59. package/dist/esm/model/nodes/PassthroughNode.js +1 -1
  60. package/dist/esm/model/nodes/PromptNode.js +2 -1
  61. package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +4 -1
  62. package/dist/esm/model/nodes/SetGlobalNode.js +5 -3
  63. package/dist/esm/model/nodes/StopWatchingStreamingOutputNode.js +8 -8
  64. package/dist/esm/model/nodes/StreamValueNode.js +38 -0
  65. package/dist/esm/model/nodes/SubGraphNode.js +196 -35
  66. package/dist/esm/model/nodes/ToBase64Node.js +106 -0
  67. package/dist/esm/model/nodes/WatchStreamingOutputNode.js +7 -7
  68. package/dist/esm/model/nodes/codeOutputInference.js +292 -0
  69. package/dist/esm/recording/ExecutionRecorder.js +26 -0
  70. package/dist/esm/utils/base64.js +18 -15
  71. package/dist/esm/utils/errors.js +41 -0
  72. package/dist/esm/utils/interpolation.js +44 -22
  73. package/dist/esm/utils/interpolationSyntax.js +15 -6
  74. package/dist/esm/utils/serialization/serialization.js +7 -2
  75. package/dist/esm/utils/serialization/serializationUtils.js +6 -0
  76. package/dist/esm/utils/serialization/serialization_v4.js +15 -3
  77. package/dist/types/api/createProcessor.d.ts +7 -1
  78. package/dist/types/exports.d.ts +6 -0
  79. package/dist/types/model/CycleDetector.d.ts +1 -2
  80. package/dist/types/model/EditorDefinition.d.ts +24 -0
  81. package/dist/types/model/ExecutorProtocol.d.ts +2 -0
  82. package/dist/types/model/GraphBoundaryCache.d.ts +2 -0
  83. package/dist/types/model/GraphProcessor.d.ts +1 -0
  84. package/dist/types/model/MarkdownSanitizationPolicy.d.ts +1 -1
  85. package/dist/types/model/NodeImpl.d.ts +3 -1
  86. package/dist/types/model/Nodes.d.ts +10 -4
  87. package/dist/types/model/ProcessContext.d.ts +12 -0
  88. package/dist/types/model/ProjectBundle.d.ts +54 -0
  89. package/dist/types/model/ProjectReferenceLoader.d.ts +3 -0
  90. package/dist/types/model/Settings.d.ts +4 -0
  91. package/dist/types/model/StreamingWatchTopology.d.ts +17 -1
  92. package/dist/types/model/SubgraphProjectTarget.d.ts +38 -0
  93. package/dist/types/model/chat-v2/chatV2Shared.d.ts +1 -1
  94. package/dist/types/model/chat-v2/llmChatV2NodeData.d.ts +3 -1
  95. package/dist/types/model/chat-v2/llmChatV2NodeMigration.d.ts +3 -1
  96. package/dist/types/model/chat-v2/llmProfileFieldRegistry.d.ts +6 -6
  97. package/dist/types/model/chat-v2/temperature.d.ts +2 -0
  98. package/dist/types/model/classifier/credentials.d.ts +26 -0
  99. package/dist/types/model/classifier/images.d.ts +18 -0
  100. package/dist/types/model/classifier/json.d.ts +19 -0
  101. package/dist/types/model/classifier/limits.d.ts +42 -0
  102. package/dist/types/model/classifier/migration.d.ts +18 -0
  103. package/dist/types/model/classifier/openai.d.ts +5 -0
  104. package/dist/types/model/classifier/preparation.d.ts +16 -0
  105. package/dist/types/model/classifier/providers.d.ts +136 -0
  106. package/dist/types/model/classifier/questionHelpers.d.ts +33 -0
  107. package/dist/types/model/classifier/questions.d.ts +4 -0
  108. package/dist/types/model/classifier/response.d.ts +4 -0
  109. package/dist/types/model/classifier/state.d.ts +37 -0
  110. package/dist/types/model/classifier/types.d.ts +60 -0
  111. package/dist/types/model/nodeBodyMarkdown.d.ts +19 -0
  112. package/dist/types/model/nodeRunFailure.d.ts +13 -0
  113. package/dist/types/model/nodes/CatchStreamingChunksNode.d.ts +20 -0
  114. package/dist/types/model/nodes/ClassifierEvaluateNode.d.ts +50 -0
  115. package/dist/types/model/nodes/ClassifierQuestionNode.d.ts +60 -0
  116. package/dist/types/model/nodes/CodeNewNode.d.ts +6 -0
  117. package/dist/types/model/nodes/GetGlobalNode.d.ts +7 -0
  118. package/dist/types/model/nodes/ImageNode.d.ts +4 -0
  119. package/dist/types/model/nodes/LLMChatV2Node.d.ts +1 -0
  120. package/dist/types/model/nodes/MatchCaseNode.d.ts +8 -1
  121. package/dist/types/model/nodes/MatchCaseNodeBase.d.ts +13 -1
  122. package/dist/types/model/nodes/ObjectNode.d.ts +6 -0
  123. package/dist/types/model/nodes/StreamValueNode.d.ts +14 -0
  124. package/dist/types/model/nodes/SubGraphNode.d.ts +21 -2
  125. package/dist/types/model/nodes/ToBase64Node.d.ts +12 -0
  126. package/dist/types/model/nodes/codeOutputInference.d.ts +31 -0
  127. package/dist/types/recording/ExecutionRecorder.d.ts +8 -2
  128. package/dist/types/utils/base64.d.ts +1 -1
  129. package/dist/types/utils/errors.d.ts +4 -0
  130. package/dist/types/utils/interpolation.d.ts +13 -2
  131. package/dist/types/utils/interpolationSyntax.d.ts +1 -1
  132. package/dist/types/utils/serialization/serialization.d.ts +3 -1
  133. package/dist/types/utils/serialization/serializationUtils.d.ts +1 -0
  134. package/package.json +1 -1
@@ -0,0 +1,613 @@
1
+ import { nanoid } from 'nanoid/non-secure';
2
+ import { nodeDefinition } from '../NodeDefinition.js';
3
+ import { NodeImpl } from '../NodeImpl.js';
4
+ import { formatNodeBodyMarkdownField, formatNodeBodyMarkdownSeparator, formatNodeBodyMarkdownTextRow, } from '../nodeBodyMarkdown.js';
5
+ import { ClassifierQuestionPreparation } from '../classifier/preparation.js';
6
+ import { assertClassifierJson } from '../classifier/json.js';
7
+ import { abbreviate, assertClassifierEntry, assertClassifierInstructions, getCriteriaInputDefinition, getInstructionsInputDefinition, getInterpolationInputDefinitions, getQuestionInputValue, getStringInput, getStructuredInput, interpolateQuestionText, requireQuestionId, resolveAuthoredClassifierEntry, } from '../classifier/questionHelpers.js';
8
+ const defaultOptions = () => [
9
+ { key: '', value: '' },
10
+ { key: '', value: '' },
11
+ ];
12
+ const defaultLevels = () => ['', ''];
13
+ const defaultScoreCriteria = () => [createScoreCriterion(), createScoreCriterion()];
14
+ const entryTypeOptions = [
15
+ { value: 'text', label: 'Text' },
16
+ { value: 'lines', label: 'List of lines' },
17
+ { value: 'object', label: 'Object' },
18
+ ];
19
+ function createScoreCriterion() {
20
+ return { id: nanoid(), type: 'text', text: '', lines: [''], objectTemplate: '{}' };
21
+ }
22
+ function createChoiceCriterion() {
23
+ return { id: nanoid(), key: '', text: '', lines: [''], objectTemplate: '{}' };
24
+ }
25
+ export class ClassifierQuestionNodeImpl extends NodeImpl {
26
+ static create() {
27
+ return {
28
+ type: 'classifierQuestion',
29
+ title: 'Classifier Question',
30
+ id: nanoid(),
31
+ visualData: { x: 0, y: 0, width: 280 },
32
+ data: {
33
+ questionType: 'choice',
34
+ questionId: 'choice',
35
+ instructions: '',
36
+ instructionsType: 'text',
37
+ instructionsLines: [''],
38
+ instructionsObjectTemplate: '{}',
39
+ criteriaType: 'text',
40
+ options: defaultOptions(),
41
+ choiceCriteria: [createChoiceCriterion(), createChoiceCriterion()],
42
+ scoreCriteria: defaultScoreCriteria(),
43
+ noulTrueCriteria: '',
44
+ noulFalseCriteria: '',
45
+ noulTrueCriteriaLines: [''],
46
+ noulFalseCriteriaLines: [''],
47
+ noulTrueCriteriaObjectTemplate: '{}',
48
+ noulFalseCriteriaObjectTemplate: '{}',
49
+ },
50
+ };
51
+ }
52
+ getInputDefinitions() {
53
+ const data = this.data;
54
+ const inputs = [];
55
+ if (data.useInstructionsInput)
56
+ inputs.push(getInstructionsInputDefinition());
57
+ if (data.useCriteriaInput) {
58
+ inputs.push(getCriteriaInputDefinition(getCriteriaInputTypes(data.questionType)));
59
+ }
60
+ else if (data.questionType === 'noul') {
61
+ const criteriaType = getCriteriaType(data);
62
+ if (data.useNoulTrueCriteriaInput)
63
+ inputs.push(getNoulCriteriaInputDefinition('criteriaTrue', 'true', criteriaType));
64
+ if (data.useNoulFalseCriteriaInput)
65
+ inputs.push(getNoulCriteriaInputDefinition('criteriaFalse', 'false', criteriaType));
66
+ }
67
+ const templates = data.useInstructionsInput
68
+ ? []
69
+ : getEntryTemplates(data.instructionsType, data.instructions, data.instructionsLines, data.instructionsObjectTemplate);
70
+ if (!data.useCriteriaInput) {
71
+ if (data.questionType === 'choice') {
72
+ if (getCriteriaType(data) === 'text')
73
+ templates.push(...(data.options ?? []).map((option) => option.value));
74
+ else {
75
+ for (const criterion of getChoiceCriteria(data)) {
76
+ templates.push(...getEntryTemplates(getCriteriaType(data), criterion.text, criterion.lines, criterion.objectTemplate));
77
+ }
78
+ }
79
+ }
80
+ else if (data.questionType === 'score') {
81
+ for (const criterion of getScoreCriteria(data)) {
82
+ templates.push(...getEntryTemplates(data.criteriaType ?? criterion.type, criterion.text, criterion.lines, criterion.objectTemplate));
83
+ }
84
+ }
85
+ else {
86
+ if (!data.useNoulTrueCriteriaInput)
87
+ templates.push(...getNoulCriteriaTemplates(data, 'true'));
88
+ if (!data.useNoulFalseCriteriaInput)
89
+ templates.push(...getNoulCriteriaTemplates(data, 'false'));
90
+ }
91
+ }
92
+ inputs.push(...getInterpolationInputDefinitions(templates, new Set(inputs.map((input) => input.id))));
93
+ return inputs;
94
+ }
95
+ getOutputDefinitions() {
96
+ return [{ id: 'question', title: 'Question', dataType: 'object' }];
97
+ }
98
+ getEditors() {
99
+ return [
100
+ {
101
+ type: 'segmented',
102
+ label: 'Question type',
103
+ ariaLabel: 'Question type',
104
+ dataKey: 'questionType',
105
+ options: [
106
+ { value: 'noul', label: 'Noul' },
107
+ { value: 'choice', label: 'Choice' },
108
+ { value: 'score', label: 'Score' },
109
+ ],
110
+ defaultValue: 'choice',
111
+ allowOptionWrap: false,
112
+ },
113
+ { type: 'string', label: 'Question ID', dataKey: 'questionId' },
114
+ {
115
+ type: 'group',
116
+ label: 'Instructions',
117
+ presentation: 'section',
118
+ editors: [
119
+ {
120
+ type: 'dropdown',
121
+ label: '',
122
+ ariaLabel: 'Instructions type',
123
+ dataKey: 'instructionsType',
124
+ options: entryTypeOptions,
125
+ defaultValue: 'text',
126
+ },
127
+ {
128
+ type: 'code',
129
+ label: '',
130
+ dataKey: 'instructions',
131
+ useInputToggleDataKey: 'useInstructionsInput',
132
+ language: 'prompt-interpolation-markdown',
133
+ theme: 'prompt-interpolation',
134
+ height: 100,
135
+ hideIf: (data) => (data.instructionsType ?? 'text') !== 'text',
136
+ },
137
+ {
138
+ type: 'stringList',
139
+ label: '',
140
+ dataKey: 'instructionsLines',
141
+ useInputToggleDataKey: 'useInstructionsInput',
142
+ placeholder: 'Instruction line',
143
+ highlightInterpolationTokens: true,
144
+ inputFontFamily: 'monospace',
145
+ minimumItems: 1,
146
+ reorderable: true,
147
+ hideIf: (data) => data.instructionsType !== 'lines',
148
+ },
149
+ {
150
+ type: 'code',
151
+ label: '',
152
+ dataKey: 'instructionsObjectTemplate',
153
+ useInputToggleDataKey: 'useInstructionsInput',
154
+ language: 'json',
155
+ interpolationSyntax: 'json-template',
156
+ theme: 'prompt-interpolation',
157
+ enableFolding: true,
158
+ defaultValue: '{}',
159
+ height: 200,
160
+ hideIf: (data) => data.instructionsType !== 'object',
161
+ },
162
+ ],
163
+ },
164
+ {
165
+ type: 'group',
166
+ label: 'Criteria',
167
+ presentation: 'section',
168
+ hideIf: (data) => data.questionType === 'noul' && Boolean(data.useCriteriaInput),
169
+ editors: [
170
+ {
171
+ type: 'dropdown',
172
+ label: '',
173
+ ariaLabel: 'Criteria type',
174
+ dataKey: 'criteriaType',
175
+ options: entryTypeOptions,
176
+ defaultValue: 'text',
177
+ },
178
+ {
179
+ type: 'keyValuePair',
180
+ label: '',
181
+ dataKey: 'options',
182
+ useInputToggleDataKey: 'useCriteriaInput',
183
+ keyPlaceholder: 'Name',
184
+ valuePlaceholder: 'Optional description',
185
+ itemLabel: 'criterion',
186
+ reorderable: true,
187
+ highlightInterpolationTokens: true,
188
+ hideIf: (data) => data.questionType !== 'choice' || getCriteriaType(data) !== 'text',
189
+ },
190
+ {
191
+ type: 'custom',
192
+ customEditorId: 'ClassifierChoiceCriteria',
193
+ label: '',
194
+ dataKey: 'choiceCriteria',
195
+ useInputToggleDataKey: 'useCriteriaInput',
196
+ hideIf: (data) => data.questionType !== 'choice' || getCriteriaType(data) === 'text',
197
+ },
198
+ {
199
+ type: 'custom',
200
+ customEditorId: 'ClassifierScoreCriteria',
201
+ label: '',
202
+ dataKey: 'scoreCriteria',
203
+ useInputToggleDataKey: 'useCriteriaInput',
204
+ hideIf: (data) => data.questionType !== 'score',
205
+ },
206
+ {
207
+ type: 'code',
208
+ label: 'true',
209
+ dataKey: 'noulTrueCriteria',
210
+ useInputToggleDataKey: 'useNoulTrueCriteriaInput',
211
+ language: 'prompt-interpolation-markdown',
212
+ theme: 'prompt-interpolation',
213
+ height: 80,
214
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'text',
215
+ },
216
+ {
217
+ type: 'code',
218
+ label: 'false',
219
+ dataKey: 'noulFalseCriteria',
220
+ useInputToggleDataKey: 'useNoulFalseCriteriaInput',
221
+ language: 'prompt-interpolation-markdown',
222
+ theme: 'prompt-interpolation',
223
+ height: 80,
224
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'text',
225
+ },
226
+ {
227
+ type: 'stringList',
228
+ label: 'true',
229
+ dataKey: 'noulTrueCriteriaLines',
230
+ useInputToggleDataKey: 'useNoulTrueCriteriaInput',
231
+ placeholder: 'Criterion line',
232
+ highlightInterpolationTokens: true,
233
+ inputFontFamily: 'monospace',
234
+ minimumItems: 1,
235
+ reorderable: true,
236
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'lines',
237
+ },
238
+ {
239
+ type: 'stringList',
240
+ label: 'false',
241
+ dataKey: 'noulFalseCriteriaLines',
242
+ useInputToggleDataKey: 'useNoulFalseCriteriaInput',
243
+ placeholder: 'Criterion line',
244
+ highlightInterpolationTokens: true,
245
+ inputFontFamily: 'monospace',
246
+ minimumItems: 1,
247
+ reorderable: true,
248
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'lines',
249
+ },
250
+ {
251
+ type: 'code',
252
+ label: 'true',
253
+ dataKey: 'noulTrueCriteriaObjectTemplate',
254
+ useInputToggleDataKey: 'useNoulTrueCriteriaInput',
255
+ language: 'json',
256
+ interpolationSyntax: 'json-template',
257
+ theme: 'prompt-interpolation',
258
+ enableFolding: true,
259
+ defaultValue: '{}',
260
+ height: 200,
261
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'object',
262
+ },
263
+ {
264
+ type: 'code',
265
+ label: 'false',
266
+ dataKey: 'noulFalseCriteriaObjectTemplate',
267
+ useInputToggleDataKey: 'useNoulFalseCriteriaInput',
268
+ language: 'json',
269
+ interpolationSyntax: 'json-template',
270
+ theme: 'prompt-interpolation',
271
+ enableFolding: true,
272
+ defaultValue: '{}',
273
+ height: 200,
274
+ hideIf: (data) => data.questionType !== 'noul' || getCriteriaType(data) !== 'object',
275
+ },
276
+ ],
277
+ },
278
+ ];
279
+ }
280
+ getBody() {
281
+ const sections = getClassifierQuestionBodySections(this.data);
282
+ const identity = sections[0];
283
+ const instructions = sections[1];
284
+ const criteria = sections[2];
285
+ return {
286
+ type: 'markdown',
287
+ disableLinks: true,
288
+ text: [
289
+ ...identity.fields.map((field) => formatNodeBodyMarkdownField(field.label, field.value)),
290
+ formatNodeBodyMarkdownSeparator(),
291
+ ...(instructions.summary === undefined
292
+ ? instructions.fields.map((field) => formatNodeBodyMarkdownField(field.label, field.value))
293
+ : [formatNodeBodyMarkdownTextRow(instructions.summary)]),
294
+ formatNodeBodyMarkdownSeparator(),
295
+ ...criteria.fields.map((field) => formatNodeBodyMarkdownField(field.label, field.value)),
296
+ ].join(''),
297
+ };
298
+ }
299
+ static getUIData() {
300
+ return {
301
+ contextMenuTitle: 'Classifier Question',
302
+ infoBoxTitle: 'Classifier Question',
303
+ infoBoxBody: 'Builds one typed classifier question without making a network request.',
304
+ group: ['Classifier'],
305
+ };
306
+ }
307
+ async process(inputs, context) {
308
+ const preparation = new ClassifierQuestionPreparation(context);
309
+ const data = this.data;
310
+ preparation.capture(data.questionId);
311
+ // Reject impossible cardinalities before resolving even Instructions.
312
+ const criteria = data.useCriteriaInput ? getQuestionInputValue(inputs, 'criteria', 'Criteria') : undefined;
313
+ if (data.questionType === 'score') {
314
+ const count = data.useCriteriaInput
315
+ ? Array.isArray(criteria)
316
+ ? criteria.length
317
+ : 0
318
+ : (data.scoreCriteria ?? data.levels ?? defaultLevels()).length;
319
+ if (count < 2 || count > 10)
320
+ throw new Error('Score questions require 2 to 10 levels.');
321
+ }
322
+ else if (data.questionType === 'choice') {
323
+ const count = data.useCriteriaInput
324
+ ? criteria && typeof criteria === 'object' && !Array.isArray(criteria)
325
+ ? Object.keys(criteria).length
326
+ : 0
327
+ : (getCriteriaType(data) === 'text'
328
+ ? data.options ?? defaultOptions()
329
+ : data.choiceCriteria ?? data.options ?? defaultOptions()).length;
330
+ if (count < 2 || count > 255)
331
+ throw new Error('Choice questions require 2 to 255 options.');
332
+ }
333
+ if (data.useInstructionsInput)
334
+ preparation.capture(getQuestionInputValue(inputs, 'instructions', 'Instructions'));
335
+ const questionId = requireQuestionId(data.questionId);
336
+ const instructions = data.useInstructionsInput
337
+ ? getStructuredInput(inputs, 'instructions', 'Instructions')
338
+ : resolveAuthoredClassifierEntry({
339
+ type: data.instructionsType,
340
+ text: data.instructions,
341
+ lines: data.instructionsLines,
342
+ objectTemplate: data.instructionsObjectTemplate,
343
+ inputs,
344
+ context,
345
+ label: 'Instructions',
346
+ preparation,
347
+ });
348
+ assertClassifierInstructions(instructions);
349
+ const question = createQuestionDefinition(data, inputs, context, questionId, instructions, preparation);
350
+ assertClassifierJson(question, 'Question', false, preparation.check);
351
+ preparation.check();
352
+ return { ['question']: { type: 'object', value: question } };
353
+ }
354
+ }
355
+ function getCriteriaInputTypes(questionType) {
356
+ return questionType === 'score' ? ['object[]', 'any', 'any[]'] : ['object', 'any'];
357
+ }
358
+ function getNoulCriteriaInputDefinition(id, title, criteriaType) {
359
+ return {
360
+ id: id,
361
+ title,
362
+ dataType: criteriaType === 'text' ? 'string' : ['string', 'object', 'object[]', 'any', 'any[]'],
363
+ required: true,
364
+ splitRunBehavior: criteriaType === 'text' ? undefined : 'preserve-array',
365
+ description: `Description for the Noul ${title} criterion.`,
366
+ };
367
+ }
368
+ function getQuestionTypeLabel(questionType) {
369
+ return questionType === 'noul' ? 'Noul' : questionType[0].toUpperCase() + questionType.slice(1);
370
+ }
371
+ /**
372
+ * Shared presentation model for the app's Classifier card. Keep this data-only
373
+ * so the Core Markdown fallback and the app's LLM-style React body cannot
374
+ * disagree about what a saved question represents.
375
+ */
376
+ export function getClassifierQuestionBodySections(data) {
377
+ const instructions = getInstructionsBodySection(data);
378
+ return [
379
+ {
380
+ id: 'identity',
381
+ fields: [
382
+ { label: 'Type', value: getQuestionTypeLabel(data.questionType) },
383
+ { label: 'ID', value: data.questionId || '(required)' },
384
+ ],
385
+ },
386
+ instructions,
387
+ {
388
+ id: 'criteria',
389
+ fields: [{ label: 'Criteria', value: data.useCriteriaInput ? 'input' : getCriteriaSummary(data) }],
390
+ },
391
+ ];
392
+ }
393
+ function getCriteriaSummary(data) {
394
+ if (data.questionType === 'choice') {
395
+ return `${getCriteriaType(data) === 'text' ? (data.options ?? []).length : getChoiceCriteria(data).length} choices`;
396
+ }
397
+ if (data.questionType === 'score')
398
+ return `${getScoreCriteria(data).length} ordered criteria`;
399
+ const count = Number(data.useNoulTrueCriteriaInput || hasAuthoredNoulCriteria(data, 'true')) +
400
+ Number(data.useNoulFalseCriteriaInput || hasAuthoredNoulCriteria(data, 'false'));
401
+ return `${count} descriptions`;
402
+ }
403
+ function getInstructionsBodySection(data) {
404
+ if (data.useInstructionsInput)
405
+ return { id: 'instructions', fields: [{ label: 'Instructions', value: 'input' }] };
406
+ if (data.instructionsType === 'lines') {
407
+ return {
408
+ id: 'instructions',
409
+ fields: [{ label: 'Instructions', value: `${Math.max(1, data.instructionsLines?.length ?? 0)} lines` }],
410
+ };
411
+ }
412
+ if (data.instructionsType === 'object')
413
+ return { id: 'instructions', fields: [{ label: 'Instructions', value: 'object' }] };
414
+ return { id: 'instructions', fields: [], summary: abbreviate(data.instructions) || '(required)' };
415
+ }
416
+ function createQuestionDefinition(data, inputs, context, questionId, instructions, preparation) {
417
+ if (data.questionType === 'choice') {
418
+ return createChoiceQuestion(data, inputs, context, questionId, instructions, preparation);
419
+ }
420
+ if (data.questionType === 'score') {
421
+ return createScoreQuestion(data, inputs, context, questionId, instructions, preparation);
422
+ }
423
+ return createNoulQuestion(data, inputs, context, questionId, instructions, preparation);
424
+ }
425
+ function createChoiceQuestion(data, inputs, context, questionId, instructions, preparation) {
426
+ let criteria;
427
+ if (data.useCriteriaInput) {
428
+ preparation.capture(getQuestionInputValue(inputs, 'criteria', 'Criteria'));
429
+ const value = getStructuredInput(inputs, 'criteria', 'Criteria');
430
+ if (value === null || Array.isArray(value) || typeof value !== 'object') {
431
+ throw new Error('Choice criteria must be an object.');
432
+ }
433
+ criteria = Object.create(null);
434
+ for (const [key, entry] of Object.entries(value)) {
435
+ Object.defineProperty(criteria, key, { enumerable: true, value: entry });
436
+ }
437
+ }
438
+ else {
439
+ const criteriaType = getCriteriaType(data);
440
+ const options = criteriaType === 'text' ? data.options ?? defaultOptions() : getChoiceCriteria(data);
441
+ if (options.length < 2 || options.length > 255)
442
+ throw new Error('Choice questions require 2 to 255 options.');
443
+ criteria = Object.create(null);
444
+ for (const option of options) {
445
+ preparation.capture(option.key);
446
+ if (option.key.trim() === '')
447
+ throw new Error('Choice criterion names must not be empty.');
448
+ if (Object.prototype.hasOwnProperty.call(criteria, option.key)) {
449
+ throw new Error(`Choice criterion name '${option.key}' is duplicated.`);
450
+ }
451
+ Object.defineProperty(criteria, option.key, {
452
+ enumerable: true,
453
+ value: criteriaType === 'text'
454
+ ? interpolateQuestionText(option.value, inputs, context, preparation) || null
455
+ : resolveAuthoredClassifierEntry({
456
+ type: criteriaType,
457
+ text: option.text,
458
+ lines: option.lines,
459
+ objectTemplate: option.objectTemplate,
460
+ inputs,
461
+ context,
462
+ label: `Criteria.${option.key}`,
463
+ preparation,
464
+ }),
465
+ });
466
+ }
467
+ }
468
+ const keys = Object.keys(criteria);
469
+ if (keys.length < 2 || keys.length > 255)
470
+ throw new Error('Choice questions require 2 to 255 unique options.');
471
+ for (const [key, entry] of Object.entries(criteria))
472
+ assertClassifierEntry(entry, `Criteria.${key}`);
473
+ return { questionId, type: 'choice', instructions, criteria };
474
+ }
475
+ function createScoreQuestion(data, inputs, context, questionId, instructions, preparation) {
476
+ let criteria;
477
+ if (data.useCriteriaInput) {
478
+ preparation.capture(getQuestionInputValue(inputs, 'criteria', 'Criteria'));
479
+ const value = getStructuredInput(inputs, 'criteria', 'Criteria');
480
+ if (!Array.isArray(value))
481
+ throw new Error('Score criteria must be an array.');
482
+ criteria = value;
483
+ }
484
+ else {
485
+ criteria = getScoreCriteria(data).map((criterion, index) => resolveAuthoredClassifierEntry({
486
+ type: data.criteriaType ?? criterion.type,
487
+ text: criterion.text,
488
+ lines: criterion.lines,
489
+ objectTemplate: criterion.objectTemplate,
490
+ inputs,
491
+ context,
492
+ label: `Criteria[${index}]`,
493
+ preparation,
494
+ }));
495
+ }
496
+ if (criteria.length < 2 || criteria.length > 10)
497
+ throw new Error('Score questions require 2 to 10 levels.');
498
+ criteria.forEach((entry, index) => assertClassifierEntry(entry, `Criteria[${index}]`));
499
+ return { questionId, type: 'score', instructions, criteria };
500
+ }
501
+ function createNoulQuestion(data, inputs, context, questionId, instructions, preparation) {
502
+ let criteria;
503
+ if (data.useCriteriaInput) {
504
+ preparation.capture(getQuestionInputValue(inputs, 'criteria', 'Criteria'));
505
+ const value = getStructuredInput(inputs, 'criteria', 'Criteria');
506
+ if (value === null ||
507
+ Array.isArray(value) ||
508
+ typeof value !== 'object' ||
509
+ !Object.prototype.hasOwnProperty.call(value, 'true') ||
510
+ !Object.prototype.hasOwnProperty.call(value, 'false')) {
511
+ throw new Error('Noul criteria must contain both true and false entries.');
512
+ }
513
+ criteria = { true: value.true, false: value.false };
514
+ }
515
+ else {
516
+ const trueCriteria = resolveNoulCriteria(data, 'true', inputs, context, preparation);
517
+ const falseCriteria = resolveNoulCriteria(data, 'false', inputs, context, preparation);
518
+ if (hasClassifierCriteriaValue(trueCriteria) || hasClassifierCriteriaValue(falseCriteria)) {
519
+ if (!hasClassifierCriteriaValue(trueCriteria) || !hasClassifierCriteriaValue(falseCriteria)) {
520
+ throw new Error('Provide both true and false criteria, or neither.');
521
+ }
522
+ criteria = {
523
+ true: trueCriteria,
524
+ false: falseCriteria,
525
+ };
526
+ }
527
+ }
528
+ if (criteria) {
529
+ assertClassifierEntry(criteria.true, 'Criteria.true');
530
+ assertClassifierEntry(criteria.false, 'Criteria.false');
531
+ }
532
+ return { questionId, type: 'noul', instructions, ...(criteria ? { criteria } : {}) };
533
+ }
534
+ function getEntryTemplates(type, text, lines, objectTemplate) {
535
+ if (type === 'lines')
536
+ return [...(lines ?? [])];
537
+ if (type === 'object')
538
+ return [objectTemplate ?? '{}'];
539
+ return [text ?? ''];
540
+ }
541
+ function getScoreCriteria(data) {
542
+ if (data.scoreCriteria)
543
+ return data.scoreCriteria;
544
+ return (data.levels ?? defaultLevels()).map((text) => ({ ...createScoreCriterion(), text }));
545
+ }
546
+ function getChoiceCriteria(data) {
547
+ if (data.choiceCriteria)
548
+ return data.choiceCriteria;
549
+ return (data.options ?? defaultOptions()).map((option) => ({
550
+ ...createChoiceCriterion(),
551
+ key: option.key,
552
+ text: option.value,
553
+ lines: [option.value],
554
+ }));
555
+ }
556
+ function getCriteriaType(data) {
557
+ return data.criteriaType ?? 'text';
558
+ }
559
+ function getNoulTrueCriteria(data) {
560
+ return data.noulTrueCriteria ?? data.yesMeans ?? '';
561
+ }
562
+ function getNoulFalseCriteria(data) {
563
+ return data.noulFalseCriteria ?? data.noMeans ?? '';
564
+ }
565
+ function getNoulCriteriaTemplates(data, truthValue) {
566
+ const type = getCriteriaType(data);
567
+ return getEntryTemplates(type, truthValue === 'true' ? getNoulTrueCriteria(data) : getNoulFalseCriteria(data), truthValue === 'true' ? data.noulTrueCriteriaLines : data.noulFalseCriteriaLines, truthValue === 'true' ? data.noulTrueCriteriaObjectTemplate : data.noulFalseCriteriaObjectTemplate);
568
+ }
569
+ function hasAuthoredNoulCriteria(data, truthValue) {
570
+ const type = getCriteriaType(data);
571
+ if (type === 'lines') {
572
+ const lines = truthValue === 'true' ? data.noulTrueCriteriaLines : data.noulFalseCriteriaLines;
573
+ return lines?.some((line) => line.trim() !== '') ?? false;
574
+ }
575
+ if (type === 'object') {
576
+ const template = truthValue === 'true' ? data.noulTrueCriteriaObjectTemplate : data.noulFalseCriteriaObjectTemplate;
577
+ const normalizedTemplate = template?.trim();
578
+ return normalizedTemplate !== undefined && normalizedTemplate !== '' && normalizedTemplate !== '{}';
579
+ }
580
+ return (truthValue === 'true' ? getNoulTrueCriteria(data) : getNoulFalseCriteria(data)).trim() !== '';
581
+ }
582
+ function resolveNoulCriteria(data, truthValue, inputs, context, preparation) {
583
+ const useInput = truthValue === 'true' ? data.useNoulTrueCriteriaInput : data.useNoulFalseCriteriaInput;
584
+ const label = `${truthValue} criteria`;
585
+ if (useInput) {
586
+ preparation.capture(getQuestionInputValue(inputs, `criteria${truthValue === 'true' ? 'True' : 'False'}`, label));
587
+ return getCriteriaType(data) === 'text'
588
+ ? getStringInput(inputs, `criteria${truthValue === 'true' ? 'True' : 'False'}`, label)
589
+ : getStructuredInput(inputs, `criteria${truthValue === 'true' ? 'True' : 'False'}`, label);
590
+ }
591
+ return resolveAuthoredClassifierEntry({
592
+ type: getCriteriaType(data),
593
+ text: truthValue === 'true' ? getNoulTrueCriteria(data) : getNoulFalseCriteria(data),
594
+ lines: truthValue === 'true' ? data.noulTrueCriteriaLines : data.noulFalseCriteriaLines,
595
+ objectTemplate: truthValue === 'true' ? data.noulTrueCriteriaObjectTemplate : data.noulFalseCriteriaObjectTemplate,
596
+ inputs,
597
+ context,
598
+ label,
599
+ preparation,
600
+ });
601
+ }
602
+ function hasClassifierCriteriaValue(value) {
603
+ if (typeof value === 'string')
604
+ return value.trim() !== '';
605
+ if (Array.isArray(value))
606
+ return value.some(hasClassifierCriteriaValue);
607
+ if (value === null || value === undefined)
608
+ return false;
609
+ if (typeof value !== 'object')
610
+ return true;
611
+ return Object.keys(value).length > 0;
612
+ }
613
+ export const classifierQuestionNode = nodeDefinition(ClassifierQuestionNodeImpl, 'Classifier Question');