@valerypopoff/rivet2-core 2.1.8 → 2.1.10

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 (168) hide show
  1. package/dist/cjs/bundle.cjs +4457 -1809
  2. package/dist/cjs/bundle.cjs.map +4 -4
  3. package/dist/cjs/webAppRuntime.cjs +298 -21
  4. package/dist/cjs/webAppRuntime.cjs.map +4 -4
  5. package/dist/esm/exports.js +1 -0
  6. package/dist/esm/model/AgentResponseTrace.js +448 -0
  7. package/dist/esm/model/ConnectedToolContinuationHost.js +33 -0
  8. package/dist/esm/model/GraphProcessor.js +152 -74
  9. package/dist/esm/model/NodeImpl.js +6 -0
  10. package/dist/esm/model/ProcessContextBuilder.js +3 -1
  11. package/dist/esm/model/RecordingPlayer.js +164 -22
  12. package/dist/esm/model/SplitRunProcessor.js +68 -18
  13. package/dist/esm/model/SubprocessorBridge.js +2 -0
  14. package/dist/esm/model/ToolCallContinuationCoordinator.js +13 -4
  15. package/dist/esm/model/UiGraph.js +21 -0
  16. package/dist/esm/model/UiGraphActionProtocol.js +2 -0
  17. package/dist/esm/model/UiGraphBrowserRuntime.js +133 -4
  18. package/dist/esm/model/UiGraphRendererStyles.js +221 -0
  19. package/dist/esm/model/UiGraphRuntimeModel.js +9 -3
  20. package/dist/esm/model/UiGraphSchema.js +7 -1
  21. package/dist/esm/model/chat/aiSdkMessages.js +8 -0
  22. package/dist/esm/model/chat/aiSdkTools.js +3 -2
  23. package/dist/esm/model/chat-v2/chatV2CallObserver.js +11 -5
  24. package/dist/esm/model/chat-v2/chatV2EditorCache.js +58 -10
  25. package/dist/esm/model/chat-v2/chatV2Errors.js +7 -19
  26. package/dist/esm/model/chat-v2/chatV2FeatureCompatibility.js +1 -1
  27. package/dist/esm/model/chat-v2/chatV2Outputs.js +56 -106
  28. package/dist/esm/model/chat-v2/chatV2Pipeline.js +142 -149
  29. package/dist/esm/model/chat-v2/chatV2ProviderProfile.js +9 -2
  30. package/dist/esm/model/chat-v2/chatV2ProviderRegistry.js +9 -0
  31. package/dist/esm/model/chat-v2/chatV2RequestPlan.js +5 -5
  32. package/dist/esm/model/chat-v2/chatV2ResponseBodyCapture.js +55 -0
  33. package/dist/esm/model/chat-v2/chatV2ResponseFormat.js +8 -4
  34. package/dist/esm/model/chat-v2/chatV2Retry.js +4 -3
  35. package/dist/esm/model/chat-v2/chatV2RuntimeOptions.js +6 -1
  36. package/dist/esm/model/chat-v2/chatV2Shared.js +1 -6
  37. package/dist/esm/model/chat-v2/chatV2Types.js +7 -1
  38. package/dist/esm/model/chat-v2/chatV2UsageAccounting.js +78 -0
  39. package/dist/esm/model/chat-v2/customProviderApi.js +42 -0
  40. package/dist/esm/model/chat-v2/developerMessageRoles.js +45 -0
  41. package/dist/esm/model/chat-v2/index.js +12 -0
  42. package/dist/esm/model/chat-v2/llmChatV2Body.js +281 -0
  43. package/dist/esm/model/chat-v2/llmChatV2CacheBoundary.js +32 -0
  44. package/dist/esm/model/chat-v2/llmChatV2CachePolicy.js +20 -0
  45. package/dist/esm/model/chat-v2/llmChatV2NodeData.js +8 -54
  46. package/dist/esm/model/chat-v2/llmChatV2NodeEditors.js +41 -20
  47. package/dist/esm/model/chat-v2/llmChatV2NodeMigration.js +24 -0
  48. package/dist/esm/model/chat-v2/llmChatV2NodeRuntime.js +178 -94
  49. package/dist/esm/model/chat-v2/llmInvocationCoordinator.js +60 -0
  50. package/dist/esm/model/chat-v2/llmInvocationDecision.js +11 -0
  51. package/dist/esm/model/chat-v2/llmInvocationJournal.js +43 -0
  52. package/dist/esm/model/chat-v2/llmInvocationPlan.js +68 -0
  53. package/dist/esm/model/chat-v2/llmInvocationProjections.js +5 -0
  54. package/dist/esm/model/chat-v2/llmInvocationResultProjector.js +48 -0
  55. package/dist/esm/model/chat-v2/llmModelCandidate.js +75 -0
  56. package/dist/esm/model/chat-v2/llmProfile.js +31 -65
  57. package/dist/esm/model/chat-v2/llmProfileBody.js +183 -0
  58. package/dist/esm/model/chat-v2/llmProfileFallback.js +244 -0
  59. package/dist/esm/model/chat-v2/llmProfileFieldRegistry.js +67 -0
  60. package/dist/esm/model/chat-v2/llmProfileNodeRuntime.js +3 -0
  61. package/dist/esm/model/chat-v2/llmProfileTypes.js +1 -18
  62. package/dist/esm/model/chat-v2/llmResponseMaterializer.js +42 -0
  63. package/dist/esm/model/chat-v2/messageConverter.js +14 -0
  64. package/dist/esm/model/chat-v2/modelRegistry.js +54 -4
  65. package/dist/esm/model/chat-v2/parallelToolCalls.js +9 -2
  66. package/dist/esm/model/chat-v2/providerOptions.js +120 -21
  67. package/dist/esm/model/chat-v2/rivetToolRegistry.js +9 -0
  68. package/dist/esm/model/chat-v2/toolCallCodec.js +52 -0
  69. package/dist/esm/model/chat-v2/toolContinuation.js +72 -39
  70. package/dist/esm/model/chat-v2/toolHandlerResolver.js +23 -0
  71. package/dist/esm/model/chat-v2/toolRoundExecutor.js +12 -0
  72. package/dist/esm/model/nodes/AssembleMessageNode.js +6 -0
  73. package/dist/esm/model/nodes/ChatNode.js +6 -0
  74. package/dist/esm/model/nodes/ChatNodeBase.js +1 -0
  75. package/dist/esm/model/nodes/DelegateFunctionCallNode.js +26 -1
  76. package/dist/esm/model/nodes/LLMChatV2Node.js +131 -144
  77. package/dist/esm/model/nodes/LLMProfileNode.js +27 -8
  78. package/dist/esm/model/nodes/PromptNode.js +16 -2
  79. package/dist/esm/model/nodes/ToolNode.js +7 -0
  80. package/dist/esm/model/nodes/toolCallDelegation.js +193 -110
  81. package/dist/esm/plugins/anthropic/anthropic.js +4 -4
  82. package/dist/esm/plugins/anthropic/nodes/ChatAnthropicNode.js +29 -28
  83. package/dist/esm/plugins/google/google.js +25 -18
  84. package/dist/esm/plugins/google/nodes/ChatGoogleNode.js +21 -7
  85. package/dist/esm/recording/ExecutionRecorder.js +13 -5
  86. package/dist/esm/utils/chatMessageToOpenAIChatCompletionMessage.js +4 -0
  87. package/dist/esm/utils/emitDetached.js +6 -3
  88. package/dist/esm/utils/openai.js +60 -16
  89. package/dist/esm/utils/serialization/serialization.js +2 -0
  90. package/dist/esm/webAppRuntime.js +3 -2
  91. package/dist/types/exports.d.ts +1 -0
  92. package/dist/types/model/AgentResponseTrace.d.ts +110 -0
  93. package/dist/types/model/ConnectedToolContinuationHost.d.ts +34 -0
  94. package/dist/types/model/DataValue.d.ts +7 -1
  95. package/dist/types/model/ExecutorProtocol.d.ts +25 -7
  96. package/dist/types/model/GraphProcessor.d.ts +27 -5
  97. package/dist/types/model/NodeBase.d.ts +6 -0
  98. package/dist/types/model/NodeImpl.d.ts +18 -1
  99. package/dist/types/model/Nodes.d.ts +2 -2
  100. package/dist/types/model/ProcessContext.d.ts +51 -0
  101. package/dist/types/model/ProcessContextBuilder.d.ts +3 -1
  102. package/dist/types/model/RecordingPlayer.d.ts +6 -0
  103. package/dist/types/model/SplitRunProcessor.d.ts +11 -3
  104. package/dist/types/model/UiGraph.d.ts +8 -0
  105. package/dist/types/model/UiGraphActionProtocol.d.ts +2 -0
  106. package/dist/types/model/UiGraphBrowserRuntime.d.ts +11 -2
  107. package/dist/types/model/UiGraphRendererStyles.d.ts +1 -1
  108. package/dist/types/model/UiGraphRuntimeModel.d.ts +2 -0
  109. package/dist/types/model/UiGraphSchema.d.ts +1 -0
  110. package/dist/types/model/chat-v2/chatV2CallObserver.d.ts +2 -0
  111. package/dist/types/model/chat-v2/chatV2EditorCache.d.ts +3 -1
  112. package/dist/types/model/chat-v2/chatV2Outputs.d.ts +7 -17
  113. package/dist/types/model/chat-v2/chatV2Pipeline.d.ts +29 -0
  114. package/dist/types/model/chat-v2/chatV2ProviderProfile.d.ts +5 -0
  115. package/dist/types/model/chat-v2/chatV2ProviderRegistry.d.ts +8 -0
  116. package/dist/types/model/chat-v2/chatV2RequestPlan.d.ts +2 -2
  117. package/dist/types/model/chat-v2/chatV2ResponseBodyCapture.d.ts +19 -0
  118. package/dist/types/model/chat-v2/chatV2ResponseFormat.d.ts +3 -2
  119. package/dist/types/model/chat-v2/chatV2Retry.d.ts +2 -0
  120. package/dist/types/model/chat-v2/chatV2Shared.d.ts +0 -1
  121. package/dist/types/model/chat-v2/chatV2Types.d.ts +35 -1
  122. package/dist/types/model/chat-v2/chatV2UsageAccounting.d.ts +14 -0
  123. package/dist/types/model/chat-v2/customProviderApi.d.ts +23 -0
  124. package/dist/types/model/chat-v2/developerMessageRoles.d.ts +10 -0
  125. package/dist/types/model/chat-v2/index.d.ts +12 -0
  126. package/dist/types/model/chat-v2/llmChatV2Body.d.ts +24 -0
  127. package/dist/types/model/chat-v2/llmChatV2CacheBoundary.d.ts +10 -0
  128. package/dist/types/model/chat-v2/llmChatV2CachePolicy.d.ts +13 -0
  129. package/dist/types/model/chat-v2/llmChatV2NodeData.d.ts +13 -3
  130. package/dist/types/model/chat-v2/llmChatV2NodeMigration.d.ts +8 -0
  131. package/dist/types/model/chat-v2/llmChatV2NodeRuntime.d.ts +18 -4
  132. package/dist/types/model/chat-v2/llmInvocationCoordinator.d.ts +16 -0
  133. package/dist/types/model/chat-v2/llmInvocationDecision.d.ts +21 -0
  134. package/dist/types/model/chat-v2/llmInvocationJournal.d.ts +38 -0
  135. package/dist/types/model/chat-v2/llmInvocationPlan.d.ts +41 -0
  136. package/dist/types/model/chat-v2/llmInvocationProjections.d.ts +3 -0
  137. package/dist/types/model/chat-v2/llmInvocationResultProjector.d.ts +26 -0
  138. package/dist/types/model/chat-v2/llmModelCandidate.d.ts +18 -0
  139. package/dist/types/model/chat-v2/llmProfile.d.ts +7 -0
  140. package/dist/types/model/chat-v2/llmProfileBody.d.ts +20 -0
  141. package/dist/types/model/chat-v2/llmProfileFallback.d.ts +47 -0
  142. package/dist/types/model/chat-v2/llmProfileFieldRegistry.d.ts +189 -0
  143. package/dist/types/model/chat-v2/llmProfileTypes.d.ts +1 -1
  144. package/dist/types/model/chat-v2/llmResponseMaterializer.d.ts +20 -0
  145. package/dist/types/model/chat-v2/modelRegistry.d.ts +17 -187
  146. package/dist/types/model/chat-v2/parallelToolCalls.d.ts +3 -1
  147. package/dist/types/model/chat-v2/providerOptions.d.ts +11 -0
  148. package/dist/types/model/chat-v2/rivetToolRegistry.d.ts +11 -0
  149. package/dist/types/model/chat-v2/toolCallCodec.d.ts +7 -0
  150. package/dist/types/model/chat-v2/toolContinuation.d.ts +3 -3
  151. package/dist/types/model/chat-v2/toolHandlerResolver.d.ts +25 -0
  152. package/dist/types/model/chat-v2/toolRoundExecutor.d.ts +18 -0
  153. package/dist/types/model/nodes/AssembleMessageNode.d.ts +2 -1
  154. package/dist/types/model/nodes/ChatNode.d.ts +2 -1
  155. package/dist/types/model/nodes/DelegateFunctionCallNode.d.ts +2 -1
  156. package/dist/types/model/nodes/LLMChatV2Node.d.ts +2 -1
  157. package/dist/types/model/nodes/LLMProfileNode.d.ts +2 -1
  158. package/dist/types/model/nodes/PromptNode.d.ts +6 -3
  159. package/dist/types/model/nodes/ToolNode.d.ts +2 -1
  160. package/dist/types/model/nodes/toolCallDelegation.d.ts +10 -3
  161. package/dist/types/plugins/anthropic/anthropic.d.ts +1 -0
  162. package/dist/types/plugins/google/google.d.ts +14 -9
  163. package/dist/types/plugins/google/nodes/ChatGoogleNode.d.ts +2 -1
  164. package/dist/types/recording/RecordedEvents.d.ts +6 -0
  165. package/dist/types/utils/emitDetached.d.ts +3 -2
  166. package/dist/types/utils/openai.d.ts +49 -0
  167. package/dist/types/webAppRuntime.d.ts +4 -2
  168. package/package.json +2 -2
@@ -28,6 +28,7 @@ __export(webAppRuntime_exports, {
28
28
  copyUiGraphText: () => copyUiGraphText,
29
29
  createUiGraphActionExecutionController: () => createUiGraphActionExecutionController,
30
30
  createUiGraphChatHistoryFlushStatePatch: () => createUiGraphChatHistoryFlushStatePatch,
31
+ createUiGraphChatMessageRemovalStatePatch: () => createUiGraphChatMessageRemovalStatePatch,
31
32
  createUiGraphChatPinStatePatch: () => createUiGraphChatPinStatePatch,
32
33
  createUiGraphChatSubmissionStatePatch: () => createUiGraphChatSubmissionStatePatch,
33
34
  createUiGraphInteractionController: () => createUiGraphInteractionController,
@@ -45,17 +46,148 @@ __export(webAppRuntime_exports, {
45
46
  getUiGraphWebAppStorageKey: () => getUiGraphWebAppStorageKey,
46
47
  hasUiGraphChatPersistentStateChanged: () => hasUiGraphChatPersistentStateChanged,
47
48
  highlightUiGraphChatSearchMatches: () => highlightUiGraphChatSearchMatches,
49
+ isAgentResponseTrace: () => isAgentResponseTrace,
48
50
  loadUiGraphChatPersistentState: () => loadUiGraphChatPersistentState,
51
+ loadUiGraphResponseTrace: () => loadUiGraphResponseTrace,
49
52
  loadUiGraphWebAppStorage: () => loadUiGraphWebAppStorage,
50
53
  observeUiGraphOutputResizeBounds: () => observeUiGraphOutputResizeBounds,
51
54
  parseRivetWebAppClientMessage: () => parseRivetWebAppClientMessage,
52
55
  parseRivetWebAppServerMessage: () => parseRivetWebAppServerMessage,
56
+ pruneUiGraphResponseTraces: () => pruneUiGraphResponseTraces,
53
57
  revealUiGraphChatElement: () => revealUiGraphChatElement,
54
58
  revealUiGraphChatSearchMatch: () => revealUiGraphChatSearchMatch,
55
- saveUiGraphChatPersistentState: () => saveUiGraphChatPersistentState
59
+ saveUiGraphChatPersistentState: () => saveUiGraphChatPersistentState,
60
+ saveUiGraphResponseTrace: () => saveUiGraphResponseTrace
56
61
  });
57
62
  module.exports = __toCommonJS(webAppRuntime_exports);
58
63
 
64
+ // src/model/AgentResponseTrace.ts
65
+ var AGENT_RESPONSE_TRACE_SCHEMA_VERSION = 1;
66
+ var AGENT_RESPONSE_TRACE_MAX_MODEL_CALLS = 250;
67
+ var AGENT_RESPONSE_TRACE_MAX_TOOL_CALLS = 500;
68
+ function isAgentResponseTrace(value) {
69
+ if (!isRecord(value) || value.schemaVersion !== AGENT_RESPONSE_TRACE_SCHEMA_VERSION) return false;
70
+ if (!hasOnlyKeys(value, [
71
+ "schemaVersion",
72
+ "traceId",
73
+ "scope",
74
+ "rootRunId",
75
+ "graphRunId",
76
+ "graphId",
77
+ "nodeId",
78
+ "processId",
79
+ "startedAt",
80
+ "responseReadyAt",
81
+ "finishedAt",
82
+ "durationMs",
83
+ "status",
84
+ "backgroundWorkPending",
85
+ "summary",
86
+ "modelCalls",
87
+ "toolCalls",
88
+ "omittedModelCallCount",
89
+ "omittedToolCallCount"
90
+ ]))
91
+ return false;
92
+ if (typeof value.traceId !== "string" || typeof value.rootRunId !== "string") return false;
93
+ if (typeof value.graphRunId !== "string" || typeof value.graphId !== "string") return false;
94
+ if (value.scope !== "response" && value.scope !== "llm-invocation") return false;
95
+ if (!isRecord(value.summary) || !Array.isArray(value.modelCalls) || !Array.isArray(value.toolCalls)) return false;
96
+ if (value.modelCalls.length > AGENT_RESPONSE_TRACE_MAX_MODEL_CALLS) return false;
97
+ if (value.toolCalls.length > AGENT_RESPONSE_TRACE_MAX_TOOL_CALLS) return false;
98
+ return isOptionalString(value.nodeId) && isOptionalString(value.processId) && isOptionalNonNegativeFiniteNumber(value.startedAt) && isOptionalNonNegativeFiniteNumber(value.responseReadyAt) && isOptionalNonNegativeFiniteNumber(value.finishedAt) && isOptionalNonNegativeFiniteNumber(value.durationMs) && isOptionalBoolean(value.backgroundWorkPending) && ["running", "response-ready", "completed", "error", "aborted", "unavailable"].includes(String(value.status)) && isNonNegativeInteger(value.omittedModelCallCount) && isNonNegativeInteger(value.omittedToolCallCount) && isAgentTraceSummary(value.summary) && value.modelCalls.every(isAgentModelCallTrace) && value.toolCalls.every(isAgentToolCallTrace);
99
+ }
100
+ function isAgentModelCallTrace(value) {
101
+ return isRecord(value) && hasOnlyKeys(value, [
102
+ "callId",
103
+ "nodeId",
104
+ "processId",
105
+ "provider",
106
+ "model",
107
+ "customProviderApi",
108
+ "outcome",
109
+ "attemptIndex",
110
+ "profileIndex",
111
+ "roundIndex",
112
+ "startedAt",
113
+ "durationMs",
114
+ "finishReason",
115
+ "usage",
116
+ "pricing"
117
+ ]) && typeof value.callId === "string" && typeof value.nodeId === "string" && typeof value.processId === "string" && typeof value.provider === "string" && typeof value.model === "string" && (value.customProviderApi === void 0 || value.customProviderApi === "completions" || value.customProviderApi === "responses") && (value.outcome === "success" || value.outcome === "provider-failure" || value.outcome === "aborted") && isNonNegativeInteger(value.attemptIndex) && isOptionalNonNegativeInteger(value.profileIndex) && isOptionalNonNegativeInteger(value.roundIndex) && isOptionalNonNegativeFiniteNumber(value.startedAt) && isOptionalNonNegativeFiniteNumber(value.durationMs) && isOptionalString(value.finishReason) && (value.usage === void 0 || isAgentTraceUsage(value.usage)) && isAgentTracePricing(value.pricing);
118
+ }
119
+ function isAgentToolCallTrace(value) {
120
+ const hasKnownOutcome = isRecord(value) && (value.outcome === "success" || value.outcome === "passthrough-error" || value.outcome === "failure" || value.outcome === "aborted");
121
+ return isRecord(value) && hasOnlyKeys(value, [
122
+ "toolCallId",
123
+ "toolName",
124
+ "sourceNodeId",
125
+ "sourceProcessId",
126
+ "resultOwner",
127
+ "handlerKind",
128
+ "handlerGraphId",
129
+ "handlerName",
130
+ "outcome",
131
+ "startedAt",
132
+ "durationMs"
133
+ ]) && isOptionalString(value.toolCallId) && typeof value.toolName === "string" && typeof value.sourceNodeId === "string" && typeof value.sourceProcessId === "string" && (value.resultOwner === void 0 || isAgentToolResultOutcome(value.outcome) && isAgentToolResultOwner(value.resultOwner)) && (value.handlerKind === "graph" || value.handlerKind === "external" || value.handlerKind === "unknown") && isOptionalString(value.handlerGraphId) && isOptionalString(value.handlerName) && hasKnownOutcome && isOptionalNonNegativeFiniteNumber(value.startedAt) && isOptionalNonNegativeFiniteNumber(value.durationMs);
134
+ }
135
+ function isAgentToolResultOutcome(value) {
136
+ return value === "success" || value === "passthrough-error";
137
+ }
138
+ function isAgentToolResultOwner(value) {
139
+ return isRecord(value) && hasOnlyKeys(value, ["nodeId", "processId", "outputPortId"]) && typeof value.nodeId === "string" && typeof value.processId === "string" && typeof value.outputPortId === "string";
140
+ }
141
+ function isAgentTraceSummary(value) {
142
+ return hasOnlyKeys(value, [
143
+ "modelCallCount",
144
+ "toolCallCount",
145
+ "retryCount",
146
+ "fallbackCount",
147
+ "promptTokens",
148
+ "completionTokens",
149
+ "totalTokens",
150
+ "cachedTokens",
151
+ "reasoningTokens",
152
+ "knownCostUsd",
153
+ "costStatus"
154
+ ]) && isNonNegativeInteger(value.modelCallCount) && isNonNegativeInteger(value.toolCallCount) && isNonNegativeInteger(value.retryCount) && isNonNegativeInteger(value.fallbackCount) && isOptionalNonNegativeFiniteNumber(value.promptTokens) && isOptionalNonNegativeFiniteNumber(value.completionTokens) && isOptionalNonNegativeFiniteNumber(value.totalTokens) && isOptionalNonNegativeFiniteNumber(value.cachedTokens) && isOptionalNonNegativeFiniteNumber(value.reasoningTokens) && isNonNegativeFiniteNumber(value.knownCostUsd) && (value.costStatus === "known" || value.costStatus === "partial" || value.costStatus === "unknown");
155
+ }
156
+ function isAgentTraceUsage(value) {
157
+ return isRecord(value) && hasOnlyKeys(value, ["promptTokens", "completionTokens", "totalTokens", "cachedTokens", "reasoningTokens"]) && Object.values(value).every(isNonNegativeFiniteNumber);
158
+ }
159
+ function isAgentTracePricing(value) {
160
+ return isRecord(value) && hasOnlyKeys(value, ["status", "costUsd"]) && (value.status === "known" || value.status === "unknown") && isOptionalNonNegativeFiniteNumber(value.costUsd);
161
+ }
162
+ function hasOnlyKeys(value, allowed) {
163
+ const allowedKeys = new Set(allowed);
164
+ return Object.keys(value).every((key) => allowedKeys.has(key));
165
+ }
166
+ function isFiniteNumber(value) {
167
+ return typeof value === "number" && Number.isFinite(value);
168
+ }
169
+ function isNonNegativeFiniteNumber(value) {
170
+ return isFiniteNumber(value) && value >= 0;
171
+ }
172
+ function isOptionalNonNegativeFiniteNumber(value) {
173
+ return value === void 0 || isNonNegativeFiniteNumber(value);
174
+ }
175
+ function isNonNegativeInteger(value) {
176
+ return Number.isInteger(value) && value >= 0;
177
+ }
178
+ function isOptionalNonNegativeInteger(value) {
179
+ return value === void 0 || isNonNegativeInteger(value);
180
+ }
181
+ function isOptionalString(value) {
182
+ return value === void 0 || typeof value === "string";
183
+ }
184
+ function isOptionalBoolean(value) {
185
+ return value === void 0 || typeof value === "boolean";
186
+ }
187
+ function isRecord(value) {
188
+ return typeof value === "object" && value != null && !Array.isArray(value);
189
+ }
190
+
59
191
  // src/utils/newId.ts
60
192
  var import_non_secure = require("nanoid/non-secure");
61
193
 
@@ -124,6 +256,22 @@ function createUiGraphChatPinStatePatch(componentId, state, messageIndex) {
124
256
  [pinsStateKey]: pinnedIndexes.includes(messageIndex) ? pinnedIndexes.filter((index) => index !== messageIndex) : [...pinnedIndexes, messageIndex].sort((left, right) => left - right)
125
257
  };
126
258
  }
259
+ function createUiGraphChatMessageRemovalStatePatch(componentId, state, messageIndex) {
260
+ const messages = getUiGraphChatMessages(componentId, state);
261
+ if (!Number.isSafeInteger(messageIndex) || messageIndex < 0 || messageIndex >= messages.length) {
262
+ return void 0;
263
+ }
264
+ const pinsStateKey = getUiGraphChatPinsStateKey(componentId);
265
+ const pinnedIndexes = getUiGraphChatPins(componentId, state).map((pin) => pin.messageIndex);
266
+ const remainingPinnedIndexes = pinnedIndexes.flatMap((pinnedIndex) => {
267
+ if (pinnedIndex === messageIndex) return [];
268
+ return [pinnedIndex > messageIndex ? pinnedIndex - 1 : pinnedIndex];
269
+ });
270
+ return {
271
+ [getUiGraphChatMessagesStateKey(componentId)]: messages.filter((_message, index) => index !== messageIndex),
272
+ [pinsStateKey]: remainingPinnedIndexes
273
+ };
274
+ }
127
275
  function createUiGraphChatHistoryFlushStatePatch(componentId) {
128
276
  return {
129
277
  [getUiGraphChatMessagesStateKey(componentId)]: [],
@@ -392,7 +540,7 @@ function createUiGraphInteractionController(initialUiGraph, options = {}) {
392
540
  }
393
541
  }
394
542
  };
395
- const stampChatResponseAtBrowserReceipt = (component, statePatch) => {
543
+ const stampChatResponseAtBrowserReceipt = (component, statePatch, responseTrace) => {
396
544
  if (component.type !== "chat" || !statePatch) {
397
545
  return statePatch;
398
546
  }
@@ -409,7 +557,11 @@ function createUiGraphInteractionController(initialUiGraph, options = {}) {
409
557
  ...statePatch,
410
558
  [messagesStateKey]: [
411
559
  ...messages.slice(0, -1),
412
- { ...lastMessage, timestamp: (/* @__PURE__ */ new Date()).toISOString() }
560
+ {
561
+ ...lastMessage,
562
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
563
+ ...component.allowResponseInspection && responseTrace != null ? { responseTraceId: responseTrace.traceId } : {}
564
+ }
413
565
  ]
414
566
  };
415
567
  };
@@ -473,7 +625,7 @@ function createUiGraphInteractionController(initialUiGraph, options = {}) {
473
625
  }
474
626
  const statePatch = actionController.resolveStatePatch(
475
627
  execution,
476
- stampChatResponseAtBrowserReceipt(component, result.statePatch)
628
+ stampChatResponseAtBrowserReceipt(component, result.statePatch, result.responseTrace)
477
629
  );
478
630
  if (statePatch) {
479
631
  const nextState = applyUiGraphStatePatch(state, statePatch);
@@ -791,17 +943,17 @@ function applyUiGraphStatePatch(state, statePatch) {
791
943
  // src/model/UiGraphActionProtocol.ts
792
944
  var RIVET_WEB_APP_ACTION_PROTOCOL_VERSION = 1;
793
945
  function parseRivetWebAppClientMessage(value) {
794
- if (!isRecord(value) || typeof value.type !== "string") return void 0;
946
+ if (!isRecord2(value) || typeof value.type !== "string") return void 0;
795
947
  switch (value.type) {
796
948
  case "client.hello":
797
949
  return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION ? { type: value.type, protocolVersion: value.protocolVersion } : void 0;
798
950
  case "action.start":
799
- return isNonEmptyString(value.requestId) && isNonEmptyString(value.componentId) && isRecord(value.state) && (value.storage == null || isRecord(value.storage)) && (value.revisionKey == null || typeof value.revisionKey === "string") ? {
951
+ return isNonEmptyString(value.requestId) && isNonEmptyString(value.componentId) && isRecord2(value.state) && (value.storage == null || isRecord2(value.storage)) && (value.revisionKey == null || typeof value.revisionKey === "string") ? {
800
952
  type: value.type,
801
953
  requestId: value.requestId,
802
954
  componentId: value.componentId,
803
955
  state: value.state,
804
- ...isRecord(value.storage) ? { storage: value.storage } : {},
956
+ ...isRecord2(value.storage) ? { storage: value.storage } : {},
805
957
  ...typeof value.revisionKey === "string" ? { revisionKey: value.revisionKey } : {}
806
958
  } : void 0;
807
959
  case "action.cancel":
@@ -813,7 +965,7 @@ function parseRivetWebAppClientMessage(value) {
813
965
  }
814
966
  }
815
967
  function parseRivetWebAppServerMessage(value) {
816
- if (!isRecord(value) || typeof value.type !== "string") return void 0;
968
+ if (!isRecord2(value) || typeof value.type !== "string") return void 0;
817
969
  if (value.type === "server.ready") {
818
970
  return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION ? { type: value.type, protocolVersion: value.protocolVersion } : void 0;
819
971
  }
@@ -845,7 +997,7 @@ function parseRivetWebAppServerMessage(value) {
845
997
  case "action.cancelled":
846
998
  return { type: value.type, ...base };
847
999
  case "action.progress": {
848
- if (!isRecord(value.progress)) return void 0;
1000
+ if (!isRecord2(value.progress)) return void 0;
849
1001
  const progress = normalizeGraphProgress({
850
1002
  message: typeof value.progress.message === "string" ? value.progress.message : void 0,
851
1003
  percent: typeof value.progress.percent === "number" ? value.progress.percent : void 0
@@ -853,11 +1005,12 @@ function parseRivetWebAppServerMessage(value) {
853
1005
  return progress ? { type: value.type, ...base, progress } : void 0;
854
1006
  }
855
1007
  case "action.completed":
856
- return isRecord(value.statePatch) && (value.storagePatch == null || isRecord(value.storagePatch)) ? {
1008
+ return isRecord2(value.statePatch) && (value.storagePatch == null || isRecord2(value.storagePatch)) ? {
857
1009
  type: value.type,
858
1010
  ...base,
859
1011
  statePatch: value.statePatch,
860
- ...isRecord(value.storagePatch) ? { storagePatch: value.storagePatch } : {}
1012
+ ...isRecord2(value.storagePatch) ? { storagePatch: value.storagePatch } : {},
1013
+ ...isAgentResponseTrace(value.responseTrace) ? { responseTrace: value.responseTrace } : {}
861
1014
  } : void 0;
862
1015
  case "action.failed":
863
1016
  return isNonEmptyString(value.error) ? {
@@ -875,7 +1028,7 @@ function parseRivetWebAppServerMessage(value) {
875
1028
  function hasRunEventBase(value) {
876
1029
  return isNonEmptyString(value.requestId) && isNonEmptyString(value.runId) && typeof value.sequence === "number" && Number.isSafeInteger(value.sequence) && value.sequence > 0;
877
1030
  }
878
- function isRecord(value) {
1031
+ function isRecord2(value) {
879
1032
  return typeof value === "object" && value != null && !Array.isArray(value);
880
1033
  }
881
1034
  function isNonEmptyString(value) {
@@ -891,6 +1044,10 @@ var CHAT_SEARCH_MATCH_CLASS = "rivet-web-app-chat-search-match";
891
1044
  var CHAT_SEARCH_ACTIVE_MATCH_CLASS = "rivet-web-app-chat-search-match-active";
892
1045
  var UI_GRAPH_CHAT_STORAGE_PREFIX = "rivet-web-app-chat-state:v1";
893
1046
  var UI_GRAPH_APP_STORAGE_PREFIX = "rivet-web-app-storage:v1";
1047
+ var UI_GRAPH_RESPONSE_TRACE_STORAGE_PREFIX = "rivet-web-app-response-traces:v1";
1048
+ var UI_GRAPH_RESPONSE_TRACE_LIMIT = 100;
1049
+ var responseTraceMemoryFallback = /* @__PURE__ */ new Map();
1050
+ var unsyncedResponseTraceKeysByStorage = /* @__PURE__ */ new WeakMap();
894
1051
  var escapeRegularExpression = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
895
1052
  var getPixelValue = (value, fallback) => {
896
1053
  const parsed = Number.parseFloat(value);
@@ -901,7 +1058,7 @@ var setStyleProperty = (element, property, value) => {
901
1058
  element.style.setProperty(property, value);
902
1059
  }
903
1060
  };
904
- var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
1061
+ var isRecord3 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
905
1062
  var getDefaultUiGraphBrowserStorage = () => {
906
1063
  var _a;
907
1064
  try {
@@ -945,21 +1102,37 @@ function getUiGraphChatMessagePresentations(messages, options = {}) {
945
1102
  dateKey: dateKeyFormatter.format(timestamp),
946
1103
  dateTime: timestamp.toISOString(),
947
1104
  dateLabel: dateFormatter.format(timestamp),
948
- timeLabel: timeFormatter.format(timestamp)
1105
+ timeLabel: timeFormatter.format(timestamp),
1106
+ timestamp
949
1107
  };
950
1108
  });
951
1109
  const hasMultipleDates = new Set(entries.flatMap((entry) => entry ? [entry.dateKey] : [])).size > 1;
952
1110
  let previousDateKey;
953
- return entries.map((entry) => {
1111
+ let previousUserTimestamp;
1112
+ return entries.map((entry, index) => {
1113
+ const message = messages[index];
1114
+ if (message.role === "user") {
1115
+ previousUserTimestamp = entry == null ? void 0 : entry.timestamp;
1116
+ }
954
1117
  if (!entry) return {};
955
1118
  const dateSeparator = hasMultipleDates && previousDateKey != null && previousDateKey !== entry.dateKey ? { dateTime: entry.dateTime, label: entry.dateLabel } : void 0;
956
1119
  previousDateKey = entry.dateKey;
1120
+ const elapsedSincePreviousUserMessage = message.role === "assistant" && previousUserTimestamp ? formatUiGraphChatElapsedSeconds(entry.timestamp.getTime() - previousUserTimestamp.getTime()) : void 0;
957
1121
  return {
958
1122
  ...dateSeparator ? { dateSeparator } : {},
959
- timestamp: { dateTime: entry.dateTime, label: entry.timeLabel }
1123
+ timestamp: {
1124
+ dateTime: entry.dateTime,
1125
+ ...elapsedSincePreviousUserMessage ? { elapsedSincePreviousUserMessage } : {},
1126
+ label: entry.timeLabel
1127
+ }
960
1128
  };
961
1129
  });
962
1130
  }
1131
+ var formatUiGraphChatElapsedSeconds = (elapsedMilliseconds) => {
1132
+ if (!Number.isFinite(elapsedMilliseconds) || elapsedMilliseconds < 0) return void 0;
1133
+ const seconds = Math.round(elapsedMilliseconds / 1e3 * 10) / 10;
1134
+ return `${Number.isInteger(seconds) ? seconds : seconds.toFixed(1)} seconds after previous user message`;
1135
+ };
963
1136
  var parseUiGraphChatTimestamp = (value) => {
964
1137
  if (typeof value !== "string") return void 0;
965
1138
  const timestamp = new Date(value);
@@ -989,6 +1162,103 @@ function getUiGraphWebAppStorageKey(uiGraph, location = getDefaultUiGraphStorage
989
1162
  encodeURIComponent(uiGraph.id)
990
1163
  ].join(":");
991
1164
  }
1165
+ function getUiGraphResponseTraceStorageKey(uiGraph, componentId, location = getDefaultUiGraphStorageLocation()) {
1166
+ if (!(location == null ? void 0 : location.origin)) return void 0;
1167
+ const pathname = location.pathname.replace(/\/+$/, "") || "/";
1168
+ return [
1169
+ UI_GRAPH_RESPONSE_TRACE_STORAGE_PREFIX,
1170
+ encodeURIComponent(location.origin),
1171
+ encodeURIComponent(pathname),
1172
+ encodeURIComponent(uiGraph.id),
1173
+ encodeURIComponent(componentId)
1174
+ ].join(":");
1175
+ }
1176
+ var isUiGraphResponseInspectionEnabled = (uiGraph, componentId) => uiGraph.components.some(
1177
+ (component) => component.type === "chat" && component.id === componentId && component.allowResponseInspection === true
1178
+ );
1179
+ var removeUiGraphChatResponseTraceIds = (messages) => messages.map((message) => {
1180
+ if (message.responseTraceId == null) return message;
1181
+ const sanitized = { ...message };
1182
+ delete sanitized.responseTraceId;
1183
+ return sanitized;
1184
+ });
1185
+ var purgeDisabledUiGraphResponseTraces = (uiGraph, storage, location) => {
1186
+ for (const component of uiGraph.components) {
1187
+ if (component.type !== "chat" || component.allowResponseInspection === true) continue;
1188
+ const key = getUiGraphResponseTraceStorageKey(uiGraph, component.id, location) ?? `memory:${uiGraph.id}:${component.id}`;
1189
+ persistUiGraphResponseTraces(key, [], storage);
1190
+ }
1191
+ };
1192
+ function saveUiGraphResponseTrace(uiGraph, componentId, trace, storage = getDefaultUiGraphBrowserStorage(), location) {
1193
+ if (!isAgentResponseTrace(trace)) return;
1194
+ if (!isUiGraphResponseInspectionEnabled(uiGraph, componentId)) {
1195
+ const key2 = getUiGraphResponseTraceStorageKey(uiGraph, componentId, location) ?? `memory:${uiGraph.id}:${componentId}`;
1196
+ persistUiGraphResponseTraces(key2, [], storage);
1197
+ return;
1198
+ }
1199
+ const key = getUiGraphResponseTraceStorageKey(uiGraph, componentId, location) ?? `memory:${uiGraph.id}:${componentId}`;
1200
+ const traces = loadUiGraphResponseTraces(uiGraph, componentId, storage, location).filter(
1201
+ (candidate) => candidate.traceId !== trace.traceId
1202
+ );
1203
+ traces.push(trace);
1204
+ const bounded = traces.slice(-UI_GRAPH_RESPONSE_TRACE_LIMIT);
1205
+ persistUiGraphResponseTraces(key, bounded, storage);
1206
+ }
1207
+ function loadUiGraphResponseTrace(uiGraph, componentId, traceId, storage = getDefaultUiGraphBrowserStorage(), location) {
1208
+ if (!isUiGraphResponseInspectionEnabled(uiGraph, componentId)) return void 0;
1209
+ return loadUiGraphResponseTraces(uiGraph, componentId, storage, location).find((trace) => trace.traceId === traceId);
1210
+ }
1211
+ function pruneUiGraphResponseTraces(uiGraph, state, storage = getDefaultUiGraphBrowserStorage(), location) {
1212
+ for (const component of uiGraph.components) {
1213
+ if (component.type !== "chat") continue;
1214
+ const referenced = component.allowResponseInspection === true ? new Set(
1215
+ getUiGraphChatMessages(component.id, state).flatMap(
1216
+ (message) => typeof message.responseTraceId === "string" ? [message.responseTraceId] : []
1217
+ )
1218
+ ) : /* @__PURE__ */ new Set();
1219
+ const key = getUiGraphResponseTraceStorageKey(uiGraph, component.id, location) ?? `memory:${uiGraph.id}:${component.id}`;
1220
+ const retained = loadUiGraphResponseTraces(uiGraph, component.id, storage, location).filter(
1221
+ (trace) => referenced.has(trace.traceId)
1222
+ );
1223
+ persistUiGraphResponseTraces(key, retained, storage);
1224
+ }
1225
+ }
1226
+ function persistUiGraphResponseTraces(key, traces, storage) {
1227
+ var _a;
1228
+ responseTraceMemoryFallback.set(key, traces);
1229
+ if (!storage || key.startsWith("memory:")) return;
1230
+ try {
1231
+ if (traces.length === 0) storage.removeItem(key);
1232
+ else storage.setItem(key, JSON.stringify(traces));
1233
+ (_a = unsyncedResponseTraceKeysByStorage.get(storage)) == null ? void 0 : _a.delete(key);
1234
+ } catch {
1235
+ const unsyncedKeys = unsyncedResponseTraceKeysByStorage.get(storage) ?? /* @__PURE__ */ new Set();
1236
+ unsyncedKeys.add(key);
1237
+ unsyncedResponseTraceKeysByStorage.set(storage, unsyncedKeys);
1238
+ }
1239
+ }
1240
+ function loadUiGraphResponseTraces(uiGraph, componentId, storage, location) {
1241
+ var _a;
1242
+ const key = getUiGraphResponseTraceStorageKey(uiGraph, componentId, location) ?? `memory:${uiGraph.id}:${componentId}`;
1243
+ if (storage && ((_a = unsyncedResponseTraceKeysByStorage.get(storage)) == null ? void 0 : _a.has(key))) {
1244
+ return responseTraceMemoryFallback.get(key) ?? [];
1245
+ }
1246
+ if (storage && !key.startsWith("memory:")) {
1247
+ try {
1248
+ const raw = storage.getItem(key);
1249
+ if (raw) {
1250
+ const parsed = JSON.parse(raw);
1251
+ if (Array.isArray(parsed)) {
1252
+ const traces = parsed.filter(isAgentResponseTrace).slice(-UI_GRAPH_RESPONSE_TRACE_LIMIT);
1253
+ responseTraceMemoryFallback.set(key, traces);
1254
+ return traces;
1255
+ }
1256
+ }
1257
+ } catch {
1258
+ }
1259
+ }
1260
+ return responseTraceMemoryFallback.get(key) ?? [];
1261
+ }
992
1262
  function loadUiGraphWebAppStorage(uiGraph, storage = getDefaultUiGraphBrowserStorage(), location) {
993
1263
  const key = getUiGraphWebAppStorageKey(uiGraph, location);
994
1264
  if (!key || !storage) return {};
@@ -996,7 +1266,7 @@ function loadUiGraphWebAppStorage(uiGraph, storage = getDefaultUiGraphBrowserSto
996
1266
  const serialized = storage.getItem(key);
997
1267
  if (!serialized) return {};
998
1268
  const value = JSON.parse(serialized);
999
- return isRecord2(value) ? value : {};
1269
+ return isRecord3(value) ? value : {};
1000
1270
  } catch {
1001
1271
  return {};
1002
1272
  }
@@ -1037,12 +1307,13 @@ function getUiGraphChatPersistentState(uiGraph, state) {
1037
1307
  const pinsStateKey = getUiGraphChatPinsStateKey(component.id);
1038
1308
  const draft = state[draftStateKey];
1039
1309
  const messages = getUiGraphChatMessages(component.id, state);
1310
+ const persistedMessages = component.allowResponseInspection === true ? messages : removeUiGraphChatResponseTraceIds(messages);
1040
1311
  const pins = getUiGraphChatPins(component.id, state).map((pin) => pin.messageIndex);
1041
1312
  if (typeof draft === "string" && draft) {
1042
1313
  persistentState[draftStateKey] = draft;
1043
1314
  }
1044
- if (messages.length > 0) {
1045
- persistentState[messagesStateKey] = messages;
1315
+ if (persistedMessages.length > 0) {
1316
+ persistentState[messagesStateKey] = persistedMessages;
1046
1317
  }
1047
1318
  if (pins.length > 0) {
1048
1319
  persistentState[pinsStateKey] = pins;
@@ -1067,12 +1338,13 @@ function loadUiGraphChatPersistentState(uiGraph, storage = getDefaultUiGraphBrow
1067
1338
  return {};
1068
1339
  }
1069
1340
  const storedState = JSON.parse(serializedState);
1070
- return isRecord2(storedState) ? getUiGraphChatPersistentState(uiGraph, storedState) : {};
1341
+ return isRecord3(storedState) ? getUiGraphChatPersistentState(uiGraph, storedState) : {};
1071
1342
  } catch {
1072
1343
  return {};
1073
1344
  }
1074
1345
  }
1075
1346
  function saveUiGraphChatPersistentState(uiGraph, state, storage = getDefaultUiGraphBrowserStorage(), location) {
1347
+ purgeDisabledUiGraphResponseTraces(uiGraph, storage, location);
1076
1348
  const key = getUiGraphChatStorageKey(uiGraph, location);
1077
1349
  if (!key || !storage) {
1078
1350
  return;
@@ -1310,6 +1582,7 @@ var revealUiGraphChatSearchMatch = revealUiGraphChatElement;
1310
1582
  copyUiGraphText,
1311
1583
  createUiGraphActionExecutionController,
1312
1584
  createUiGraphChatHistoryFlushStatePatch,
1585
+ createUiGraphChatMessageRemovalStatePatch,
1313
1586
  createUiGraphChatPinStatePatch,
1314
1587
  createUiGraphChatSubmissionStatePatch,
1315
1588
  createUiGraphInteractionController,
@@ -1327,13 +1600,17 @@ var revealUiGraphChatSearchMatch = revealUiGraphChatElement;
1327
1600
  getUiGraphWebAppStorageKey,
1328
1601
  hasUiGraphChatPersistentStateChanged,
1329
1602
  highlightUiGraphChatSearchMatches,
1603
+ isAgentResponseTrace,
1330
1604
  loadUiGraphChatPersistentState,
1605
+ loadUiGraphResponseTrace,
1331
1606
  loadUiGraphWebAppStorage,
1332
1607
  observeUiGraphOutputResizeBounds,
1333
1608
  parseRivetWebAppClientMessage,
1334
1609
  parseRivetWebAppServerMessage,
1610
+ pruneUiGraphResponseTraces,
1335
1611
  revealUiGraphChatElement,
1336
1612
  revealUiGraphChatSearchMatch,
1337
- saveUiGraphChatPersistentState
1613
+ saveUiGraphChatPersistentState,
1614
+ saveUiGraphResponseTrace
1338
1615
  });
1339
1616
  //# sourceMappingURL=webAppRuntime.cjs.map