feat(workflows): integrate handleSubagentChatCredits into runAgentWorkflow

AH
5mo ago
+485-0
  • Added the handleSubagentChatCredits function to manage credit handling for subagents.
  • Updated runAgentWorkflow to call handleSubagentChatCredits with appropriate parameters, including accountId, responseMessage, and previousResponseMessage.
  • Enhanced tests for runAgentWorkflow to verify the correct invocation of handleSubagentChatCredits under various scenarios, including when resuming a turn with an in-progress assistant message.
  • Updated handleChatCredits to support agentType and toolCallCount for better credit tracking.

Summary by cubic

Adds subagent credit attribution to the agent workflow by billing per task tool invocation and aggregating usage by subagent model. Prevents double-billing when resuming a turn and records agentType and toolCallCount for better spend analytics.

  • New Features

    • runAgentWorkflow now calls handleSubagentChatCredits with accountId, responseMessage, optional previousResponseMessage (for resume), fallback modelId, and source.
    • Per-invocation tracking: extracts task tool usage, diffs against a baseline on resume, aggregates by model, and counts tool calls.
    • Extended handleChatCredits to accept agentType: "subagent" and toolCallCount; added tests for aggregation, event collection/diffing, subagent billing, and workflow resume paths.
  • Refactors

    • Simplified imports in collectTaskToolUsageEvents.ts (no functional changes).

Summary by CodeRabbit

  • New Features
    • Subagent tool usage is now tracked and recorded for accurate credit deduction, ensuring billing reflects all agent activity within workflows.