Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

5.1 KB
Raw Blame
import { isNil, isObject, spreadIfDefined } from '@activepieces/core-utils'
import { AgentConversation, AgentTurnModel, chatBilling, ChatToolCall, isAppSumoCreditedPlan, PersistedAgentMessage, PersistedAgentPartType, PersistedAgentRole, PersistedToolCallStatus } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { LicenseKeyPostHogEvents } from '../../helper/telemetry.utils'
import { trackBillingAndSendTelemetry } from '../../platform/billing-and-telemetry'
import { CreditUsageSource } from '../../platform/billing-provider'
import { platformPlanService } from '../platform/platform-plan/platform-plan.service'
import { agentHelpers } from './agent-helpers'
import { agentModelTier } from './agent-model-tier'
import { agentHistory } from './history/agent-history'

function latestTurnToolCalls({ messages }: { messages: PersistedAgentMessage[] }): ChatToolCall[] {
    const lastUserIndex = messages.map((message) => message.role).lastIndexOf(PersistedAgentRole.USER)
    const turn = lastUserIndex === -1 ? messages : messages.slice(lastUserIndex + 1)
    return turn.flatMap((message) => message.parts.flatMap((part) =>
        part.type === PersistedAgentPartType.TOOL_CALL && part.status === PersistedToolCallStatus.COMPLETED
            ? [{ toolName: part.toolName, output: part.output }, ...(SUB_AGENT_TOOLS.includes(part.toolName) ? taskToolCalls(part.output) : [])]
            : [],
    ))
}

function taskToolCalls(output: unknown): ChatToolCall[] {
    if (!isObject(output) || !Array.isArray(output['billedToolCalls'])) {
        return []
    }
    return output['billedToolCalls'].flatMap((call) => isObject(call) && typeof call['toolName'] === 'string' ? [{ toolName: call['toolName'], output: call['output'] }] : [])
}

function countBillableToolCallsInLatestTurn({ messages }: { messages: PersistedAgentMessage[] }): number {
    return latestTurnToolCalls({ messages }).filter(chatBilling.isFlatBilledToolCall).length
}

async function chargeForLatestTurn({ conversation, runId, answeredBy, log }: ChargeForLatestTurnParams): Promise<void> {
    const messages = agentHistory.resolveMessages({ conversation, log })
    const turnIndex = messages.filter((message) => message.role === PersistedAgentRole.USER).length
    const idempotencyScope = runId ?? turnIndex
    const tierModel = answeredBy ?? (isNil(conversation.modelTierId) ? null : await agentModelTier(log).mainModelOf({ conversation }))
    const provider = tierModel?.provider ?? await agentHelpers.resolveConversationProviderName({
        platformId: conversation.platformId,
        projectId: conversation.projectId ?? null,
        providerConfigId: conversation.providerConfigId ?? null,
        log,
    })
    const surface = agentHelpers.surfaceOf({ source: conversation.source })
    const model = tierModel?.modelId ?? agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider, surface, pinned: !isNil(conversation.providerConfigId) })
    const tier = agentHelpers.resolveTier({ tierId: conversation.modelName ?? null, surface })
    const platformPlan = await platformPlanService(log).getOrCreateForPlatform(conversation.platformId)

    const { messageCredits, billedToolCalls, total } = chatBilling.creditsForTurn({ provider, toolCalls: latestTurnToolCalls({ messages }) })
    const charge = total === 0 ? undefined : {
        platformId: conversation.platformId,
        value: total,
        source: CreditUsageSource.CHAT,
        idempotencyKey: `${conversation.id}:chatTurn:${idempotencyScope}`,
        properties: {
            platformId: conversation.platformId,
            projectId: conversation.projectId ?? PROJECTLESS_CHAT,
            userId: conversation.userId,
            conversationId: conversation.id,
            turnIndex,
            messages: messageCredits,
            toolCalls: billedToolCalls,
            provider,
            model,
            tier: tier.id,
        },
    }
    const appSumoCharge = isNil(charge) || !isAppSumoCreditedPlan(platformPlan.plan) ? undefined : {
        ...charge,
        idempotencyKey: `${conversation.id}:appSumoChatTurn:${idempotencyScope}`,
        properties: {
            platformId: conversation.platformId,
            projectId: conversation.projectId ?? PROJECTLESS_CHAT,
            conversationId: conversation.id,
            turnIndex,
            tier: tier.id,
        },
    }

    await trackBillingAndSendTelemetry({
        log,
        licenseKey: platformPlan.licenseKey,
        ...spreadIfDefined('credits', charge),
        ...spreadIfDefined('appSumo', appSumoCharge),
        telemetry: {
            event: LicenseKeyPostHogEvents.CHAT_MESSAGE,
            properties: {
                provider,
                model,
                toolsUsed: billedToolCalls,
            },
        },
    })
}

export const chatToolBilling = {
    countBillableToolCallsInLatestTurn,
    chargeForLatestTurn,
}

const PROJECTLESS_CHAT = 'chat'

type ChargeForLatestTurnParams = {
    conversation: AgentConversation
    runId?: string
    answeredBy?: AgentTurnModel
    log: FastifyBaseLogger
}

const SUB_AGENT_TOOLS = ['ap_run_task', 'ap_deep_research']