|
1 | 1 | import { createLogger } from '@sim/logger' |
2 | 2 | import { getErrorMessage } from '@sim/utils/errors' |
3 | 3 | import { generateId } from '@sim/utils/id' |
| 4 | +import { calculateCostSummary } from '@/lib/logs/execution/logging-factory' |
| 5 | +import type { TraceSpan } from '@/lib/logs/types' |
4 | 6 | import { ChildWorkflowError } from '@/executor/errors/child-workflow-error' |
5 | 7 | import { |
6 | 8 | buildCustomBlockExecutionContext, |
7 | 9 | type CustomBlockExecutorContext, |
8 | 10 | } from '@/executor/handlers/workflow/custom-block-tool-runner' |
9 | | -import { |
10 | | - aggregateChildCost, |
11 | | - WorkflowBlockHandler, |
12 | | -} from '@/executor/handlers/workflow/workflow-handler' |
| 11 | +import { WorkflowBlockHandler } from '@/executor/handlers/workflow/workflow-handler' |
13 | 12 | import { classifyExecutionError } from '@/executor/utils/errors' |
14 | 13 | import { parseJSON } from '@/executor/utils/json' |
15 | 14 | import type { SerializedBlock } from '@/serializer/types' |
16 | 15 | import type { ToolResponse } from '@/tools/types' |
17 | 16 |
|
18 | 17 | const logger = createLogger('WorkflowToolRunner') |
19 | 18 |
|
| 19 | +/** |
| 20 | + * Hosted-key spend of a failed child run, the way the parent bills it: recurse |
| 21 | + * nested spans and de-dupe model breakdowns, then subtract the base execution |
| 22 | + * charge the parent already applies once itself. A naive top-level `cost.total` |
| 23 | + * sum undercounts when spend sits on nested children. |
| 24 | + */ |
| 25 | +function aggregateChildCost(childTraceSpans: TraceSpan[]): number { |
| 26 | + if (childTraceSpans.length === 0) return 0 |
| 27 | + const summary = calculateCostSummary(childTraceSpans) |
| 28 | + return Math.max(0, summary.totalCost - summary.baseExecutionCharge) |
| 29 | +} |
| 30 | + |
20 | 31 | interface WorkflowToolParams { |
21 | 32 | workflowId?: string |
22 | 33 | inputMapping?: Record<string, unknown> | string |
|
0 commit comments