Skip to content

Commit 56eb6cc

Browse files
committed
fix
1 parent e4b1919 commit 56eb6cc

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

apps/sim/lib/logs/execution/trace-spans/span-factory.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createLogger } from '@sim/logger'
2+
import { isRecordLike } from '@sim/utils/object'
23
import type { ProviderTiming, TraceSpan } from '@/lib/logs/types'
34
import {
45
isConditionBlockType,
@@ -17,6 +18,12 @@ const logger = createLogger('SpanFactory')
1718
/** A BlockLog that has already passed the id/type validity check. */
1819
type ValidBlockLog = BlockLog & { blockType: string }
1920

21+
/** Converts arbitrary tool results to the object shape expected by trace spans. */
22+
function normalizeTraceOutput(value: unknown): Record<string, unknown> | undefined {
23+
if (value === undefined) return undefined
24+
return isRecordLike(value) ? value : { value }
25+
}
26+
2027
/**
2128
* Creates a TraceSpan from a BlockLog. Returns null for invalid logs.
2229
*
@@ -190,6 +197,7 @@ function buildChildrenFromTimeSegments(
190197
const currentIndex = toolCallIndices.get(normalizedName) ?? 0
191198
const match = callsForName[currentIndex]
192199
toolCallIndices.set(normalizedName, currentIndex + 1)
200+
const output = normalizeTraceOutput(match?.result ?? match?.output)
193201

194202
const toolChild: TraceSpan = {
195203
id: `${span.id}-segment-${index}`,
@@ -200,9 +208,7 @@ function buildChildrenFromTimeSegments(
200208
endTime: segmentEndTime,
201209
status: match?.error || segment.errorMessage ? 'error' : 'success',
202210
input: match?.arguments ?? match?.input,
203-
output: match?.error
204-
? { error: match.error, ...(match.result ?? match.output ?? {}) }
205-
: (match?.result ?? match?.output),
211+
output: match?.error ? { error: match.error, ...output } : output,
206212
}
207213
if (segment.toolCallId) toolChild.toolCallId = segment.toolCallId
208214
if (segment.errorType) toolChild.errorType = segment.errorType
@@ -269,6 +275,7 @@ function buildChildrenFromToolCalls(span: TraceSpan, log: ValidBlockLog): TraceS
269275
return toolCalls.map((tc, index) => {
270276
const startTime = tc.startTime ?? log.startedAt
271277
const endTime = tc.endTime ?? log.endedAt
278+
const output = normalizeTraceOutput(tc.result ?? tc.output)
272279
return {
273280
id: `${span.id}-tool-${index}`,
274281
name: stripCustomToolPrefix(tc.name ?? 'unnamed-tool'),
@@ -278,9 +285,7 @@ function buildChildrenFromToolCalls(span: TraceSpan, log: ValidBlockLog): TraceS
278285
endTime,
279286
status: tc.error ? 'error' : 'success',
280287
input: tc.arguments ?? tc.input,
281-
output: tc.error
282-
? { error: tc.error, ...(tc.result ?? tc.output ?? {}) }
283-
: (tc.result ?? tc.output),
288+
output: tc.error ? { error: tc.error, ...output } : output,
284289
}
285290
})
286291
}

apps/sim/lib/logs/execution/trace-spans/trace-spans.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,34 @@ describe('buildTraceSpans', () => {
196196
expect(secondToolCall.output).toEqual({ status: 200, data: 'response' })
197197
})
198198

199+
it.concurrent('normalizes scalar tool results only at the trace display boundary', () => {
200+
const mockExecutionResult: ExecutionResult = {
201+
success: true,
202+
output: { content: 'Final output' },
203+
logs: [
204+
{
205+
blockId: 'agent-scalar',
206+
blockName: 'Scalar Agent',
207+
blockType: 'agent',
208+
startedAt: '2024-01-01T10:00:00.000Z',
209+
endedAt: '2024-01-01T10:00:01.000Z',
210+
durationMs: 1000,
211+
success: true,
212+
output: {
213+
toolCalls: {
214+
list: [{ name: 'boolean_tool', result: false }],
215+
count: 1,
216+
},
217+
},
218+
},
219+
],
220+
}
221+
222+
const { traceSpans } = buildTraceSpans(mockExecutionResult)
223+
224+
expect(traceSpans[0].children?.[0].output).toEqual({ value: false })
225+
})
226+
199227
it.concurrent(
200228
'extracts tool calls from agent block output with direct toolCalls array format',
201229
() => {

0 commit comments

Comments
 (0)