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

feat(agents): a run says how long the agent actually worked (#15561)

Hazem Adelaed443bfd295
diff --git a/bun.lock b/bun.lock
index d74907c0a6b..591d4289b96 100644
--- a/bun.lock
+++ b/bun.lock
@@ -163,7 +163,7 @@
},
"packages/core/shared": {
"name": "@activepieces/shared",
- "version": "0.171.0",
+ "version": "0.172.0",
"dependencies": {
"@activepieces/core-execution": "workspace:*",
"@activepieces/core-formula": "workspace:*",
diff --git a/packages/core/execution/src/lib/workers/worker-contract.ts b/packages/core/execution/src/lib/workers/worker-contract.ts
index ded702340f1..435918b55ad 100644
--- a/packages/core/execution/src/lib/workers/worker-contract.ts
+++ b/packages/core/execution/src/lib/workers/worker-contract.ts
@@ -175,6 +175,7 @@ export type SaveAgentMessagesRequest = {
uiMessages: unknown[]
title?: string
modelName?: string
+ aiCredits?: number
}
export type SaveAgentFileRequest = {
diff --git a/packages/core/piece-types/src/lib/ai-providers.ts b/packages/core/piece-types/src/lib/ai-providers.ts
index 1a4323e88c0..44359a4b403 100644
--- a/packages/core/piece-types/src/lib/ai-providers.ts
+++ b/packages/core/piece-types/src/lib/ai-providers.ts
@@ -484,8 +484,20 @@ export const AI_PROVIDER_CAPABILITIES: Record<AIProviderName, AIProviderCapabili
[AIProviderName.MOONSHOT]: buildProviderCapabilities(AIProviderName.MOONSHOT),
}
+function resolveAiCreditWeight({ provider, model }: { provider: string, model: string }): number {
+ if (provider !== AIProviderName.ACTIVEPIECES) {
+ return 1
+ }
+ const tierWeight = ACTIVEPIECES_CHAT_TIERS.find((tier) => tier.modelId === model)?.creditWeight
+ if (tierWeight !== undefined) {
+ return tierWeight
+ }
+ return MANAGED_MODEL_WEIGHTS[model] ?? DEFAULT_MANAGED_MODEL_WEIGHT
+}
+
export const aiProviderUtils = {
getMaxContextTokens,
+ resolveAiCreditWeight,
getCuratedChatModels,
isCuratedChatModelId,
managedChatModelIds,
diff --git a/packages/core/shared/package.json b/packages/core/shared/package.json
index c5e7bee948c..6a26621c547 100644
--- a/packages/core/shared/package.json
+++ b/packages/core/shared/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/shared",
- "version": "0.171.0",
+ "version": "0.172.0",
"type": "commonjs",
"sideEffects": false,
"main": "./dist/src/index.js",
diff --git a/packages/core/shared/src/lib/ee/agent/index.ts b/packages/core/shared/src/lib/ee/agent/index.ts
index db2ee24c092..2cb873c3744 100644
--- a/packages/core/shared/src/lib/ee/agent/index.ts
+++ b/packages/core/shared/src/lib/ee/agent/index.ts
@@ -200,6 +200,7 @@ export const AgentConversation = z.object({
userId: z.string(),
agentId: Nullable(z.string()),
flowRunId: Nullable(z.string()),
+ aiCredits: Nullable(z.number().int()),
source: z.enum(AgentRunSource),
title: Nullable(z.string()),
modelName: Nullable(z.string()),
diff --git a/packages/server/api/src/app/database/migration/postgres/1850000000000-AddAgentConversationAiCredits.ts b/packages/server/api/src/app/database/migration/postgres/1850000000000-AddAgentConversationAiCredits.ts
new file mode 100644
index 00000000000..4cc7b14bdf2
--- /dev/null
+++ b/packages/server/api/src/app/database/migration/postgres/1850000000000-AddAgentConversationAiCredits.ts
@@ -0,0 +1,22 @@
+import { QueryRunner } from 'typeorm'
+import { Migration } from '../../migration'
+
+export class AddAgentConversationAiCredits1850000000000 implements Migration {
+ name = 'AddAgentConversationAiCredits1850000000000'
+ breaking = false
+ release = '0.91.0'
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`
+ ALTER TABLE "agent_conversation"
+ ADD COLUMN IF NOT EXISTS "aiCredits" integer
+ `)
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`
+ ALTER TABLE "agent_conversation"
+ DROP COLUMN IF EXISTS "aiCredits"
+ `)
+ }
+}
diff --git a/packages/server/api/src/app/database/postgres-connection.ts b/packages/server/api/src/app/database/postgres-connection.ts
index adb773bba82..a0e7e16f5b4 100644
--- a/packages/server/api/src/app/database/postgres-connection.ts
+++ b/packages/server/api/src/app/database/postgres-connection.ts
@@ -438,6 +438,7 @@ import { AddAgentRunsIndex1842000000000 } from './migration/postgres/18420000000
import { AddAgentConversationFlowRunId1843000000000 } from './migration/postgres/1843000000000-AddAgentConversationFlowRunId'
import { AddMcpActivity1844000000000 } from './migration/postgres/1844000000000-AddMcpActivity'
import { NullOrphanFolderReferences1846000000000 } from './migration/postgres/1846000000000-NullOrphanFolderReferences'
+import { AddAgentConversationAiCredits1850000000000 } from './migration/postgres/1850000000000-AddAgentConversationAiCredits'
const getSslConfig = (): boolean | TlsOptions => {
const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL)
@@ -891,6 +892,7 @@ export const getMigrations = (): (new () => Migration)[] => {
AddAgentConversationFlowRunId1843000000000,
AddMcpActivity1844000000000,
NullOrphanFolderReferences1846000000000,
+ AddAgentConversationAiCredits1850000000000,
]
return migrations
}
diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts b/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts
index ff59a22e524..2f40d1c919b 100644
--- a/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts
+++ b/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts
@@ -29,6 +29,10 @@ export const AgentConversationEntity = new EntitySchema<AgentConversationWithRel
...ApIdSchema,
nullable: true,
},
+ aiCredits: {
+ type: Number,
+ nullable: true,
+ },
agentId: {
...ApIdSchema,
nullable: true,
diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-service.ts b/packages/server/api/src/app/ee/agent/agent-conversation-service.ts
index 88e43430e40..1acca12ca1b 100644
--- a/packages/server/api/src/app/ee/agent/agent-conversation-service.ts
+++ b/packages/server/api/src/app/ee/agent/agent-conversation-service.ts
@@ -150,6 +150,7 @@ export const agentConversationService = (log: FastifyBaseLogger) => ({
'agent_conversation.projectId',
'agent_conversation.agentId',
'agent_conversation.flowRunId',
+ 'agent_conversation.aiCredits',
'agent_conversation.title',
'agent_conversation.modelName',
'agent_conversation.status',
diff --git a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
index 08995470b8b..818ec88844b 100644
--- a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
+++ b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
@@ -40,6 +40,7 @@ export const conversationRpc = (log: FastifyBaseLogger) => ({
const isSuccessfulCompletion = input.messages.length > 0
const updates: Record<string, unknown> = {
status: isSuccessfulCompletion ? AgentConversationStatus.IDLE : AgentConversationStatus.ERROR,
+ ...spreadIfDefined('aiCredits', input.aiCredits),
}
// No-shrink guard against silent context loss. The LLM history only ever grows within a
diff --git a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts b/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts
index 6b423927092..bfb9aef88a9 100644
--- a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts
+++ b/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts
@@ -1,5 +1,6 @@
-import { AIProviderName, isNil } from '@activepieces/core-utils'
-import { ACTIVEPIECES_CHAT_TIERS, DEFAULT_MANAGED_MODEL_WEIGHT, FileType, FlowRun, FlowVersion, isAppSumoCreditedPlan, LogSliceRef, MANAGED_MODEL_WEIGHTS } from '@activepieces/shared'
+import { aiProviderUtils } from '@activepieces/core-piece-types'
+import { isNil } from '@activepieces/core-utils'
+import { FileType, FlowRun, FlowVersion, isAppSumoCreditedPlan, LogSliceRef } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { platformPlanService } from '../../ee/platform/platform-plan/platform-plan.service'
import { fileService } from '../../file/file.service'
@@ -79,16 +80,7 @@ export const flowRunAiUsageTracker = (log: FastifyBaseLogger) => ({
},
})
-export function resolveAiCreditWeight({ provider, model }: { provider: string, model: string }): number {
- if (provider !== AIProviderName.ACTIVEPIECES) {
- return 1
- }
- const tierWeight = ACTIVEPIECES_CHAT_TIERS.find((tier) => tier.modelId === model)?.creditWeight
- if (!isNil(tierWeight)) {
- return tierWeight
- }
- return MANAGED_MODEL_WEIGHTS[model] ?? DEFAULT_MANAGED_MODEL_WEIGHT
-}
+export const resolveAiCreditWeight = aiProviderUtils.resolveAiCreditWeight
diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
index 5e53cdee05d..29b757954dc 100644
--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
@@ -1,6 +1,6 @@
import { AIProviderName, ErrorCode, formatPieceError, isNil, isObject, spreadIfDefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
import { agentAiUtils } from '@activepieces/server-utils'
-import { AgentEvent, AgentEventType, AgentKnowledgeBaseTool, AgentMcpTool, AgentOutputField, AgentPhase, AgentPieceTool, AgentResult, AgentRunSource, AgentTool, AgentToolType, EngineResponseStatus, ExecuteAgentRunJobData, MAX_AGENT_TURN_WALL_CLOCK_MS, PersistedAgentMessage, PersistedAgentPart, PersistedAgentRole, ResolvedAgentFlowTool, WorkerJobType } from '@activepieces/shared'
+import { AgentEvent, AgentEventType, AgentKnowledgeBaseTool, AgentMcpTool, AgentOutputField, AgentPhase, AgentPieceTool, AgentResult, AgentRunSource, AgentTool, AgentToolType, aiProviderUtils, EngineResponseStatus, ExecuteAgentRunJobData, MAX_AGENT_TURN_WALL_CLOCK_MS, PersistedAgentMessage, PersistedAgentPart, PersistedAgentPartType, PersistedAgentRole, ResolvedAgentFlowTool, WorkerJobType } from '@activepieces/shared'
import { createUIMessageStream, generateText, ModelMessage, streamText, ToolSet, toUIMessageStream } from 'ai'
import { FireAndForgetJobResult, JobContext, JobHandler, JobResultKind } from '../../../types'
import { agentMcpClient, McpConnection } from './agent-mcp-client'
@@ -336,6 +336,11 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge
}, 'Chat message completed')
const thinkingDurationMs = Date.now() - thinkingStartTime
+ const billedAsAiCredits = source === AgentRunSource.FLOW_STEP
+ const toolCallsThisTurn = uiParts.filter((part) => part.type === PersistedAgentPartType.TOOL_CALL).length
+ const aiCredits = billedAsAiCredits
+ ? aiProviderUtils.resolveAiCreditWeight({ provider: config.provider, model: config.modelId }) + toolCallsThisTurn
+ : undefined
const savePayload = {
conversationId,
runId,
@@ -346,6 +351,7 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge
],
...spreadIfDefined('title', autoTitle),
...spreadIfDefined('modelName', isNil(data.modelName) ? config.tier.id : undefined),
+ ...spreadIfDefined('aiCredits', aiCredits),
}
await retryWithBackoff({ fn: () => ctx.apiClient.saveAgentMessages(savePayload), description: 'Saving the transcript', throwOnExhausted: true, log })
diff --git a/packages/web/src/app/routes/agents/id/runs/index.tsx b/packages/web/src/app/routes/agents/id/runs/index.tsx
index d231d9bb00c..5cfadd2d078 100644
--- a/packages/web/src/app/routes/agents/id/runs/index.tsx
+++ b/packages/web/src/app/routes/agents/id/runs/index.tsx
@@ -2,7 +2,15 @@ import { isNil } from '@activepieces/core-utils';
import { AgentRunListItem } from '@activepieces/shared';
import { ColumnDef } from '@tanstack/react-table';
import { t } from 'i18next';
-import { Activity, Bot, Clock, History, Workflow } from 'lucide-react';
+import {
+ Activity,
+ Bot,
+ Clock,
+ History,
+ Coins,
+ Hourglass,
+ Workflow,
+} from 'lucide-react';
import { useCallback, useMemo } from 'react';
import { Link, useNavigate } from 'react-router-dom';
@@ -15,6 +23,7 @@ import { agentsQueries } from '@/features/agents/hooks/agents-hooks';
import { agentRunUtils } from '@/features/agents/lib/agent-run-utils';
import { projectCollectionUtils } from '@/features/projects';
import { authenticationSession } from '@/lib/authentication-session';
+import { formatUtils } from '@/lib/format-utils';
import { useNewWindow } from '@/lib/navigation-utils';
type AgentRunsProps = {
@@ -119,6 +128,43 @@ export const AgentRuns = ({ agentId }: AgentRunsProps) => {
);
},
},
+ {
+ accessorKey: 'updated',
+ size: 140,
+ header: ({ column }) => (
+ <DataTableColumnHeader
+ column={column}
+ title={t('Duration')}
+ icon={Hourglass}
+ />
+ ),
+ cell: ({ row }) => {
+ const durationMs = agentRunUtils.getDurationMs(row.original);
+ return (
+ <span className="text-left text-muted-foreground">
+ {isNil(durationMs)
+ ? '\u2014'
+ : formatUtils.formatDuration(durationMs, true)}
+ </span>
+ );
+ },
+ },
+ {
+ accessorKey: 'aiCredits',
+ size: 130,
+ header: ({ column }) => (
+ <DataTableColumnHeader
+ column={column}
+ title={t('Credits')}
+ icon={Coins}
+ />
+ ),
+ cell: ({ row }) => (
+ <span className="text-left text-muted-foreground">
+ {isNil(row.original.aiCredits) ? '\u2014' : row.original.aiCredits}
+ </span>
+ ),
+ },
{
accessorKey: 'created',
header: ({ column }) => (
diff --git a/packages/web/src/features/agents/lib/agent-run-utils.ts b/packages/web/src/features/agents/lib/agent-run-utils.ts
index 17773f59abf..2c78251ada6 100644
--- a/packages/web/src/features/agents/lib/agent-run-utils.ts
+++ b/packages/web/src/features/agents/lib/agent-run-utils.ts
@@ -1,4 +1,7 @@
-import { AgentConversationStatus } from '@activepieces/shared';
+import {
+ AgentConversationStatus,
+ AgentRunListItem,
+} from '@activepieces/shared';
import { t } from 'i18next';
import { CircleAlert, CircleCheck, LucideIcon, Play } from 'lucide-react';
@@ -24,9 +27,18 @@ function getStatusLabel(status: AgentConversationStatus): string {
}
}
+function getDurationMs(run: AgentRunListItem): number | undefined {
+ const stillRunning = run.status === AgentConversationStatus.STREAMING;
+ if (stillRunning) {
+ return undefined;
+ }
+ return new Date(run.updated).getTime() - new Date(run.created).getTime();
+}
+
export const agentRunUtils = {
getStatusIcon,
getStatusLabel,
+ getDurationMs,
};
export type AgentRunStatusIcon = {