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

23.9 KB
Raw Blame
import { ActivepiecesError, apId, connectionTemplate, ErrorCode, isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'
import { AgentConversation, AgentConversationStatus, AgentRunSource, AgentToolType, CreateAgentConversationRequest, ImportAgentMemoryRequest, InstructAgentMemoryRequest, LATEST_JOB_DATA_SCHEMA_VERSION, PrincipalType, SendAgentMessageRequest, SERVICE_KEY_SECURITY_OPENAPI, SetAgentMessageFeedbackRequest, UpdateAgentConversationRequest, UpdateAgentMemoryRequest, WorkerJobType } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { StatusCodes } from 'http-status-codes'
import { z } from 'zod'
import { securityAccess } from '../../core/security/authorization/fastify-security'
import { mcpUtils } from '../../mcp/tools/mcp-utils'
import { assertCreditsAndAppSumoNotExceeded } from '../../platform/billing-provider'
import { jobQueue, JobType } from '../../workers/job-queue/job-queue'
import { agentApprovalGate } from './agent-approval-gate'
import { agentChatModel } from './agent-chat-model'
import { agentConversationService } from './agent-conversation-service'
import { agentHelpers } from './agent-helpers'
import { agentMemoryAi } from './agent-memory-ai'
import { agentService } from './agent-service'
import { agentPrompt } from './prompt/agent-prompt'
import { updateConversationForRun } from './rpc/rpc-shared'
import { findConnectionsForPiece } from './tools/agent-tools'

const CHAT_PRINCIPALS = [PrincipalType.USER] as const

// Tools configured before 0.87 stored the pin as a template rather than the bare id.

export const agentConversationController: FastifyPluginAsyncZod = async (app) => {

    app.post('/conversations', CreateConversationRoute, async (request, reply) => {
        const conversation = await agentConversationService(request.log).createConversation({
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            request: request.body,
        })
        return reply.status(StatusCodes.CREATED).send(conversation)
    })

    app.get('/conversations', ListConversationsRoute, async (request) => {
        return agentConversationService(request.log).listConversations({
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            cursor: request.query.cursor,
            limit: request.query.limit ?? 20,
            ...spreadIfDefined('agentId', request.query.agentId),
        })
    })

    app.get('/conversations/:id', GetConversationRoute, async (request) => {
        return agentConversationService(request.log).getConversationOrThrow({
            id: request.params.id,
            platformId: request.principal.platform.id,
            userId: request.principal.id,
        })
    })

    app.post('/conversations/:id', UpdateConversationRoute, async (request) => {
        return agentConversationService(request.log).updateConversation({
            id: request.params.id,
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            request: request.body,
        })
    })

    app.delete('/conversations/:id', DeleteConversationRoute, async (request, reply) => {
        await agentConversationService(request.log).deleteConversation({
            id: request.params.id,
            platformId: request.principal.platform.id,
            userId: request.principal.id,
        })
        return reply.status(StatusCodes.NO_CONTENT).send()
    })

    app.get('/conversations/:id/messages', GetMessagesRoute, async (request) => {
        return agentConversationService(request.log).getMessages({
            id: request.params.id,
            platformId: request.principal.platform.id,
            userId: request.principal.id,
        })
    })

    app.post('/conversations/:id/messages/:messageIndex/feedback', SetMessageFeedbackRoute, async (request, reply) => {
        await agentConversationService(request.log).setMessageFeedback({
            id: request.params.id,
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            messageIndex: request.params.messageIndex,
            request: request.body,
        })
        return reply.status(StatusCodes.OK).send({ success: true })
    })

    app.post('/conversations/:id/messages', SendMessageRoute, async (request, reply) => {
        const { content, runId: clientRunId, files } = request.body
        const conversationId = request.params.id
        const userId = request.principal.id
        const platformId = request.principal.platform.id
        const log = request.log.child({ conversation: { id: conversationId }, user: { id: userId }, platform: { id: platformId } })

        log.info({ filesCount: files?.length ?? 0, contentLength: content.length }, '[agentConversationController] Chat message received')

        const conversation = await agentConversationService(log).getConversationOrThrow({
            id: conversationId,
            platformId,
            userId,
        })

        await assertAgentMessageRateLimitNotExceeded({ platformId, userId, log })

        const runId = typeof clientRunId === 'string' ? clientRunId : apId()
        const runLog = log.child({ run: { id: runId } })

        const agent = isNil(conversation.agentId)
            ? null
            : await agentService(log).getOneOrThrowByPlatform({ id: conversation.agentId, platformId, userId })
        const agentConfig = agent?.draft ?? null
        const isBuilder = conversation.source === AgentRunSource.AGENT_BUILDER
        // resolveRunProvider and the assertion below both fall through to the platform's chat
        // provider when no provider is named. An agent answers on its own model or it does not run.
        const runsOnAgentModel = !isNil(agentConfig) && !isBuilder
        const runTierId = runsOnAgentModel ? agentConfig.modelTierId ?? null : conversation.modelTierId ?? null
        if (runsOnAgentModel && isNil(runTierId) && (isNil(agentConfig.provider) || isNil(agentConfig.modelName))) {
            throw new ActivepiecesError({
                code: ErrorCode.VALIDATION,
                params: { message: 'Pick a model for this agent before talking to it' },
            })
        }

        // Resolve the project the run will adopt, so admission asks about the same credentials the
        // worker will resolve. A brand-new conversation carries no project until the first turn
        // persists one, and checking platform-wide there would admit a message no key can serve.
        const runProjectId = agentHelpers.selectRunProject({
            conversationProjectId: conversation.projectId ?? null,
            projects: await agentHelpers.getUserProjects({ platformId, userId, log }),
        })
        const runScope = agentHelpers.runScopeOrThrow({ projectId: runProjectId })
        if (runsOnAgentModel) {
            await agentHelpers.assertRunProviderConfigured({
                platformId,
                log,
                scope: runScope,
                modelTierId: runTierId,
                ...spreadIfDefined('provider', agentConfig.provider ?? undefined),
                ...spreadIfDefined('providerConfigId', agentConfig.providerConfigId ?? undefined),
            })
        }
        else {
            await agentChatModel(log).assertRunnable({ platformId, conversation, scope: runScope })
        }
        await assertCreditsAndAppSumoNotExceeded({ platformId, log })

        const carriesConfiguredTools = !isNil(agentConfig) && !isBuilder
        const flowTools = carriesConfiguredTools
            ? await agentHelpers.resolveFlowTools({ projectId: runScope.projectId, tools: agentConfig.tools, log: runLog })
            : []

        // Claim ownership atomically in the DB — the single source of truth that
        // saveAgentMessages/updateAgentProgress/heartbeat fence against. A late write from the
        // preempted run is rejected as soon as this UPDATE commits (its runId no longer matches),
        // with no Redis/DB split to race through. The prior owner is read from the same row.
        const { preemptedRunId, wasStreaming } = await agentHelpers.claimConversationForRun({ conversationId, runId })

        if (wasStreaming) {
            log.info({ ...spreadIfDefined('preemptedRunId', preemptedRunId ?? undefined) }, '[agentConversationController] Cancelling in-flight run before new message')
            const cancelPromises = [
                agentApprovalGate.requestCancel({ conversationId }),
            ]
            if (preemptedRunId) {
                cancelPromises.push(agentApprovalGate.requestCancel({ conversationId, runId: preemptedRunId }))
            }
            await Promise.all(cancelPromises)
            const stillOwned = await updateConversationForRun({
                conversationId,
                runId,
                updates: { status: AgentConversationStatus.IDLE },
            })
            if (stillOwned) {
                await agentApprovalGate.clearPendingGate({ conversationId })
            }
        }

        await jobQueue(runLog).add({
            id: apId(),
            type: JobType.ONE_TIME,
            data: {
                schemaVersion: LATEST_JOB_DATA_SCHEMA_VERSION,
                jobType: WorkerJobType.EXECUTE_AGENT_RUN,
                conversationId,
                runId,
                projectId: conversation.projectId ?? null,
                platformId,
                userId,
                userMessage: content,
                modelName: conversation.modelName ?? null,
                files,
                flowTools,
                ...spreadIfDefined('source', conversation.source === AgentRunSource.CHAT ? undefined : conversation.source),
                ...spreadIfDefined('messageSource', request.body.messageSource),
                ...(isBuilder ? { promptOverride: { system: agentPrompt.buildBuilderSystemPrompt({ agent }) } } : {}),
                ...(carriesConfiguredTools ? agentHelpers.jobFieldsFromConfig({ config: agentConfig }) : {}),
            },
        })
        runLog.info({ job: { type: WorkerJobType.EXECUTE_AGENT_RUN } }, '[agentConversationController] Enqueued chat agent job')

        return reply.status(StatusCodes.OK).send({ conversationId, runId })
    })

    app.post('/tool-approvals/:gateId', ToolApprovalRoute, async (request, reply) => {
        request.log.info({ gate: { id: request.params.gateId }, approved: request.body.approved }, '[agentConversationController] Tool approval received')
        const gateConversationId = await agentApprovalGate.conversationIdForGate({ gateId: request.params.gateId })
        if (!isNil(gateConversationId)) {
            await agentConversationService(request.log).getConversationOrThrow({
                id: gateConversationId,
                platformId: request.principal.platform.id,
                userId: request.principal.id,
            })
        }
        await agentApprovalGate.resolveGate({
            gateId: request.params.gateId,
            approved: request.body.approved,
            payload: request.body.payload,
            log: request.log,
        })
        return reply.status(StatusCodes.OK).send({ success: true })
    })

    app.post('/conversations/:id/cancel', CancelConversationRoute, async (request, reply) => {
        const conversationId = request.params.id
        const platformId = request.principal.platform.id
        const userId = request.principal.id
        const log = request.log.child({ conversation: { id: conversationId }, user: { id: userId }, platform: { id: platformId } })
        const conversation = await agentConversationService(log).getConversationOrThrow({ id: conversationId, platformId, userId })
        const activeRunId = conversation.activeRunId
        log.info({ ...spreadIfDefined('activeRunId', activeRunId ?? undefined) }, '[agentConversationController] Cancel requested')
        const cancelPromises = [
            agentApprovalGate.requestCancel({ conversationId }),
        ]
        if (activeRunId) {
            cancelPromises.push(agentApprovalGate.requestCancel({ conversationId, runId: activeRunId }))
        }
        await Promise.all(cancelPromises)
        await agentHelpers.conversationRepo().update(conversationId, {
            status: AgentConversationStatus.IDLE,
        })
        await agentApprovalGate.clearPendingGate({ conversationId })
        return reply.status(StatusCodes.OK).send({ success: true })
    })

    app.get('/conversations/:id/pending-gate', GetPendingGateRoute, async (request, reply) => {
        const conversationId = request.params.id
        const platformId = request.principal.platform.id
        const userId = request.principal.id
        const conversation = await agentConversationService(request.log).getConversationOrThrow({ id: conversationId, platformId, userId })
        const gates = await agentApprovalGate.getPendingGates({ conversationId })
        // A preempted run can leave (or race in) a pending gate; only surface one that belongs to
        // the run that currently owns the conversation. A turn can open several at once, so the
        // client is handed one at a time and asks again once it has been answered.
        const ownedByThisRun = gates.filter((gate) => isNil(gate.runId) || isNil(conversation.activeRunId) || gate.runId === conversation.activeRunId)
        return reply.status(StatusCodes.OK).send(ownedByThisRun[0] ?? null)
    })

    app.get('/conversations/:id/connections', GetPickerConnectionsRoute, async (request, reply) => {
        const conversationId = request.params.id
        const platformId = request.principal.platform.id
        const userId = request.principal.id
        const conversation = await agentConversationService(request.log).getConversationOrThrow({ id: conversationId, platformId, userId })
        const pieceName = request.query.pieceName
        const [pinned, allProjects] = await Promise.all([
            pinnedAccounts({ conversation, pieceName, platformId, userId, log: request.log }),
            agentHelpers.getUserProjects({ platformId, userId, log: request.log }),
        ])
        const projects = isNil(pinned) ? allProjects : allProjects.filter((project) => project.id === pinned.projectId)
        const { data: result } = await tryCatch(() => findConnectionsForPiece({ pieceName, projects, platformId, log: request.log }))
        if (isNil(result)) {
            const cached = await agentApprovalGate.getAvailableConnections({ conversationId, pieceName })
            return reply.status(StatusCodes.OK).send(connectionOffer({ connections: cached, pinned }))
        }
        if (!('pickConnection' in result)) {
            return reply.status(StatusCodes.OK).send(connectionOffer({ connections: [], pinned }))
        }
        await agentApprovalGate.storeAvailableConnections({ conversationId, pieceName, connections: result.connections })
        return reply.status(StatusCodes.OK).send(connectionOffer({ connections: result.connections, pinned }))
    })

    app.get('/memory', GetMemoryRoute, async (request) => {
        return agentHelpers.getUserMemory({
            platformId: request.principal.platform.id,
            userId: request.principal.id,
        })
    })

    app.post('/memory', UpdateMemoryRoute, async (request) => {
        return agentHelpers.saveUserMemory({
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            instructions: request.body.instructions,
            memories: request.body.memories,
        })
    })

    app.post('/memory/import', ImportMemoryRoute, async (request) => {
        const platformId = request.principal.platform.id
        const userId = request.principal.id
        const draft = await agentMemoryAi.extract({ platformId, userId, text: request.body.text, log: request.log })
        const current = await agentHelpers.getUserMemory({ platformId, userId })
        return agentHelpers.saveUserMemory({
            platformId,
            userId,
            memories: { next: [...current.memories, ...draft.memories], base: current.memories },
        })
    })

    app.post('/memory/instruct', InstructMemoryRoute, async (request) => {
        return agentMemoryAi.applyInstruction({
            platformId: request.principal.platform.id,
            userId: request.principal.id,
            instruction: request.body.instruction,
            log: request.log,
        })
    })

}

const CHAT_MESSAGES_PER_WINDOW = 40
const CHAT_MESSAGE_RATE_WINDOW_SECONDS = 10 * 60

// A saved agent's configured tools carry their pinned auth themselves and never consult the run's
// selection, so any other account offered here would report a switch that never happens.
async function pinnedAccounts({ conversation, pieceName, platformId, userId, log }: {
    conversation: AgentConversation
    pieceName: string
    platformId: string
    userId: string
    log: FastifyBaseLogger
}): Promise<PinnedAccounts | null> {
    const { agentId } = conversation
    if (conversation.source !== AgentRunSource.AGENT || isNil(agentId)) {
        return null
    }
    const agent = await agentService(log).getOneOrThrowByPlatform({ id: agentId, platformId, userId })
    const normalizedPiece = normalizePiece(pieceName)
    const config = agent.draft
    const externalIds = config.tools.flatMap((tool) => {
        if (tool.type !== AgentToolType.PIECE || normalizePiece(tool.pieceMetadata.pieceName) !== normalizedPiece) {
            return []
        }
        const auth = tool.pieceMetadata.predefinedInput?.auth
        const externalId = connectionTemplate.unwrapExternalId(auth)
        return isNil(externalId) ? [] : [externalId]
    })
    return { externalIds, projectId: agent.projectId }
}

// externalId is caller-supplied and its index is not unique, so two projects on one platform can
// carry the same one. The agent's own project has to match or a lookalike row slips through.
function connectionOffer<T extends { externalId: string, projectId: string }>({ connections, pinned }: {
    connections: T[]
    pinned: PinnedAccounts | null
}): { connections: T[], reconnectOnly: boolean } {
    if (isNil(pinned) || pinned.externalIds.length === 0) {
        return { connections, reconnectOnly: false }
    }
    return {
        connections: connections.filter((connection) => connection.projectId === pinned.projectId && pinned.externalIds.includes(connection.externalId)),
        reconnectOnly: true,
    }
}

function normalizePiece(pieceName: string): string {
    return mcpUtils.normalizePieceName(pieceName) ?? pieceName
}

// Per-user flood guard: nothing else bounds how fast a user fires messages, and each one enqueues a
// worker job and spends credits. Complements the credit balance, which bounds spend, not rate.
async function assertAgentMessageRateLimitNotExceeded({ platformId, userId, log }: { platformId: string, userId: string, log: FastifyBaseLogger }): Promise<void> {
    const { allowed, count } = await agentHelpers.incrementAndCheckLimit({
        key: `chat-message-rate:${platformId}:${userId}`,
        limit: CHAT_MESSAGES_PER_WINDOW,
        ttlSeconds: CHAT_MESSAGE_RATE_WINDOW_SECONDS,
    })
    if (!allowed) {
        log.warn({ user: { id: userId }, count }, '[agentConversationController] Chat message rate limit exceeded')
        throw new ActivepiecesError({
            code: ErrorCode.CHAT_MESSAGE_LIMIT_EXCEEDED,
            params: { limit: CHAT_MESSAGES_PER_WINDOW, windowSeconds: CHAT_MESSAGE_RATE_WINDOW_SECONDS },
        })
    }
}

const CreateConversationRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        body: CreateAgentConversationRequest,
    },
}

const ListConversationsRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        querystring: z.object({
            cursor: z.string().optional(),
            limit: z.coerce.number().int().min(1).max(100).default(20).optional(),
            agentId: z.string().optional(),
        }),
    },
}

const CONVERSATION_PARAMS = z.object({ id: z.string() })

const GetConversationRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
    },
}

const UpdateConversationRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
        body: UpdateAgentConversationRequest,
    },
}

const DeleteConversationRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
    },
}

const GetMessagesRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
    },
}

const SetMessageFeedbackRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: z.object({ id: z.string(), messageIndex: z.coerce.number().int().min(0) }),
        body: SetAgentMessageFeedbackRequest,
    },
}

const SendMessageRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
        body: SendAgentMessageRequest,
    },
}

const ToolApprovalRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: z.object({ gateId: z.string() }),
        body: z.object({ approved: z.boolean(), payload: z.record(z.string(), z.unknown()).optional() }),
    },
}

const GetPendingGateRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
    },
}

const GetPickerConnectionsRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
        querystring: z.object({ pieceName: z.string() }),
    },
}

const GetMemoryRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
    },
}

const UpdateMemoryRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        body: UpdateAgentMemoryRequest,
    },
}

const ImportMemoryRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        body: ImportAgentMemoryRequest,
    },
}

const InstructMemoryRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        body: InstructAgentMemoryRequest,
    },
}

const CancelConversationRoute = {
    config: {
        security: securityAccess.publicPlatform(CHAT_PRINCIPALS),
    },
    schema: {
        tags: ['agent'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: CONVERSATION_PARAMS,
    },
}

type PinnedAccounts = {
    externalIds: string[]
    projectId: string
}