@valerypopoff/rivet2-core 2.1.9 → 2.3.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 (188) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/bundle.cjs +5618 -2533
  3. package/dist/cjs/bundle.cjs.map +4 -4
  4. package/dist/cjs/webAppRuntime.cjs +333 -22
  5. package/dist/cjs/webAppRuntime.cjs.map +4 -4
  6. package/dist/esm/api/createProcessor.js +2 -0
  7. package/dist/esm/exports.js +1 -0
  8. package/dist/esm/integrations/CodeRunner.js +2 -7
  9. package/dist/esm/integrations/CodeRunnerOptions.js +7 -0
  10. package/dist/esm/model/AgentResponseTrace.js +558 -0
  11. package/dist/esm/model/ConnectedToolContinuationHost.js +33 -0
  12. package/dist/esm/model/GraphProcessor.js +165 -74
  13. package/dist/esm/model/NodeImpl.js +6 -0
  14. package/dist/esm/model/ProcessContextBuilder.js +3 -1
  15. package/dist/esm/model/RecordingPlayer.js +212 -25
  16. package/dist/esm/model/SplitRunProcessor.js +49 -8
  17. package/dist/esm/model/SubprocessorBridge.js +3 -0
  18. package/dist/esm/model/ToolCallContinuationCoordinator.js +7 -3
  19. package/dist/esm/model/UiGraph.js +21 -0
  20. package/dist/esm/model/UiGraphActionProtocol.js +3 -0
  21. package/dist/esm/model/UiGraphBrowserRuntime.js +133 -4
  22. package/dist/esm/model/UiGraphRendererStyles.js +221 -0
  23. package/dist/esm/model/UiGraphRuntimeModel.js +9 -3
  24. package/dist/esm/model/UiGraphSchema.js +7 -1
  25. package/dist/esm/model/chat/aiSdkTools.js +3 -2
  26. package/dist/esm/model/chat-v2/aiSdkBridge.js +146 -14
  27. package/dist/esm/model/chat-v2/chatV2CallObserver.js +11 -5
  28. package/dist/esm/model/chat-v2/chatV2CredentialNames.js +93 -0
  29. package/dist/esm/model/chat-v2/chatV2EditorCache.js +47 -20
  30. package/dist/esm/model/chat-v2/chatV2Errors.js +7 -19
  31. package/dist/esm/model/chat-v2/chatV2FeatureCompatibility.js +1 -1
  32. package/dist/esm/model/chat-v2/chatV2Outputs.js +48 -108
  33. package/dist/esm/model/chat-v2/chatV2Pipeline.js +101 -149
  34. package/dist/esm/model/chat-v2/chatV2ProviderProfile.js +39 -4
  35. package/dist/esm/model/chat-v2/chatV2ProviderRegistry.js +9 -0
  36. package/dist/esm/model/chat-v2/chatV2RequestPlan.js +2 -3
  37. package/dist/esm/model/chat-v2/chatV2ResponseBodyCapture.js +63 -0
  38. package/dist/esm/model/chat-v2/chatV2ResponseFormat.js +8 -4
  39. package/dist/esm/model/chat-v2/chatV2Retry.js +4 -3
  40. package/dist/esm/model/chat-v2/chatV2RuntimeOptions.js +7 -7
  41. package/dist/esm/model/chat-v2/chatV2Shared.js +1 -1
  42. package/dist/esm/model/chat-v2/chatV2Types.js +20 -8
  43. package/dist/esm/model/chat-v2/chatV2UsageAccounting.js +78 -0
  44. package/dist/esm/model/chat-v2/customProviderApi.js +40 -0
  45. package/dist/esm/model/chat-v2/index.js +15 -0
  46. package/dist/esm/model/chat-v2/llmChatV2Body.js +289 -0
  47. package/dist/esm/model/chat-v2/llmChatV2CacheBoundary.js +32 -0
  48. package/dist/esm/model/chat-v2/llmChatV2CachePolicy.js +25 -0
  49. package/dist/esm/model/chat-v2/llmChatV2NodeData.js +13 -50
  50. package/dist/esm/model/chat-v2/llmChatV2NodeEditors.js +117 -24
  51. package/dist/esm/model/chat-v2/llmChatV2NodeMigration.js +24 -0
  52. package/dist/esm/model/chat-v2/llmChatV2NodeRuntime.js +150 -199
  53. package/dist/esm/model/chat-v2/llmInvocationCoordinator.js +60 -0
  54. package/dist/esm/model/chat-v2/llmInvocationDecision.js +11 -0
  55. package/dist/esm/model/chat-v2/llmInvocationJournal.js +43 -0
  56. package/dist/esm/model/chat-v2/llmInvocationPlan.js +68 -0
  57. package/dist/esm/model/chat-v2/llmInvocationProjections.js +5 -0
  58. package/dist/esm/model/chat-v2/llmInvocationResultProjector.js +48 -0
  59. package/dist/esm/model/chat-v2/llmModelCandidate.js +79 -0
  60. package/dist/esm/model/chat-v2/llmProfile.js +52 -65
  61. package/dist/esm/model/chat-v2/llmProfileBody.js +193 -0
  62. package/dist/esm/model/chat-v2/llmProfileFallback.js +442 -100
  63. package/dist/esm/model/chat-v2/llmProfileFieldRegistry.js +74 -0
  64. package/dist/esm/model/chat-v2/llmProfileHealthStore.js +326 -0
  65. package/dist/esm/model/chat-v2/llmProfileNodeRuntime.js +55 -33
  66. package/dist/esm/model/chat-v2/llmProfileTypes.js +1 -18
  67. package/dist/esm/model/chat-v2/llmResponseMaterializer.js +42 -0
  68. package/dist/esm/model/chat-v2/modelRegistry.js +54 -4
  69. package/dist/esm/model/chat-v2/parallelToolCalls.js +9 -2
  70. package/dist/esm/model/chat-v2/providerOptions.js +116 -24
  71. package/dist/esm/model/chat-v2/rivetToolRegistry.js +9 -0
  72. package/dist/esm/model/chat-v2/toolCallCodec.js +52 -0
  73. package/dist/esm/model/chat-v2/toolContinuation.js +72 -35
  74. package/dist/esm/model/chat-v2/toolHandlerResolver.js +23 -0
  75. package/dist/esm/model/chat-v2/toolRoundExecutor.js +12 -0
  76. package/dist/esm/model/nodes/ChatNode.js +6 -0
  77. package/dist/esm/model/nodes/ChatNodeBase.js +1 -0
  78. package/dist/esm/model/nodes/CodeNewNode.js +5 -47
  79. package/dist/esm/model/nodes/CodeNode.js +4 -46
  80. package/dist/esm/model/nodes/DelegateFunctionCallNode.js +19 -6
  81. package/dist/esm/model/nodes/ExpressionNode.js +5 -8
  82. package/dist/esm/model/nodes/LLMChatV2Node.js +121 -229
  83. package/dist/esm/model/nodes/LLMProfileNode.js +27 -8
  84. package/dist/esm/model/nodes/MatchNode.js +86 -41
  85. package/dist/esm/model/nodes/PromptNode.js +7 -0
  86. package/dist/esm/model/nodes/ToolNode.js +7 -0
  87. package/dist/esm/model/nodes/toolCallDelegation.js +177 -110
  88. package/dist/esm/plugins/anthropic/anthropic.js +4 -4
  89. package/dist/esm/plugins/anthropic/nodes/ChatAnthropicNode.js +7 -0
  90. package/dist/esm/plugins/gentrace/gentraceSdk.js +0 -1
  91. package/dist/esm/plugins/gentrace/index.js +0 -1
  92. package/dist/esm/plugins/gentrace/plugin.js +61 -179
  93. package/dist/esm/plugins/google/google.js +5 -9
  94. package/dist/esm/plugins/google/nodes/ChatGoogleNode.js +4 -1
  95. package/dist/esm/plugins.js +1 -2
  96. package/dist/esm/recording/ExecutionRecorder.js +62 -13
  97. package/dist/esm/utils/emitDetached.js +6 -3
  98. package/dist/esm/utils/openai.js +60 -16
  99. package/dist/esm/utils/serialization/serialization.js +2 -19
  100. package/dist/esm/webAppRuntime.js +3 -2
  101. package/dist/types/api/createProcessor.d.ts +3 -0
  102. package/dist/types/exports.d.ts +1 -0
  103. package/dist/types/integrations/CodeRunner.d.ts +10 -7
  104. package/dist/types/integrations/CodeRunnerOptions.d.ts +15 -0
  105. package/dist/types/model/AgentResponseTrace.d.ts +123 -0
  106. package/dist/types/model/ConnectedToolContinuationHost.d.ts +34 -0
  107. package/dist/types/model/EditorDefinition.d.ts +16 -0
  108. package/dist/types/model/ExecutorProtocol.d.ts +32 -11
  109. package/dist/types/model/GraphProcessor.d.ts +41 -9
  110. package/dist/types/model/NodeBase.d.ts +0 -10
  111. package/dist/types/model/NodeImpl.d.ts +18 -1
  112. package/dist/types/model/Nodes.d.ts +2 -2
  113. package/dist/types/model/ProcessContext.d.ts +99 -0
  114. package/dist/types/model/ProcessContextBuilder.d.ts +3 -1
  115. package/dist/types/model/RecordingPlayer.d.ts +6 -0
  116. package/dist/types/model/SplitRunProcessor.d.ts +10 -3
  117. package/dist/types/model/UiGraph.d.ts +8 -0
  118. package/dist/types/model/UiGraphActionProtocol.d.ts +3 -0
  119. package/dist/types/model/UiGraphBrowserRuntime.d.ts +11 -2
  120. package/dist/types/model/UiGraphRendererStyles.d.ts +1 -1
  121. package/dist/types/model/UiGraphRuntimeModel.d.ts +2 -0
  122. package/dist/types/model/UiGraphSchema.d.ts +1 -0
  123. package/dist/types/model/chat-v2/chatV2CallObserver.d.ts +2 -0
  124. package/dist/types/model/chat-v2/chatV2CredentialNames.d.ts +30 -0
  125. package/dist/types/model/chat-v2/chatV2EditorCache.d.ts +2 -3
  126. package/dist/types/model/chat-v2/chatV2Outputs.d.ts +6 -16
  127. package/dist/types/model/chat-v2/chatV2Pipeline.d.ts +13 -17
  128. package/dist/types/model/chat-v2/chatV2ProviderProfile.d.ts +7 -0
  129. package/dist/types/model/chat-v2/chatV2ProviderRegistry.d.ts +8 -0
  130. package/dist/types/model/chat-v2/chatV2RequestPlan.d.ts +2 -2
  131. package/dist/types/model/chat-v2/chatV2ResponseBodyCapture.d.ts +25 -0
  132. package/dist/types/model/chat-v2/chatV2ResponseFormat.d.ts +3 -2
  133. package/dist/types/model/chat-v2/chatV2Retry.d.ts +2 -0
  134. package/dist/types/model/chat-v2/chatV2RuntimeOptions.d.ts +3 -8
  135. package/dist/types/model/chat-v2/chatV2Types.d.ts +44 -10
  136. package/dist/types/model/chat-v2/chatV2UsageAccounting.d.ts +14 -0
  137. package/dist/types/model/chat-v2/customProviderApi.d.ts +22 -0
  138. package/dist/types/model/chat-v2/index.d.ts +15 -0
  139. package/dist/types/model/chat-v2/llmChatV2Body.d.ts +24 -0
  140. package/dist/types/model/chat-v2/llmChatV2CacheBoundary.d.ts +10 -0
  141. package/dist/types/model/chat-v2/llmChatV2CachePolicy.d.ts +13 -0
  142. package/dist/types/model/chat-v2/llmChatV2NodeData.d.ts +19 -3
  143. package/dist/types/model/chat-v2/llmChatV2NodeMigration.d.ts +8 -0
  144. package/dist/types/model/chat-v2/llmChatV2NodeRuntime.d.ts +14 -16
  145. package/dist/types/model/chat-v2/llmInvocationCoordinator.d.ts +16 -0
  146. package/dist/types/model/chat-v2/llmInvocationDecision.d.ts +21 -0
  147. package/dist/types/model/chat-v2/llmInvocationJournal.d.ts +38 -0
  148. package/dist/types/model/chat-v2/llmInvocationPlan.d.ts +41 -0
  149. package/dist/types/model/chat-v2/llmInvocationProjections.d.ts +3 -0
  150. package/dist/types/model/chat-v2/llmInvocationResultProjector.d.ts +26 -0
  151. package/dist/types/model/chat-v2/llmModelCandidate.d.ts +20 -0
  152. package/dist/types/model/chat-v2/llmProfile.d.ts +14 -0
  153. package/dist/types/model/chat-v2/llmProfileBody.d.ts +20 -0
  154. package/dist/types/model/chat-v2/llmProfileFallback.d.ts +36 -49
  155. package/dist/types/model/chat-v2/llmProfileFieldRegistry.d.ts +209 -0
  156. package/dist/types/model/chat-v2/llmProfileHealthStore.d.ts +111 -0
  157. package/dist/types/model/chat-v2/llmProfileNodeRuntime.d.ts +1 -1
  158. package/dist/types/model/chat-v2/llmProfileTypes.d.ts +4 -1
  159. package/dist/types/model/chat-v2/llmResponseMaterializer.d.ts +20 -0
  160. package/dist/types/model/chat-v2/modelRegistry.d.ts +17 -187
  161. package/dist/types/model/chat-v2/parallelToolCalls.d.ts +3 -1
  162. package/dist/types/model/chat-v2/providerOptions.d.ts +12 -0
  163. package/dist/types/model/chat-v2/rivetToolRegistry.d.ts +11 -0
  164. package/dist/types/model/chat-v2/toolCallCodec.d.ts +7 -0
  165. package/dist/types/model/chat-v2/toolContinuation.d.ts +3 -3
  166. package/dist/types/model/chat-v2/toolHandlerResolver.d.ts +25 -0
  167. package/dist/types/model/chat-v2/toolRoundExecutor.d.ts +18 -0
  168. package/dist/types/model/nodes/ChatNode.d.ts +2 -1
  169. package/dist/types/model/nodes/CodeNewNode.d.ts +0 -5
  170. package/dist/types/model/nodes/CodeNode.d.ts +0 -5
  171. package/dist/types/model/nodes/DelegateFunctionCallNode.d.ts +2 -1
  172. package/dist/types/model/nodes/LLMChatV2Node.d.ts +2 -1
  173. package/dist/types/model/nodes/LLMProfileNode.d.ts +2 -1
  174. package/dist/types/model/nodes/MatchNode.d.ts +6 -0
  175. package/dist/types/model/nodes/PromptNode.d.ts +4 -1
  176. package/dist/types/model/nodes/ToolNode.d.ts +2 -1
  177. package/dist/types/model/nodes/toolCallDelegation.d.ts +10 -3
  178. package/dist/types/plugins/anthropic/anthropic.d.ts +1 -0
  179. package/dist/types/plugins/gentrace/gentraceSdk.d.ts +0 -1
  180. package/dist/types/plugins/gentrace/plugin.d.ts +55 -9
  181. package/dist/types/plugins/google/google.d.ts +2 -8
  182. package/dist/types/plugins.d.ts +1 -1
  183. package/dist/types/recording/ExecutionRecorder.d.ts +7 -1
  184. package/dist/types/recording/RecordedEvents.d.ts +6 -0
  185. package/dist/types/utils/emitDetached.d.ts +3 -2
  186. package/dist/types/utils/openai.d.ts +49 -0
  187. package/dist/types/webAppRuntime.d.ts +4 -2
  188. package/package.json +2 -2
@@ -21,6 +21,7 @@ export class MatchNodeImpl extends NodeImpl {
21
21
  data: {
22
22
  cases: ['YES', 'NO'],
23
23
  casePortIds: [nanoid(), nanoid()],
24
+ valueInputMode: 'shared',
24
25
  },
25
26
  };
26
27
  return chartNode;
@@ -34,57 +35,86 @@ export class MatchNodeImpl extends NodeImpl {
34
35
  required: true,
35
36
  description: 'The value that will be tested against each of the cases.',
36
37
  },
37
- {
38
+ ];
39
+ if (this.getValueInputMode() === 'shared') {
40
+ inputs.push({
38
41
  id: 'value',
39
- title: 'Value',
42
+ title: 'Custom value',
40
43
  dataType: 'any',
41
- description: 'The value passed through to the output port that matches. If unconnected, the test value will be passed through.',
42
- },
43
- ];
44
+ description: 'The optional value passed through to every matching output. If unconnected, the Test value is passed through.',
45
+ });
46
+ }
47
+ else {
48
+ const portIds = this.getCasePortIds();
49
+ this.data.cases.forEach((caseValue, index) => {
50
+ inputs.push({
51
+ id: this.getCaseValueInputId(portIds[index]),
52
+ title: this.getCaseTitle(caseValue, index),
53
+ dataType: 'any',
54
+ description: `The optional custom value emitted when /${caseValue}/ matches the Test value. If unconnected, Test is passed through.`,
55
+ });
56
+ });
57
+ inputs.push({
58
+ id: 'value-unmatched',
59
+ title: 'Unmatched',
60
+ dataType: 'any',
61
+ description: 'The optional custom value emitted when no regular expression matches the Test value. If unconnected, Test is passed through.',
62
+ });
63
+ }
44
64
  return inputs;
45
65
  }
46
66
  getOutputDefinitions() {
47
67
  const outputs = [];
48
- const portIds = resolveStoredOrderedPortIds(this.data.cases.length, this.data.casePortIds, {
49
- kind: 'prefix',
50
- prefix: 'case',
51
- startIndex: 1,
52
- });
68
+ const portIds = this.getCasePortIds();
53
69
  for (let i = 0; i < this.data.cases.length; i++) {
54
70
  outputs.push({
55
71
  id: portIds[i],
56
- title: this.data.cases[i]?.trim() ? this.data.cases[i] : `Case ${i + 1}`,
57
- dataType: 'string',
58
- description: `The 'value' (or 'test' if value is unconnected) passed through if the test value matches this regex: /${this
59
- .data.cases[i]}/`,
72
+ title: this.getCaseTitle(this.data.cases[i], i),
73
+ dataType: 'any',
74
+ description: this.getValueInputMode() === 'shared'
75
+ ? `The shared Custom value (or Test if Custom value is unconnected) passed through if the test value matches /${this.data.cases[i]}/.`
76
+ : `The corresponding ${this.getCaseTitle(this.data.cases[i], i)} custom value (or Test if it is unconnected) passed through if the test value matches /${this.data.cases[i]}/.`,
60
77
  });
61
78
  }
62
79
  outputs.push({
63
80
  id: 'unmatched',
64
81
  title: 'Unmatched',
65
- dataType: 'string',
66
- description: 'The value (or test if value is unconnected) passed through if no regexes match.',
82
+ dataType: 'any',
83
+ description: this.getValueInputMode() === 'shared'
84
+ ? 'The shared Custom value (or Test if Custom value is unconnected) passed through if no regexes match.'
85
+ : 'The Unmatched custom value (or Test if it is unconnected) passed through if no regexes match.',
67
86
  });
68
87
  return outputs;
69
88
  }
70
89
  getBody() {
71
- return dedent `
72
- ${this.data.exclusive ? 'First Matching Case' : 'All Matching Cases'}
73
- ${this.data.cases.length} Cases
74
- `;
90
+ return this.data.exclusive ? 'Trigger the first matching case only' : 'Trigger all matching cases';
75
91
  }
76
92
  getEditors() {
77
93
  return [
78
94
  {
79
- type: 'toggle',
95
+ type: 'segmented',
96
+ label: 'Matching cases to trigger',
80
97
  dataKey: 'exclusive',
81
- label: 'Exclusive',
82
- helperMessage: 'If enabled, only the first matching branch will be ran.',
98
+ defaultValue: false,
99
+ options: [
100
+ { value: false, label: 'Trigger all matching cases' },
101
+ { value: true, label: 'Trigger first only' },
102
+ ],
103
+ },
104
+ {
105
+ type: 'segmented',
106
+ label: 'Custom case values',
107
+ dataKey: 'valueInputMode',
108
+ defaultValue: 'shared',
109
+ options: [
110
+ { value: 'shared', label: 'One shared custom value' },
111
+ { value: 'per-output', label: 'Custom values per case' },
112
+ ],
83
113
  },
84
114
  {
85
115
  type: 'stringList',
86
116
  dataKey: 'cases',
87
- label: 'Cases',
117
+ label: 'Cases (regular expressions)',
88
118
  placeholder: 'Case (regular expression)',
89
119
  reorderable: true,
90
120
  portBinding: {
@@ -96,15 +126,15 @@ export class MatchNodeImpl extends NodeImpl {
96
126
  prefix: 'case',
97
127
  startIndex: 1,
98
128
  },
129
+ companionBindings: [{ side: 'input', prefix: 'value-' }],
99
130
  },
100
- helperMessage: '(Regular expressions)',
101
131
  },
102
132
  ];
103
133
  }
104
134
  static getUIData() {
105
135
  return {
106
136
  infoBoxBody: dedent `
107
- Any number of regular expressions can be configured, each corresponding to an output of the node. The output port of the first matching regex will be ran, and all other output ports will not be ran.
137
+ Configure any number of regular expressions, each corresponding to an output. Trigger all matched outputs by default, or choose Trigger first matching case. One shared custom value passes one optional value to every active output, while Custom values per case gives each case and Unmatched output its own optional custom value input.
108
138
  `,
109
139
  infoBoxTitle: 'Regex Match Node',
110
140
  contextMenuTitle: 'Regex Match',
@@ -114,14 +144,19 @@ export class MatchNodeImpl extends NodeImpl {
114
144
  async process(inputs) {
115
145
  const inputValue = inputs['input'];
116
146
  const inputString = inputValue?.value == null ? undefined : coerceType(inputValue, 'string');
117
- const value = inputs['value'];
118
- const portIds = resolveStoredOrderedPortIds(this.data.cases.length, this.data.casePortIds, {
119
- kind: 'prefix',
120
- prefix: 'case',
121
- startIndex: 1,
147
+ const portIds = this.getCasePortIds();
148
+ const sharedValue = inputs['value'];
149
+ const getTestOutputValue = () => ({
150
+ type: 'string',
151
+ value: inputString,
122
152
  });
123
- const outputType = value === undefined ? 'string' : value.type;
124
- const outputValue = value === undefined ? inputString : value.value;
153
+ const getSharedOutputValue = () => sharedValue ?? getTestOutputValue();
154
+ const getOutputValue = (portId) => this.getValueInputMode() === 'shared'
155
+ ? getSharedOutputValue()
156
+ : inputs[this.getCaseValueInputId(portId)] ?? getTestOutputValue();
157
+ const getUnmatchedOutputValue = () => this.getValueInputMode() === 'shared'
158
+ ? getSharedOutputValue()
159
+ : inputs['value-unmatched'] ?? getTestOutputValue();
125
160
  const cases = this.data.cases;
126
161
  let matched = false;
127
162
  const output = {};
@@ -130,10 +165,7 @@ export class MatchNodeImpl extends NodeImpl {
130
165
  const canMatch = !this.data.exclusive || !matched;
131
166
  if (match && canMatch) {
132
167
  matched = true;
133
- output[portIds[i]] = {
134
- type: outputType,
135
- value: outputValue,
136
- };
168
+ output[portIds[i]] = getOutputValue(portIds[i]);
137
169
  }
138
170
  else {
139
171
  output[portIds[i]] = {
@@ -143,10 +175,7 @@ export class MatchNodeImpl extends NodeImpl {
143
175
  }
144
176
  }
145
177
  if (!matched) {
146
- output['unmatched'] = {
147
- type: outputType,
148
- value: outputValue,
149
- };
178
+ output['unmatched'] = getUnmatchedOutputValue();
150
179
  }
151
180
  else {
152
181
  output['unmatched'] = {
@@ -156,5 +185,21 @@ export class MatchNodeImpl extends NodeImpl {
156
185
  }
157
186
  return output;
158
187
  }
188
+ getValueInputMode() {
189
+ return this.data.valueInputMode === 'per-output' ? 'per-output' : 'shared';
190
+ }
191
+ getCasePortIds() {
192
+ return resolveStoredOrderedPortIds(this.data.cases.length, this.data.casePortIds, {
193
+ kind: 'prefix',
194
+ prefix: 'case',
195
+ startIndex: 1,
196
+ });
197
+ }
198
+ getCaseTitle(caseValue, index) {
199
+ return caseValue?.trim() ? caseValue : `Case ${index + 1}`;
200
+ }
201
+ getCaseValueInputId(portId) {
202
+ return `value-${portId}`;
203
+ }
159
204
  }
160
205
  export const matchNode = nodeDefinition(MatchNodeImpl, 'Regex Match');
@@ -90,6 +90,13 @@ export class PromptNodeImpl extends NodeImpl {
90
90
  }
91
91
  return outputs;
92
92
  }
93
+ getRunActivityDescriptor() {
94
+ // A Prompt can additionally emit a token count, but its user-visible
95
+ // result is always the generated chat-message on `output`.
96
+ return {
97
+ primaryOutputPortId: 'output',
98
+ };
99
+ }
93
100
  getEditors() {
94
101
  return [
95
102
  {
@@ -84,6 +84,13 @@ export class GptFunctionNodeImpl extends NodeImpl {
84
84
  },
85
85
  ];
86
86
  }
87
+ getRunActivityDescriptor() {
88
+ return {
89
+ category: 'tool',
90
+ primaryOutputPortId: 'function',
91
+ fullOutputActionLabel: 'Open tool definition',
92
+ };
93
+ }
87
94
  getEditors() {
88
95
  return [
89
96
  {
@@ -1,6 +1,14 @@
1
1
  import { omit } from 'lodash-es';
2
2
  import { coerceTypeOptional } from '../../utils/coerceType.js';
3
3
  import { getError } from '../../utils/errors.js';
4
+ import { createDelegatedToolCallRecord, createToolResultMessage, normalizeToolCall, stringifyToolResult, } from '../chat-v2/toolCallCodec.js';
5
+ import { findAutoDelegateGraphCandidate, resolveToolHandler } from '../chat-v2/toolHandlerResolver.js';
6
+ /**
7
+ * Reserved inputs belong to Rivet's tool invocation envelope. Model-produced
8
+ * arguments must never replace them, otherwise a handler can observe a tool
9
+ * name or argument object that does not match the invocation being delegated.
10
+ */
11
+ const RIVET_TOOL_CALL_INPUT_IDS = new Set(['_function_name', '_arguments', '_rivet_tool_call']);
4
12
  /**
5
13
  * Finds an Auto Delegate handler from an ordered set of graph-like candidates.
6
14
  *
@@ -9,10 +17,7 @@ import { getError } from '../../utils/errors.js';
9
17
  * entries and uses their map keys. Keeping that distinction here preserves the
10
18
  * existing behavior for malformed projects where those identities disagree.
11
19
  */
12
- export function findAutoDelegateGraphCandidate(candidates, toolName, getGraphName) {
13
- return (candidates.find((candidate) => getGraphName(candidate) === toolName) ??
14
- candidates.find((candidate) => getGraphName(candidate)?.includes(toolName)));
15
- }
20
+ export { findAutoDelegateGraphCandidate };
16
21
  export function isDelegatedToolCallRecord(input) {
17
22
  const maybeRecord = input;
18
23
  const maybeMessage = maybeRecord?.message;
@@ -41,9 +46,26 @@ export function buildDelegatedToolCallOutputs(records, preToolMessage, cost) {
41
46
  type: 'string',
42
47
  value: preToolMessage,
43
48
  };
49
+ const executionTimesSeconds = records.map((record) => {
50
+ const executionTimeMs = getExecutionTimeMs(record);
51
+ return executionTimeMs == null ? undefined : executionTimeMs / 1_000;
52
+ });
53
+ const executionTimeOutput = executionTimesSeconds.every((executionTimeSeconds) => executionTimeSeconds != null)
54
+ ? records.length === 1
55
+ ? {
56
+ type: 'number',
57
+ value: executionTimesSeconds[0],
58
+ }
59
+ : {
60
+ type: 'number[]',
61
+ value: executionTimesSeconds,
62
+ }
63
+ : {
64
+ type: 'control-flow-excluded',
65
+ value: undefined,
66
+ };
44
67
  const outputs = records.length === 1
45
68
  ? {
46
- ['assistant-message']: preToolMessageOutput,
47
69
  ['tool-name']: {
48
70
  type: 'string',
49
71
  value: records[0].name,
@@ -52,17 +74,18 @@ export function buildDelegatedToolCallOutputs(records, preToolMessage, cost) {
52
74
  type: 'object',
53
75
  value: records[0].arguments,
54
76
  },
77
+ ['assistant-message']: preToolMessageOutput,
55
78
  ['output']: {
56
79
  type: 'string',
57
80
  value: records[0].output,
58
81
  },
82
+ ['execution-time']: executionTimeOutput,
59
83
  ['message']: {
60
84
  type: 'chat-message',
61
85
  value: records[0].message,
62
86
  },
63
87
  }
64
88
  : {
65
- ['assistant-message']: preToolMessageOutput,
66
89
  ['tool-name']: {
67
90
  type: 'string[]',
68
91
  value: records.map((record) => record.name),
@@ -71,10 +94,12 @@ export function buildDelegatedToolCallOutputs(records, preToolMessage, cost) {
71
94
  type: 'object[]',
72
95
  value: records.map((record) => record.arguments),
73
96
  },
97
+ ['assistant-message']: preToolMessageOutput,
74
98
  ['output']: {
75
99
  type: 'string[]',
76
100
  value: records.map((record) => record.output),
77
101
  },
102
+ ['execution-time']: executionTimeOutput,
78
103
  ['message']: {
79
104
  type: 'chat-message[]',
80
105
  value: records.map((record) => record.message),
@@ -85,112 +110,108 @@ export function buildDelegatedToolCallOutputs(records, preToolMessage, cost) {
85
110
  }
86
111
  return outputs;
87
112
  }
88
- export function normalizeFunctionCallInput(input) {
89
- if (Array.isArray(input)) {
90
- if (input.length !== 1) {
91
- throw new Error(`Delegate Tool Call expected a single tool call, but received ${input.length}. Use Run per item or select one tool call before delegating.`);
92
- }
93
- return normalizeFunctionCallInput(input[0]);
94
- }
95
- if (typeof input !== 'object' || input == null) {
96
- throw new Error('Delegate Tool Call expected a tool call object.');
97
- }
98
- const rawFunctionCall = input;
99
- const name = rawFunctionCall.name;
100
- if (typeof name !== 'string' || name.length === 0) {
101
- throw new Error('Delegate Tool Call expected the tool call to include a name.');
102
- }
103
- const id = typeof rawFunctionCall.id === 'string' ? rawFunctionCall.id : undefined;
104
- const rawArguments = rawFunctionCall.arguments;
105
- if (rawArguments == null) {
106
- return { id, name, arguments: {} };
107
- }
108
- if (typeof rawArguments === 'string') {
109
- try {
110
- const parsedArguments = JSON.parse(rawArguments);
111
- if (typeof parsedArguments === 'object' && parsedArguments != null && !Array.isArray(parsedArguments)) {
112
- return { id, name, arguments: parsedArguments };
113
- }
114
- }
115
- catch {
116
- // Fall through to the explicit error below.
117
- }
118
- throw new Error(`Delegate Tool Call expected "${name}" arguments to be a JSON object.`);
113
+ /** @deprecated Use normalizeToolCall from the canonical chat-v2 codec. */
114
+ export const normalizeFunctionCallInput = normalizeToolCall;
115
+ export async function delegateToolCall(rawFunctionCall, context, config) {
116
+ const functionCall = normalizeToolCall(rawFunctionCall);
117
+ const source = context.toolCallTraceSource ?? {
118
+ nodeId: context.node?.id ?? 'unknown',
119
+ processId: context.processId ?? 'unknown',
120
+ };
121
+ // A connected continuation changes the trace *source* to the LLM Chat, but
122
+ // the Delegate invocation still owns the persisted result. Carry that exact
123
+ // pointer so observers can navigate to the result without matching tool
124
+ // names or scanning potentially evicted input values.
125
+ const resultOwner = getToolCallResultOwner(context);
126
+ const startedAt = Date.now();
127
+ const timingStart = getCurrentTimeMs();
128
+ const trace = { handlerKind: 'unknown' };
129
+ try {
130
+ const result = await delegateResolvedToolCall(functionCall, context, config, trace);
131
+ notifyToolCallFinished(context, {
132
+ ...(functionCall.id == null ? {} : { toolCallId: functionCall.id }),
133
+ toolName: functionCall.name,
134
+ sourceNodeId: source.nodeId,
135
+ sourceProcessId: source.processId,
136
+ ...(resultOwner == null ? {} : { resultOwner }),
137
+ ...trace,
138
+ outcome: result.traceOutcome ?? 'success',
139
+ startedAt,
140
+ durationMs: Math.max(0, getCurrentTimeMs() - timingStart),
141
+ });
142
+ return result;
119
143
  }
120
- if (typeof rawArguments === 'object' && !Array.isArray(rawArguments)) {
121
- return { id, name, arguments: rawArguments };
144
+ catch (error) {
145
+ notifyToolCallFinished(context, {
146
+ ...(functionCall.id == null ? {} : { toolCallId: functionCall.id }),
147
+ toolName: functionCall.name,
148
+ sourceNodeId: source.nodeId,
149
+ sourceProcessId: source.processId,
150
+ ...trace,
151
+ outcome: context.signal.aborted ? 'aborted' : 'failure',
152
+ startedAt,
153
+ durationMs: Math.max(0, getCurrentTimeMs() - timingStart),
154
+ });
155
+ throw error;
122
156
  }
123
- throw new Error(`Delegate Tool Call expected "${name}" arguments to be an object.`);
124
157
  }
125
- function buildToolResultMessage(functionCall, outputString) {
126
- return {
127
- type: 'function',
128
- message: outputString,
129
- name: functionCall.id ?? '',
130
- toolName: functionCall.name,
131
- };
132
- }
133
- function buildDelegatedToolCallRecord(functionCall, outputString) {
158
+ function getToolCallResultOwner(context) {
159
+ const node = context.node;
160
+ if (node?.type !== 'delegateFunctionCall' || typeof node.id !== 'string' || typeof context.processId !== 'string') {
161
+ return undefined;
162
+ }
134
163
  return {
135
- delegatedToolCall: true,
136
- name: functionCall.name,
137
- arguments: functionCall.arguments,
138
- id: functionCall.id,
139
- output: outputString,
140
- message: buildToolResultMessage(functionCall, outputString),
164
+ nodeId: node.id,
165
+ processId: context.processId,
166
+ outputPortId: 'output',
141
167
  };
142
168
  }
143
- function stringifyToolOutput(output) {
144
- if (typeof output === 'string') {
145
- return output;
146
- }
147
- return JSON.stringify(output) ?? String(output);
148
- }
149
- export async function delegateToolCall(rawFunctionCall, context, config) {
150
- const functionCall = normalizeFunctionCallInput(rawFunctionCall);
169
+ async function delegateResolvedToolCall(functionCall, context, config, trace) {
170
+ const resolvedHandler = resolveToolHandler({
171
+ project: context.project,
172
+ toolName: functionCall.name,
173
+ config,
174
+ hasExternalFunction: context.externalFunctions[functionCall.name] != null,
175
+ });
151
176
  let handler;
152
- if (config.autoDelegate) {
153
- const graphs = Object.values(context.project.graphs);
154
- const matchingGraph = findAutoDelegateGraphCandidate(graphs, functionCall.name, (graph) => graph.metadata?.name);
155
- if (matchingGraph) {
156
- handler = { key: undefined, value: matchingGraph.metadata.id };
157
- }
158
- }
159
- else {
160
- handler = config.handlers.find((handler) => handler.key === functionCall.name);
161
- }
162
- if (!handler) {
163
- if (config.autoDelegate && config.fallBackToExternalCall) {
164
- const externalFunction = context.externalFunctions[functionCall.name];
165
- if (externalFunction) {
166
- try {
167
- const externalContext = omit(context, ['setGlobal']);
168
- const result = await externalFunction(externalContext, functionCall.arguments ?? {});
169
- const outputString = stringifyToolOutput(result);
177
+ if (resolvedHandler?.kind === 'external') {
178
+ const externalFunction = context.externalFunctions[functionCall.name];
179
+ if (externalFunction) {
180
+ trace.handlerKind = 'external';
181
+ trace.handlerName = functionCall.name;
182
+ const executionStart = getCurrentTimeMs();
183
+ try {
184
+ const externalContext = omit(context, ['setGlobal']);
185
+ const result = await externalFunction(externalContext, functionCall.arguments ?? {});
186
+ const outputString = stringifyToolResult(result);
187
+ const executionTimeMs = getCurrentTimeMs() - executionStart;
188
+ return {
189
+ outputString,
190
+ message: createToolResultMessage(functionCall, outputString),
191
+ record: createDelegatedToolCallRecord(functionCall, outputString, executionTimeMs),
192
+ cost: result.cost,
193
+ };
194
+ }
195
+ catch (error) {
196
+ if (config.passthroughErrors) {
197
+ const outputString = `Error: ${getError(error).message}`;
198
+ const executionTimeMs = getCurrentTimeMs() - executionStart;
170
199
  return {
171
200
  outputString,
172
- message: buildToolResultMessage(functionCall, outputString),
173
- record: buildDelegatedToolCallRecord(functionCall, outputString),
174
- cost: result.cost,
201
+ message: createToolResultMessage(functionCall, outputString),
202
+ record: createDelegatedToolCallRecord(functionCall, outputString, executionTimeMs),
203
+ traceOutcome: 'passthrough-error',
175
204
  };
176
205
  }
177
- catch (error) {
178
- if (config.passthroughErrors) {
179
- const outputString = `Error: ${getError(error).message}`;
180
- return {
181
- outputString,
182
- message: buildToolResultMessage(functionCall, outputString),
183
- record: buildDelegatedToolCallRecord(functionCall, outputString),
184
- };
185
- }
186
- throw new Error(`External function call failed for ${functionCall.name}: ${getError(error).message}`);
187
- }
206
+ throw new Error(`External function call failed for ${functionCall.name}: ${getError(error).message}`);
188
207
  }
189
208
  }
190
- if (config.unknownHandler) {
191
- handler = { key: undefined, value: config.unknownHandler };
192
- }
193
- else if (config.autoDelegate) {
209
+ }
210
+ if (resolvedHandler?.kind === 'graph' || resolvedHandler?.kind === 'unknown') {
211
+ handler = { value: resolvedHandler.graphId };
212
+ }
213
+ if (!handler) {
214
+ if (config.autoDelegate) {
194
215
  const errorMessage = config.fallBackToExternalCall
195
216
  ? `No handler found for tool call: ${functionCall.name}, no graph containing the name "${functionCall.name}" was found, and no external function with that name was registered.`
196
217
  : `No handler found for tool call: ${functionCall.name}, no graph containing the name "${functionCall.name}" was found.`;
@@ -200,30 +221,76 @@ export async function delegateToolCall(rawFunctionCall, context, config) {
200
221
  throw new Error(`No handler found for tool call: ${functionCall.name}`);
201
222
  }
202
223
  }
203
- const subgraphInputs = {
204
- _function_name: {
205
- type: 'string',
206
- value: functionCall.name,
207
- },
208
- _arguments: {
209
- type: 'object',
210
- value: functionCall.arguments,
224
+ // A null-prototype map prevents model arguments such as `__proto__` from
225
+ // changing the input map's prototype. Retain the legacy top-level argument
226
+ // inputs for existing handler graphs, while also providing one namespaced
227
+ // invocation envelope for new graphs.
228
+ const subgraphInputs = Object.create(null);
229
+ subgraphInputs._function_name = {
230
+ type: 'string',
231
+ value: functionCall.name,
232
+ };
233
+ subgraphInputs._arguments = {
234
+ type: 'object',
235
+ value: functionCall.arguments,
236
+ };
237
+ subgraphInputs._rivet_tool_call = {
238
+ type: 'object',
239
+ value: {
240
+ id: functionCall.id,
241
+ name: functionCall.name,
242
+ arguments: functionCall.arguments,
211
243
  },
212
244
  };
245
+ trace.handlerKind = 'graph';
246
+ trace.handlerGraphId = handler.value;
247
+ const handlerName = context.project.graphs[handler.value]?.metadata?.name;
248
+ if (handlerName != null) {
249
+ trace.handlerName = handlerName;
250
+ }
213
251
  for (const [argName, argument] of Object.entries(functionCall.arguments ?? {})) {
252
+ if (RIVET_TOOL_CALL_INPUT_IDS.has(argName)) {
253
+ continue;
254
+ }
214
255
  subgraphInputs[argName] = {
215
256
  type: 'any',
216
257
  value: argument,
217
258
  };
218
259
  }
219
260
  const subprocessor = context.createSubProcessor(handler.value, { signal: context.signal });
261
+ const executionStart = getCurrentTimeMs();
220
262
  const outputs = await subprocessor.processGraph(context, subgraphInputs, context.contextValues);
221
- const outputString = coerceTypeOptional(outputs['output'], 'string') ?? '';
263
+ const executionTimeMs = getCurrentTimeMs() - executionStart;
264
+ const outputValue = outputs['output'];
265
+ if (outputValue == null) {
266
+ throw new Error(`Tool handler graph "${handlerName ?? handler.value}" must return a string Graph Output named "output".`);
267
+ }
268
+ const outputString = coerceTypeOptional(outputValue, 'string');
269
+ if (outputString == null) {
270
+ throw new Error(`Tool handler graph "${handlerName ?? handler.value}" returned an invalid Graph Output named "output". It must resolve to a string.`);
271
+ }
222
272
  const cost = coerceTypeOptional(outputs['cost'], 'number');
223
273
  return {
224
274
  outputString,
225
- message: buildToolResultMessage(functionCall, outputString),
226
- record: buildDelegatedToolCallRecord(functionCall, outputString),
275
+ message: createToolResultMessage(functionCall, outputString),
276
+ record: createDelegatedToolCallRecord(functionCall, outputString, executionTimeMs),
227
277
  cost,
228
278
  };
229
279
  }
280
+ function notifyToolCallFinished(context, event) {
281
+ try {
282
+ context.onToolCallFinished?.(event);
283
+ }
284
+ catch {
285
+ // Trace observers must never change tool execution behavior.
286
+ }
287
+ }
288
+ function getCurrentTimeMs() {
289
+ return globalThis.performance?.now?.() ?? Date.now();
290
+ }
291
+ function getExecutionTimeMs(record) {
292
+ const { executionTimeMs } = record;
293
+ return typeof executionTimeMs === 'number' && Number.isFinite(executionTimeMs) && executionTimeMs >= 0
294
+ ? executionTimeMs
295
+ : undefined;
296
+ }
@@ -4,8 +4,8 @@ export const anthropicModels = {
4
4
  'claude-instant-1': {
5
5
  maxTokens: 100_000,
6
6
  cost: {
7
- prompt: 0.00163,
8
- completion: 0.00551,
7
+ prompt: 1.63e-6,
8
+ completion: 5.51e-6,
9
9
  },
10
10
  displayName: 'Claude Instant',
11
11
  },
@@ -85,7 +85,7 @@ export const anthropicModels = {
85
85
  maxTokens: 200_000,
86
86
  cost: {
87
87
  prompt: 3e-6,
88
- completion: 3.75e-6,
88
+ completion: 15e-6,
89
89
  },
90
90
  displayName: 'Claude Sonnet 4',
91
91
  },
@@ -93,7 +93,7 @@ export const anthropicModels = {
93
93
  maxTokens: 200_000,
94
94
  cost: {
95
95
  prompt: 15e-6,
96
- completion: 18.75e-6,
96
+ completion: 75e-6,
97
97
  },
98
98
  displayName: 'Claude Opus 4',
99
99
  },