import type { ChatMessage } from '@genesislcap/foundation-ai';
import { sumUsage } from './sum-usage';

/**
 * Sum cost across a message list: each message's provider-reported LLM `cost`
 * plus any `externalCostUsd` a widget reported for its own external service
 * calls, recursing into each tool call's `subAgentTrace` so LLM calls made by
 * sub-agents (potentially on a different provider with different rates)
 * contribute to the total.
 *
 * Returns 0 when no messages carry a cost — providers that don't report usage
 * metadata (e.g. Chrome built-in) are skipped silently.
 *
 * @internal
 */
export function sumCosts(messages: readonly ChatMessage[]): number {
  return sumUsage(messages).costUsd;
}
