@flamingo-stack/openframe-frontend-core 0.0.172 → 0.0.173-snapshot.20260513212448
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-AY3E5FKM.cjs → chunk-TX2C3PJM.cjs} +2227 -1649
- package/dist/chunk-TX2C3PJM.cjs.map +1 -0
- package/dist/{chunk-FEEPEOW2.js → chunk-X7AOT3WQ.js} +5770 -5192
- package/dist/chunk-X7AOT3WQ.js.map +1 -0
- package/dist/components/chat/approval-batch-message.d.ts +10 -0
- package/dist/components/chat/approval-batch-message.d.ts.map +1 -0
- package/dist/components/chat/chat-input.d.ts.map +1 -1
- package/dist/components/chat/chat-message-enhanced.d.ts.map +1 -1
- package/dist/components/chat/chat-message-list.d.ts.map +1 -1
- package/dist/components/chat/chat-message-loader.d.ts +23 -0
- package/dist/components/chat/chat-message-loader.d.ts.map +1 -0
- package/dist/components/chat/hooks/index.d.ts +1 -0
- package/dist/components/chat/hooks/index.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-collapsible.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-delayed-flag.d.ts +25 -0
- package/dist/components/chat/hooks/use-delayed-flag.d.ts.map +1 -0
- package/dist/components/chat/hooks/use-realtime-chunk-processor.d.ts.map +1 -1
- package/dist/components/chat/index.d.ts +3 -0
- package/dist/components/chat/index.d.ts.map +1 -1
- package/dist/components/chat/thinking-display.d.ts.map +1 -1
- package/dist/components/chat/tool-call-blocks.d.ts +18 -0
- package/dist/components/chat/tool-call-blocks.d.ts.map +1 -0
- package/dist/components/chat/tool-execution-display.d.ts +1 -1
- package/dist/components/chat/tool-execution-display.d.ts.map +1 -1
- package/dist/components/chat/types/api.types.d.ts +16 -1
- package/dist/components/chat/types/api.types.d.ts.map +1 -1
- package/dist/components/chat/types/component.types.d.ts +2 -2
- package/dist/components/chat/types/component.types.d.ts.map +1 -1
- package/dist/components/chat/types/message.types.d.ts +54 -1
- package/dist/components/chat/types/message.types.d.ts.map +1 -1
- package/dist/components/chat/types/network.types.d.ts +2 -0
- package/dist/components/chat/types/network.types.d.ts.map +1 -1
- package/dist/components/chat/types/processing.types.d.ts +14 -1
- package/dist/components/chat/types/processing.types.d.ts.map +1 -1
- package/dist/components/chat/utils/chunk-parser.d.ts.map +1 -1
- package/dist/components/chat/utils/extract-incomplete-message-state.d.ts.map +1 -1
- package/dist/components/chat/utils/index.d.ts +1 -0
- package/dist/components/chat/utils/index.d.ts.map +1 -1
- package/dist/components/chat/utils/message-segment-accumulator.d.ts +32 -4
- package/dist/components/chat/utils/message-segment-accumulator.d.ts.map +1 -1
- package/dist/components/chat/utils/process-historical-messages.d.ts +2 -1
- package/dist/components/chat/utils/process-historical-messages.d.ts.map +1 -1
- package/dist/components/chat/utils/tool-call-helpers.d.ts +38 -0
- package/dist/components/chat/utils/tool-call-helpers.d.ts.map +1 -0
- package/dist/components/features/index.cjs +2 -2
- package/dist/components/features/index.js +1 -1
- package/dist/components/index.cjs +14 -2
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.js +13 -1
- package/dist/components/navigation/index.cjs +2 -2
- package/dist/components/navigation/index.js +1 -1
- package/dist/components/ui/index.cjs +14 -2
- package/dist/components/ui/index.cjs.map +1 -1
- package/dist/components/ui/index.js +13 -1
- package/dist/components/ui/textarea.d.ts +11 -0
- package/dist/components/ui/textarea.d.ts.map +1 -1
- package/dist/index.cjs +14 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +13 -1
- package/package.json +3 -3
- package/src/components/chat/approval-batch-message.tsx +237 -0
- package/src/components/chat/chat-input.tsx +34 -106
- package/src/components/chat/chat-message-enhanced.tsx +11 -0
- package/src/components/chat/chat-message-list.tsx +100 -37
- package/src/components/chat/chat-message-loader.tsx +67 -0
- package/src/components/chat/hooks/index.ts +1 -0
- package/src/components/chat/hooks/use-collapsible.ts +10 -1
- package/src/components/chat/hooks/use-delayed-flag.ts +56 -0
- package/src/components/chat/hooks/use-realtime-chunk-processor.ts +109 -20
- package/src/components/chat/index.ts +3 -0
- package/src/components/chat/thinking-display.tsx +76 -12
- package/src/components/chat/tool-call-blocks.tsx +58 -0
- package/src/components/chat/tool-execution-display.tsx +60 -81
- package/src/components/chat/types/api.types.ts +22 -3
- package/src/components/chat/types/component.types.ts +2 -2
- package/src/components/chat/types/message.types.ts +58 -1
- package/src/components/chat/types/network.types.ts +2 -0
- package/src/components/chat/types/processing.types.ts +13 -2
- package/src/components/chat/utils/chunk-parser.ts +40 -5
- package/src/components/chat/utils/extract-incomplete-message-state.ts +19 -1
- package/src/components/chat/utils/index.ts +3 -0
- package/src/components/chat/utils/message-segment-accumulator.ts +136 -12
- package/src/components/chat/utils/process-historical-messages.ts +88 -13
- package/src/components/chat/utils/tool-call-helpers.ts +105 -0
- package/src/components/ui/textarea.tsx +107 -25
- package/dist/chunk-AY3E5FKM.cjs.map +0 -1
- package/dist/chunk-FEEPEOW2.js.map +0 -1
|
@@ -2,7 +2,26 @@
|
|
|
2
2
|
* Utilities for parsing NATS chunks into structured actions
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { MESSAGE_TYPE, type ChunkData, type ParsedChunkAction, type ToolExecutionSegment } from '../types'
|
|
5
|
+
import { MESSAGE_TYPE, type ChunkData, type ParsedChunkAction, type ToolExecutionSegment, type PendingToolCallData } from '../types'
|
|
6
|
+
|
|
7
|
+
function normalizeToolCalls(raw: unknown): PendingToolCallData[] {
|
|
8
|
+
if (!Array.isArray(raw)) return []
|
|
9
|
+
return raw
|
|
10
|
+
.filter((item): item is Record<string, any> => !!item && typeof item === 'object')
|
|
11
|
+
.map((item) => ({
|
|
12
|
+
toolExecutionRequestId: String(item.toolExecutionRequestId ?? ''),
|
|
13
|
+
toolName: String(item.toolName ?? ''),
|
|
14
|
+
toolTitle: typeof item.toolTitle === 'string' ? item.toolTitle : undefined,
|
|
15
|
+
toolExplanation: typeof item.toolExplanation === 'string' ? item.toolExplanation : undefined,
|
|
16
|
+
toolType: typeof item.toolType === 'string' ? item.toolType : undefined,
|
|
17
|
+
requiresApproval: item.requiresApproval === true,
|
|
18
|
+
approvalType: typeof item.approvalType === 'string' ? item.approvalType : null,
|
|
19
|
+
toolCallArguments:
|
|
20
|
+
item.toolCallArguments && typeof item.toolCallArguments === 'object'
|
|
21
|
+
? (item.toolCallArguments as Record<string, any>)
|
|
22
|
+
: null,
|
|
23
|
+
}))
|
|
24
|
+
}
|
|
6
25
|
|
|
7
26
|
/**
|
|
8
27
|
* Parse a raw NATS chunk into a structured action
|
|
@@ -56,10 +75,11 @@ export function parseChunkToAction(chunk: unknown): ParsedChunkAction | null {
|
|
|
56
75
|
integratedToolType: data.integratedToolType || '',
|
|
57
76
|
toolFunction: data.toolFunction || '',
|
|
58
77
|
parameters: data.parameters,
|
|
78
|
+
toolExecutionRequestId: typeof data.toolExecutionRequestId === 'string' ? data.toolExecutionRequestId : undefined,
|
|
59
79
|
}
|
|
60
80
|
}
|
|
61
81
|
}
|
|
62
|
-
|
|
82
|
+
|
|
63
83
|
case MESSAGE_TYPE.EXECUTED_TOOL:
|
|
64
84
|
return {
|
|
65
85
|
action: 'tool_execution',
|
|
@@ -72,18 +92,33 @@ export function parseChunkToAction(chunk: unknown): ParsedChunkAction | null {
|
|
|
72
92
|
parameters: data.parameters,
|
|
73
93
|
result: data.result,
|
|
74
94
|
success: data.success,
|
|
95
|
+
toolExecutionRequestId: typeof data.toolExecutionRequestId === 'string' ? data.toolExecutionRequestId : undefined,
|
|
75
96
|
}
|
|
76
97
|
}
|
|
77
98
|
}
|
|
78
99
|
|
|
79
|
-
case MESSAGE_TYPE.APPROVAL_REQUEST:
|
|
100
|
+
case MESSAGE_TYPE.APPROVAL_REQUEST: {
|
|
101
|
+
const requestId = data.approvalRequestId || data.approval_request_id || ''
|
|
102
|
+
const approvalType = data.approvalType || 'USER'
|
|
103
|
+
const toolCalls = normalizeToolCalls(data.toolCalls)
|
|
104
|
+
|
|
105
|
+
if (toolCalls.length > 0) {
|
|
106
|
+
return {
|
|
107
|
+
action: 'approval_batch',
|
|
108
|
+
requestId,
|
|
109
|
+
approvalType,
|
|
110
|
+
toolCalls,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
80
114
|
return {
|
|
81
115
|
action: 'approval_request',
|
|
82
|
-
requestId
|
|
116
|
+
requestId,
|
|
83
117
|
command: data.command || '',
|
|
84
118
|
explanation: data.explanation,
|
|
85
|
-
approvalType
|
|
119
|
+
approvalType,
|
|
86
120
|
}
|
|
121
|
+
}
|
|
87
122
|
|
|
88
123
|
case MESSAGE_TYPE.APPROVAL_RESULT:
|
|
89
124
|
return {
|
|
@@ -33,7 +33,9 @@ export function extractIncompleteMessageState(
|
|
|
33
33
|
switch (segment.type) {
|
|
34
34
|
case 'tool_execution':
|
|
35
35
|
if (segment.data.type === 'EXECUTING_TOOL') {
|
|
36
|
-
const toolKey =
|
|
36
|
+
const toolKey =
|
|
37
|
+
segment.data.toolExecutionRequestId ||
|
|
38
|
+
`${segment.data.integratedToolType}-${segment.data.toolFunction}`
|
|
37
39
|
executingTools.set(toolKey, {
|
|
38
40
|
integratedToolType: segment.data.integratedToolType,
|
|
39
41
|
toolFunction: segment.data.toolFunction,
|
|
@@ -54,6 +56,22 @@ export function extractIncompleteMessageState(
|
|
|
54
56
|
}
|
|
55
57
|
break
|
|
56
58
|
|
|
59
|
+
case 'approval_batch': {
|
|
60
|
+
// Treat a batch as in-progress until every tool call has a
|
|
61
|
+
// `done` execution OR the batch was rejected. Otherwise the realtime
|
|
62
|
+
// accumulator won't hold the segment and post-approval EXECUTED_TOOL
|
|
63
|
+
// chunks won't be able to merge into it via `applyExecutionToBatch`.
|
|
64
|
+
const allDone =
|
|
65
|
+
!!segment.data.executions &&
|
|
66
|
+
segment.data.toolCalls.every(
|
|
67
|
+
(c) => segment.data.executions?.[c.toolExecutionRequestId]?.status === 'done',
|
|
68
|
+
)
|
|
69
|
+
if (segment.status !== 'rejected' && !allDone) {
|
|
70
|
+
hasIncompleteState = true
|
|
71
|
+
}
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
case 'context_compaction':
|
|
58
76
|
if (segment.status === 'started') {
|
|
59
77
|
hasIncompleteState = true
|
|
@@ -12,9 +12,12 @@ import type {
|
|
|
12
12
|
MessageSegment,
|
|
13
13
|
ToolExecutionSegment,
|
|
14
14
|
ApprovalRequestSegment,
|
|
15
|
+
ApprovalBatchSegment,
|
|
16
|
+
ApprovalBatchExecutionState,
|
|
15
17
|
ContextCompactionSegment,
|
|
16
18
|
ErrorSegment,
|
|
17
19
|
PendingApproval,
|
|
20
|
+
PendingToolCallData,
|
|
18
21
|
AccumulatorState,
|
|
19
22
|
ChatApprovalStatus,
|
|
20
23
|
} from '../types'
|
|
@@ -136,13 +139,27 @@ export class MessageSegmentAccumulator {
|
|
|
136
139
|
}
|
|
137
140
|
|
|
138
141
|
/**
|
|
139
|
-
* Add a tool execution segment
|
|
140
|
-
*
|
|
142
|
+
* Add a tool execution segment.
|
|
143
|
+
*
|
|
144
|
+
* Routing:
|
|
145
|
+
* 1) If `toolExecutionRequestId` matches a tool call inside an existing
|
|
146
|
+
* `approval_batch` segment, merge the state into that batch's
|
|
147
|
+
* `executions` map (no standalone segment is pushed).
|
|
148
|
+
* 2) Otherwise: pair EXECUTING ↔ EXECUTED by `toolExecutionRequestId`.
|
|
149
|
+
* If no id is present (older backends), fall back to
|
|
150
|
+
* `(integratedToolType, toolFunction)` so repeat calls of the same
|
|
151
|
+
* function don't all bucket under one key.
|
|
141
152
|
*/
|
|
142
153
|
addToolExecution(segment: ToolExecutionSegment): MessageSegment[] {
|
|
143
154
|
const toolData = segment.data
|
|
144
|
-
const
|
|
145
|
-
|
|
155
|
+
const execId = toolData.toolExecutionRequestId
|
|
156
|
+
|
|
157
|
+
if (execId && this.applyExecutionToBatch(execId, toolData)) {
|
|
158
|
+
return this.getSegments()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const toolKey = execId || `${toolData.integratedToolType}-${toolData.toolFunction}`
|
|
162
|
+
|
|
146
163
|
if (toolData.type === 'EXECUTING_TOOL') {
|
|
147
164
|
this.executingTools.set(toolKey, {
|
|
148
165
|
integratedToolType: toolData.integratedToolType,
|
|
@@ -155,10 +172,12 @@ export class MessageSegmentAccumulator {
|
|
|
155
172
|
(s): s is ToolExecutionSegment =>
|
|
156
173
|
s.type === 'tool_execution' &&
|
|
157
174
|
s.data.type === 'EXECUTING_TOOL' &&
|
|
158
|
-
|
|
159
|
-
|
|
175
|
+
(execId
|
|
176
|
+
? s.data.toolExecutionRequestId === execId
|
|
177
|
+
: s.data.integratedToolType === toolData.integratedToolType &&
|
|
178
|
+
s.data.toolFunction === toolData.toolFunction),
|
|
160
179
|
)
|
|
161
|
-
|
|
180
|
+
|
|
162
181
|
const executingTool = this.executingTools.get(toolKey)
|
|
163
182
|
const mergedSegment: ToolExecutionSegment = {
|
|
164
183
|
type: 'tool_execution',
|
|
@@ -167,19 +186,50 @@ export class MessageSegmentAccumulator {
|
|
|
167
186
|
parameters: toolData.parameters || executingTool?.parameters,
|
|
168
187
|
}
|
|
169
188
|
}
|
|
170
|
-
|
|
189
|
+
|
|
171
190
|
if (existingIndex !== -1) {
|
|
172
191
|
this.segments[existingIndex] = mergedSegment
|
|
173
192
|
} else {
|
|
174
193
|
this.segments.push(mergedSegment)
|
|
175
194
|
}
|
|
176
|
-
|
|
195
|
+
|
|
177
196
|
this.executingTools.delete(toolKey)
|
|
178
197
|
}
|
|
179
|
-
|
|
198
|
+
|
|
180
199
|
return this.getSegments()
|
|
181
200
|
}
|
|
182
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Try to merge a tool execution event into an existing approval_batch
|
|
204
|
+
* segment whose `toolCalls` contains the same `toolExecutionRequestId`.
|
|
205
|
+
* Returns true when a batch was updated, false when no batch matches.
|
|
206
|
+
*/
|
|
207
|
+
private applyExecutionToBatch(execId: string, toolData: ToolExecutionSegment['data']): boolean {
|
|
208
|
+
let matched = false
|
|
209
|
+
this.segments = this.segments.map((seg) => {
|
|
210
|
+
if (matched) return seg
|
|
211
|
+
if (seg.type !== 'approval_batch') return seg
|
|
212
|
+
const hasCall = seg.data.toolCalls.some((c) => c.toolExecutionRequestId === execId)
|
|
213
|
+
if (!hasCall) return seg
|
|
214
|
+
|
|
215
|
+
const prev: ApprovalBatchExecutionState | undefined = seg.data.executions?.[execId]
|
|
216
|
+
const next: ApprovalBatchExecutionState =
|
|
217
|
+
toolData.type === 'EXECUTED_TOOL'
|
|
218
|
+
? { status: 'done', result: toolData.result, success: toolData.success }
|
|
219
|
+
: { status: 'executing', result: prev?.result, success: prev?.success }
|
|
220
|
+
|
|
221
|
+
matched = true
|
|
222
|
+
return {
|
|
223
|
+
...seg,
|
|
224
|
+
data: {
|
|
225
|
+
...seg.data,
|
|
226
|
+
executions: { ...(seg.data.executions ?? {}), [execId]: next },
|
|
227
|
+
},
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
return matched
|
|
231
|
+
}
|
|
232
|
+
|
|
183
233
|
/**
|
|
184
234
|
* Track a pending approval request
|
|
185
235
|
*/
|
|
@@ -209,7 +259,67 @@ export class MessageSegmentAccumulator {
|
|
|
209
259
|
onApprove: this.callbacks.onApprove,
|
|
210
260
|
onReject: this.callbacks.onReject,
|
|
211
261
|
}
|
|
212
|
-
|
|
262
|
+
|
|
263
|
+
this.segments.push(segment)
|
|
264
|
+
return this.getSegments()
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Add a batch approval segment containing multiple tool calls. Upserts by
|
|
269
|
+
* `approvalRequestId`: when a batch with the same id is already in the
|
|
270
|
+
* accumulator, the existing segment is updated in place rather than a
|
|
271
|
+
* second segment being pushed. This matters for the consumer-store replay
|
|
272
|
+
* path, which feeds `[existing..., new...]` into `replaySegments` and would
|
|
273
|
+
* otherwise produce two batch segments for the same approval after a
|
|
274
|
+
* status flip or per-tool execution merge.
|
|
275
|
+
*
|
|
276
|
+
* `approvalType` is the highest-privilege type required across the batch.
|
|
277
|
+
* `executions` is forwarded as-is. On upsert, a new `executions` object
|
|
278
|
+
* overrides the existing one (so the latest replay wins).
|
|
279
|
+
*/
|
|
280
|
+
addApprovalBatch(
|
|
281
|
+
approvalRequestId: string,
|
|
282
|
+
approvalType: string,
|
|
283
|
+
toolCalls: PendingToolCallData[],
|
|
284
|
+
status: ChatApprovalStatus = 'pending',
|
|
285
|
+
executions?: Record<string, ApprovalBatchExecutionState>,
|
|
286
|
+
): MessageSegment[] {
|
|
287
|
+
const existingIndex = this.segments.findIndex(
|
|
288
|
+
(s): s is ApprovalBatchSegment =>
|
|
289
|
+
s.type === 'approval_batch' && s.data.approvalRequestId === approvalRequestId,
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
if (existingIndex !== -1) {
|
|
293
|
+
const existing = this.segments[existingIndex] as ApprovalBatchSegment
|
|
294
|
+
const mergedExecutions = executions ?? existing.data.executions
|
|
295
|
+
this.segments[existingIndex] = {
|
|
296
|
+
...existing,
|
|
297
|
+
data: {
|
|
298
|
+
approvalRequestId,
|
|
299
|
+
approvalType,
|
|
300
|
+
toolCalls,
|
|
301
|
+
...(mergedExecutions ? { executions: mergedExecutions } : {}),
|
|
302
|
+
},
|
|
303
|
+
status,
|
|
304
|
+
onApprove: this.callbacks.onApprove,
|
|
305
|
+
onReject: this.callbacks.onReject,
|
|
306
|
+
}
|
|
307
|
+
return this.getSegments()
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const segment: ApprovalBatchSegment = {
|
|
311
|
+
type: 'approval_batch',
|
|
312
|
+
data: {
|
|
313
|
+
approvalRequestId,
|
|
314
|
+
approvalType,
|
|
315
|
+
toolCalls,
|
|
316
|
+
...(executions ? { executions } : {}),
|
|
317
|
+
},
|
|
318
|
+
status,
|
|
319
|
+
onApprove: this.callbacks.onApprove,
|
|
320
|
+
onReject: this.callbacks.onReject,
|
|
321
|
+
}
|
|
322
|
+
|
|
213
323
|
this.segments.push(segment)
|
|
214
324
|
return this.getSegments()
|
|
215
325
|
}
|
|
@@ -249,13 +359,16 @@ export class MessageSegmentAccumulator {
|
|
|
249
359
|
}
|
|
250
360
|
|
|
251
361
|
/**
|
|
252
|
-
* Update status of an existing approval segment
|
|
362
|
+
* Update status of an existing approval segment (single or batch)
|
|
253
363
|
*/
|
|
254
364
|
updateApprovalStatus(requestId: string, status: ChatApprovalStatus): MessageSegment[] {
|
|
255
365
|
this.segments = this.segments.map(segment => {
|
|
256
366
|
if (segment.type === 'approval_request' && segment.data.requestId === requestId) {
|
|
257
367
|
return { ...segment, status }
|
|
258
368
|
}
|
|
369
|
+
if (segment.type === 'approval_batch' && segment.data.approvalRequestId === requestId) {
|
|
370
|
+
return { ...segment, status }
|
|
371
|
+
}
|
|
259
372
|
return segment
|
|
260
373
|
})
|
|
261
374
|
return this.getSegments()
|
|
@@ -366,6 +479,17 @@ export class MessageSegmentAccumulator {
|
|
|
366
479
|
)
|
|
367
480
|
break
|
|
368
481
|
}
|
|
482
|
+
case 'approval_batch': {
|
|
483
|
+
const { data, status } = segment
|
|
484
|
+
this.addApprovalBatch(
|
|
485
|
+
data.approvalRequestId,
|
|
486
|
+
data.approvalType,
|
|
487
|
+
data.toolCalls,
|
|
488
|
+
status,
|
|
489
|
+
data.executions,
|
|
490
|
+
)
|
|
491
|
+
break
|
|
492
|
+
}
|
|
369
493
|
case 'error':
|
|
370
494
|
this.addError(segment.title, segment.details)
|
|
371
495
|
break
|
|
@@ -7,13 +7,16 @@ import {
|
|
|
7
7
|
MESSAGE_TYPE,
|
|
8
8
|
OWNER_TYPE,
|
|
9
9
|
type AuthorType,
|
|
10
|
+
type ChatApprovalStatus,
|
|
10
11
|
type HistoricalMessage,
|
|
12
|
+
type PendingToolCallData,
|
|
11
13
|
type ProcessedMessage,
|
|
12
14
|
type MessageProcessingOptions,
|
|
13
15
|
type MessageData,
|
|
14
16
|
type MessageOwner,
|
|
15
17
|
} from '../types'
|
|
16
18
|
import { MessageSegmentAccumulator, createMessageSegmentAccumulator } from './message-segment-accumulator'
|
|
19
|
+
import { getCommandText } from './tool-call-helpers'
|
|
17
20
|
|
|
18
21
|
function getOwnerDisplayName(owner?: MessageOwner): string {
|
|
19
22
|
if (owner?.type === OWNER_TYPE.ADMIN && owner.user) {
|
|
@@ -48,7 +51,10 @@ function pushStandaloneMessages(
|
|
|
48
51
|
*/
|
|
49
52
|
export interface ProcessHistoricalMessagesResult {
|
|
50
53
|
messages: ProcessedMessage[]
|
|
51
|
-
escalatedApprovals: Map<
|
|
54
|
+
escalatedApprovals: Map<
|
|
55
|
+
string,
|
|
56
|
+
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
57
|
+
>
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
/**
|
|
@@ -71,7 +77,10 @@ export function processHistoricalMessages(
|
|
|
71
77
|
|
|
72
78
|
const processedMessages: ProcessedMessage[] = []
|
|
73
79
|
const accumulator = createMessageSegmentAccumulator({ onApprove, onReject })
|
|
74
|
-
const escalatedApprovals = new Map<
|
|
80
|
+
const escalatedApprovals = new Map<
|
|
81
|
+
string,
|
|
82
|
+
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
83
|
+
>()
|
|
75
84
|
|
|
76
85
|
let currentAssistantId: string | null = null
|
|
77
86
|
let currentAssistantTimestamp: Date | null = null
|
|
@@ -192,9 +201,15 @@ function processMessageData(
|
|
|
192
201
|
accumulator: MessageSegmentAccumulator,
|
|
193
202
|
approvalStatuses: Record<string, string>,
|
|
194
203
|
options: MessageProcessingOptions = {},
|
|
195
|
-
escalatedApprovals?: Map<
|
|
204
|
+
escalatedApprovals?: Map<
|
|
205
|
+
string,
|
|
206
|
+
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
207
|
+
>
|
|
196
208
|
): void {
|
|
197
|
-
|
|
209
|
+
// batchApprovalsEnabled is owned by the consumer (oss-tenant chat client /
|
|
210
|
+
// openframe-frontend tickets). Defaults to ON so consumers that haven't
|
|
211
|
+
// wired the flag yet get the batch UI; pass `false` to force legacy.
|
|
212
|
+
const { displayApprovalTypes, batchApprovalsEnabled = true } = options
|
|
198
213
|
switch (data.type) {
|
|
199
214
|
case MESSAGE_TYPE.TEXT:
|
|
200
215
|
if ('text' in data && data.text) {
|
|
@@ -217,6 +232,7 @@ function processMessageData(
|
|
|
217
232
|
integratedToolType: data.integratedToolType || '',
|
|
218
233
|
toolFunction: data.toolFunction || '',
|
|
219
234
|
parameters: data.parameters,
|
|
235
|
+
toolExecutionRequestId: data.toolExecutionRequestId,
|
|
220
236
|
},
|
|
221
237
|
})
|
|
222
238
|
}
|
|
@@ -233,6 +249,7 @@ function processMessageData(
|
|
|
233
249
|
parameters: data.parameters,
|
|
234
250
|
result: data.result,
|
|
235
251
|
success: data.success,
|
|
252
|
+
toolExecutionRequestId: data.toolExecutionRequestId,
|
|
236
253
|
},
|
|
237
254
|
})
|
|
238
255
|
}
|
|
@@ -241,18 +258,42 @@ function processMessageData(
|
|
|
241
258
|
case MESSAGE_TYPE.APPROVAL_REQUEST:
|
|
242
259
|
if ('approvalRequestId' in data && data.approvalRequestId) {
|
|
243
260
|
const approvalType = data.approvalType || 'CLIENT'
|
|
261
|
+
const toolCalls: PendingToolCallData[] | undefined = Array.isArray(data.toolCalls)
|
|
262
|
+
? data.toolCalls
|
|
263
|
+
: undefined
|
|
264
|
+
const isBatch = !!toolCalls && toolCalls.length > 0
|
|
244
265
|
|
|
245
266
|
if (!displayApprovalTypes || displayApprovalTypes.includes(approvalType)) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
267
|
+
if (isBatch) {
|
|
268
|
+
const status = (approvalStatuses[data.approvalRequestId] as ChatApprovalStatus) || 'pending'
|
|
269
|
+
if (batchApprovalsEnabled) {
|
|
270
|
+
accumulator.addApprovalBatch(data.approvalRequestId, approvalType, toolCalls!, status)
|
|
271
|
+
} else {
|
|
272
|
+
// Flag OFF — unfold batch into N legacy approval cards (same id).
|
|
273
|
+
for (const call of toolCalls!) {
|
|
274
|
+
if (!call.requiresApproval) continue
|
|
275
|
+
accumulator.addApprovalRequest(
|
|
276
|
+
data.approvalRequestId,
|
|
277
|
+
getCommandText(call),
|
|
278
|
+
call.toolExplanation,
|
|
279
|
+
approvalType,
|
|
280
|
+
status,
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
} else {
|
|
285
|
+
accumulator.trackApprovalRequest(data.approvalRequestId, {
|
|
286
|
+
command: data.command || '',
|
|
287
|
+
explanation: data.explanation,
|
|
288
|
+
approvalType,
|
|
289
|
+
})
|
|
290
|
+
}
|
|
251
291
|
} else {
|
|
252
292
|
escalatedApprovals?.set(data.approvalRequestId, {
|
|
253
293
|
command: data.command || '',
|
|
254
294
|
explanation: data.explanation,
|
|
255
295
|
approvalType,
|
|
296
|
+
...(isBatch ? { toolCalls } : {}),
|
|
256
297
|
})
|
|
257
298
|
}
|
|
258
299
|
}
|
|
@@ -260,10 +301,34 @@ function processMessageData(
|
|
|
260
301
|
|
|
261
302
|
case MESSAGE_TYPE.APPROVAL_RESULT:
|
|
262
303
|
if ('approvalRequestId' in data && data.approvalRequestId) {
|
|
263
|
-
const existingStatus = approvalStatuses[data.approvalRequestId]
|
|
264
|
-
const status = existingStatus || (data.approved ? 'approved' : 'rejected')
|
|
304
|
+
const existingStatus = approvalStatuses[data.approvalRequestId] as ChatApprovalStatus | undefined
|
|
305
|
+
const status: ChatApprovalStatus = existingStatus || (data.approved ? 'approved' : 'rejected')
|
|
265
306
|
const escalatedData = escalatedApprovals?.get(data.approvalRequestId)
|
|
266
307
|
|
|
308
|
+
if (escalatedData?.toolCalls && escalatedData.toolCalls.length > 0) {
|
|
309
|
+
if (batchApprovalsEnabled) {
|
|
310
|
+
accumulator.addApprovalBatch(
|
|
311
|
+
data.approvalRequestId,
|
|
312
|
+
escalatedData.approvalType,
|
|
313
|
+
escalatedData.toolCalls,
|
|
314
|
+
status,
|
|
315
|
+
)
|
|
316
|
+
} else {
|
|
317
|
+
for (const call of escalatedData.toolCalls) {
|
|
318
|
+
if (!call.requiresApproval) continue
|
|
319
|
+
accumulator.addApprovalRequest(
|
|
320
|
+
data.approvalRequestId,
|
|
321
|
+
getCommandText(call),
|
|
322
|
+
call.toolExplanation,
|
|
323
|
+
escalatedData.approvalType,
|
|
324
|
+
status,
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
escalatedApprovals?.delete(data.approvalRequestId)
|
|
329
|
+
break
|
|
330
|
+
}
|
|
331
|
+
|
|
267
332
|
if (escalatedData) {
|
|
268
333
|
accumulator.trackApprovalRequest(data.approvalRequestId, {
|
|
269
334
|
command: escalatedData.command,
|
|
@@ -272,7 +337,14 @@ function processMessageData(
|
|
|
272
337
|
})
|
|
273
338
|
escalatedApprovals?.delete(data.approvalRequestId)
|
|
274
339
|
}
|
|
275
|
-
|
|
340
|
+
|
|
341
|
+
// If a segment with this id is already present (batch or legacy), just flip its status.
|
|
342
|
+
// updateApprovalStatus matches both `approval_batch` and `approval_request` segments.
|
|
343
|
+
const before = accumulator.getSegments()
|
|
344
|
+
const after = accumulator.updateApprovalStatus(data.approvalRequestId, status)
|
|
345
|
+
const updatedExisting = before.some((s, i) => after[i] !== s)
|
|
346
|
+
if (updatedExisting) break
|
|
347
|
+
|
|
276
348
|
accumulator.processApprovalResult(
|
|
277
349
|
data.approvalRequestId,
|
|
278
350
|
status === 'approved',
|
|
@@ -364,7 +436,10 @@ export function processHistoricalMessagesWithErrors(
|
|
|
364
436
|
|
|
365
437
|
const processedMessages: ProcessedMessage[] = []
|
|
366
438
|
const accumulator = createMessageSegmentAccumulator({ onApprove, onReject })
|
|
367
|
-
const escalatedApprovals = new Map<
|
|
439
|
+
const escalatedApprovals = new Map<
|
|
440
|
+
string,
|
|
441
|
+
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
442
|
+
>()
|
|
368
443
|
|
|
369
444
|
let currentAssistantId: string | null = null
|
|
370
445
|
let currentAssistantTimestamp: Date | null = null
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { PendingToolCallData } from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keys in `toolCallArguments` whose value is the actual command/query/script
|
|
5
|
+
* body — anything that should be rendered as the tool's "title" line in the
|
|
6
|
+
* approval card. Order matters: first match wins.
|
|
7
|
+
*/
|
|
8
|
+
export const COMMAND_BODY_ARG_KEYS = ['command', 'query', 'script', 'scriptContent', 'code'] as const
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Extract a human-readable command string from a tool call.
|
|
12
|
+
* Order: command/query/script/scriptContent/code arg → toolTitle → toolName → fallback.
|
|
13
|
+
*/
|
|
14
|
+
export function getCommandText(call: PendingToolCallData): string {
|
|
15
|
+
const args = call.toolCallArguments
|
|
16
|
+
if (args && typeof args === 'object') {
|
|
17
|
+
const a = args as Record<string, unknown>
|
|
18
|
+
for (const key of COMMAND_BODY_ARG_KEYS) {
|
|
19
|
+
const candidate = a[key]
|
|
20
|
+
if (typeof candidate === 'string' && candidate.trim()) return candidate
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return call.toolTitle || call.toolName || 'Tool call'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type FormattedArgValue =
|
|
27
|
+
| { kind: 'inline'; text: string }
|
|
28
|
+
| { kind: 'block'; text: string; language: 'json' | 'text' }
|
|
29
|
+
|
|
30
|
+
const INLINE_STRING_MAX = 80
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Decide how to render a single `toolCallArguments` value:
|
|
34
|
+
* - objects/arrays → pretty-printed JSON block
|
|
35
|
+
* - strings that parse as JSON → pretty-printed JSON block
|
|
36
|
+
* - multi-line or long strings → preserved-whitespace text block
|
|
37
|
+
* - everything else → inline single line
|
|
38
|
+
*/
|
|
39
|
+
export function formatToolArgValue(value: unknown): FormattedArgValue {
|
|
40
|
+
if (value === null || value === undefined) return { kind: 'inline', text: '' }
|
|
41
|
+
|
|
42
|
+
if (typeof value === 'object') {
|
|
43
|
+
return { kind: 'block', text: safeStringify(value), language: 'json' }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof value === 'string') {
|
|
47
|
+
const trimmed = value.trim()
|
|
48
|
+
if (
|
|
49
|
+
(trimmed.startsWith('{') && trimmed.endsWith('}')) ||
|
|
50
|
+
(trimmed.startsWith('[') && trimmed.endsWith(']'))
|
|
51
|
+
) {
|
|
52
|
+
try {
|
|
53
|
+
return { kind: 'block', text: JSON.stringify(JSON.parse(trimmed), null, 2), language: 'json' }
|
|
54
|
+
} catch {
|
|
55
|
+
// not valid JSON — fall through
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (value.includes('\n') || value.length > INLINE_STRING_MAX) {
|
|
59
|
+
return { kind: 'block', text: value, language: 'text' }
|
|
60
|
+
}
|
|
61
|
+
return { kind: 'inline', text: value }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return { kind: 'inline', text: String(value) }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function safeStringify(value: unknown): string {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(value, null, 2)
|
|
70
|
+
} catch {
|
|
71
|
+
return String(value)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Decide how to render a tool execution `result` string.
|
|
77
|
+
* - Strips a single surrounding markdown code fence (``` … ```), since the
|
|
78
|
+
* backend often wraps Bash/PowerShell output that way.
|
|
79
|
+
* - If the body parses as JSON, returns a pretty-printed JSON block.
|
|
80
|
+
* - If the body is multi-line / long, returns a preserved-whitespace block.
|
|
81
|
+
* - Otherwise inline.
|
|
82
|
+
*/
|
|
83
|
+
export function formatToolResult(value: string | undefined | null): FormattedArgValue {
|
|
84
|
+
if (typeof value !== 'string') return { kind: 'inline', text: '' }
|
|
85
|
+
|
|
86
|
+
const fenced = value.match(/^\s*```(?:[a-zA-Z0-9_-]*)\n([\s\S]*?)\n```\s*$/)
|
|
87
|
+
const body = fenced ? fenced[1] : value
|
|
88
|
+
const trimmed = body.trim()
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
(trimmed.startsWith('{') && trimmed.endsWith('}')) ||
|
|
92
|
+
(trimmed.startsWith('[') && trimmed.endsWith(']'))
|
|
93
|
+
) {
|
|
94
|
+
try {
|
|
95
|
+
return { kind: 'block', text: JSON.stringify(JSON.parse(trimmed), null, 2), language: 'json' }
|
|
96
|
+
} catch {
|
|
97
|
+
// fall through
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (body.includes('\n') || body.length > INLINE_STRING_MAX) {
|
|
102
|
+
return { kind: 'block', text: body, language: 'text' }
|
|
103
|
+
}
|
|
104
|
+
return { kind: 'inline', text: body }
|
|
105
|
+
}
|