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

18.0 KB
Raw Blame
import { ActivepiecesError, apId, ErrorCode, isNil, sanitizeObjectForPostgresql, SeekPage, spreadIfDefined, unique } from '@activepieces/core-utils'
import { Agent, AgentConversation, AgentConversationStatus, AgentHistoryMessage, AgentRunFlowReference, AgentRunListItem, AgentRunSource, CreateAgentConversationRequest, PersistedAgentMessage, PersistedAgentRole, SetAgentMessageFeedbackRequest, UpdateAgentConversationRequest } from '@activepieces/shared'
import { ModelMessage } from 'ai'
import { FastifyBaseLogger } from 'fastify'
import { EntityManager, In } from 'typeorm'
import { transaction } from '../../core/db/transaction'
import { databaseConnection } from '../../database/database-connection'
import { FlowRunEntity } from '../../flows/flow-run/flow-run-entity'
import { FlowVersionEntity } from '../../flows/flow-version/flow-version-entity'
import { buildPaginator } from '../../helper/pagination/build-paginator'
import { paginationHelper } from '../../helper/pagination/pagination-utils'
import { Order } from '../../helper/pagination/paginator'
import { agentApprovalGate } from './agent-approval-gate'
import { agentChatModel } from './agent-chat-model'
import { AgentConversationEntity } from './agent-conversation-entity'
import { AgentEntity } from './agent-entity'
import { agentHelpers, EVAL_CONVERSATION_ID_PREFIX, isEvalConversationId } from './agent-helpers'
import { agentModelTier } from './agent-model-tier'
import { agentService } from './agent-service'
import { agentHistory } from './history/agent-history'

async function flowReferencesFor(runs: AgentConversation[]): Promise<Map<string, AgentRunFlowReference>> {
    const flowRunIds = unique(runs.map((run) => run.flowRunId).filter((id): id is string => !isNil(id)))
    if (flowRunIds.length === 0) {
        return new Map()
    }
    const flowRuns = await databaseConnection().getRepository(FlowRunEntity).find({
        where: { id: In(flowRunIds) },
        select: ['id', 'flowId', 'flowVersionId'],
    })
    const flowVersionIds = unique(flowRuns.map((flowRun) => flowRun.flowVersionId))
    const flowVersions = await databaseConnection().getRepository(FlowVersionEntity).find({
        where: { id: In(flowVersionIds) },
        select: ['id', 'displayName'],
    })
    const displayNameByVersionId = new Map(flowVersions.map((version) => [version.id, version.displayName]))
    return new Map(flowRuns.map((flowRun) => [flowRun.id, {
        flowRunId: flowRun.id,
        flowId: flowRun.flowId,
        displayName: displayNameByVersionId.get(flowRun.flowVersionId) ?? '',
    }]))
}

async function projectStillHoldingAgent({ agentId, authorisedProjectId, entityManager }: { agentId: string, authorisedProjectId: string, entityManager: EntityManager }): Promise<string> {
    const locked = await entityManager.getRepository(AgentEntity)
        .createQueryBuilder('agent')
        .select(['agent.projectId'])
        .setLock('pessimistic_write')
        .where('agent.id = :agentId', { agentId })
        .getOne()
    if (isNil(locked) || locked.projectId !== authorisedProjectId) {
        throw new ActivepiecesError({
            code: ErrorCode.VALIDATION,
            params: { message: 'That agent has just moved to another project. Open it again to start a new chat.' },
        })
    }
    return locked.projectId
}

async function modelChoiceFrom({ platformId, projectId, request, log }: { platformId: string, projectId: string | null, request: ModelChoiceRequest, log: FastifyBaseLogger }): Promise<ModelChoice> {
    const { modelName, modelTierId, providerConfigId } = request
    if (!isNil(modelTierId)) {
        await agentModelTier(log).assertUsable({ platformId, tierId: modelTierId, scope: isNil(projectId) ? { type: 'platform' } : { type: 'project', projectId } })
        return { modelTierId, modelName: null, providerConfigId: null }
    }
    if (!isNil(providerConfigId)) {
        if (isNil(modelName)) {
            throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'A model picked on a key needs its model name' } })
        }
        if (isNil(projectId)) {
            throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'A chat needs a project before it can use a model on a key' } })
        }
        await agentChatModel(log).assertOffered({ platformId, projectId, providerConfigId, modelName })
        return { providerConfigId, modelName, modelTierId: null }
    }
    if (!isNil(modelName)) {
        return { modelName, modelTierId: null, providerConfigId: null }
    }
    return {
        ...(modelName === null ? { modelName: null, providerConfigId: null } : {}),
        ...(modelTierId === null ? { modelTierId: null } : {}),
        ...(providerConfigId === null ? { providerConfigId: null } : {}),
    }
}

export const agentConversationService = (log: FastifyBaseLogger) => ({
    async createConversation({ platformId, userId, request, id }: CreateConversationParams): Promise<AgentConversation> {
        const agent = isNil(request.agentId)
            ? null
            : await agentService(log).getOneOrThrowByPlatform({ id: request.agentId, platformId, userId })
        const builder = request.builder === true
        const builderProjectId = builder
            ? await resolveBuilderProject({ agent, requestedProjectId: request.projectId, platformId, userId, log })
            : null
        const chatProjectId = !builder && isNil(agent) && !isNil(request.projectId)
            ? await reachableProjectOrThrow({ projectId: request.projectId, platformId, userId, log })
            : null
        const modelChoice = await modelChoiceFrom({ platformId, projectId: agent?.projectId ?? builderProjectId ?? chatProjectId, request, log })
        const conversation = await transaction(async (entityManager) => entityManager.getRepository(AgentConversationEntity).save({
            id: id ?? apId(),
            platformId,
            projectId: isNil(agent)
                ? builderProjectId ?? chatProjectId
                : await projectStillHoldingAgent({ agentId: agent.id, authorisedProjectId: agent.projectId, entityManager }),
            userId,
            agentId: agent?.id ?? null,
            source: builder ? AgentRunSource.AGENT_BUILDER : isNil(agent) ? AgentRunSource.CHAT : AgentRunSource.AGENT,
            title: request.title ?? null,
            modelName: modelChoice.modelName ?? null,
            modelTierId: modelChoice.modelTierId ?? null,
            providerConfigId: modelChoice.providerConfigId ?? null,
            messages: [],
        }))
        log.info({ conversation: { id: conversation.id }, platform: { id: platformId }, user: { id: userId } }, '[agentConversationService] Conversation created')
        return conversation
    },

    async listConversations({ platformId, userId, cursor, limit, agentId }: ListConversationsParams): Promise<SeekPage<AgentRunListItem>> {
        const decodedCursor = paginationHelper.decodeCursor(cursor)
        const paginator = buildPaginator({
            entity: AgentConversationEntity,
            query: {
                limit,
                orderBy: [
                    { field: 'created', order: Order.DESC },
                    { field: 'id', order: Order.DESC },
                ],
                afterCursor: decodedCursor.nextCursor,
                beforeCursor: decodedCursor.previousCursor,
            },
        })

        const queryBuilder = agentHelpers.conversationRepo()
            .createQueryBuilder('agent_conversation')
            .select([
                'agent_conversation.id',
                'agent_conversation.created',
                'agent_conversation.updated',
                'agent_conversation.platformId',
                'agent_conversation.projectId',
                'agent_conversation.userId',
                'agent_conversation.title',
                'agent_conversation.modelName',
                'agent_conversation.status',
            ])
            .where({ platformId, userId })
            // Eval conversations are owned by the platform owner; keep them out of the regular list.
            .andWhere('agent_conversation.id NOT LIKE :evalPrefix', { evalPrefix: `${EVAL_CONVERSATION_ID_PREFIX}%` })
        if (isNil(agentId)) {
            queryBuilder.andWhere('agent_conversation.source = :chatSource', { chatSource: AgentRunSource.CHAT })
        }
        else {
            queryBuilder
                .andWhere('agent_conversation.source = :agentSource', { agentSource: AgentRunSource.AGENT })
                .andWhere('agent_conversation."agentId" = :agentId', { agentId })
        }

        const { data, cursor: paginationCursor } = await paginator.paginate(queryBuilder)
        return paginationHelper.createPage(data, paginationCursor)
    },

    async listAgentRuns({ projectId, agentId, cursor, limit }: ListAgentRunsParams): Promise<SeekPage<AgentConversation>> {
        const decodedCursor = paginationHelper.decodeCursor(cursor)
        const paginator = buildPaginator({
            entity: AgentConversationEntity,
            query: {
                limit,
                orderBy: [
                    { field: 'created', order: Order.DESC },
                    { field: 'id', order: Order.DESC },
                ],
                afterCursor: decodedCursor.nextCursor,
                beforeCursor: decodedCursor.previousCursor,
            },
        })

        const queryBuilder = agentHelpers.conversationRepo()
            .createQueryBuilder('agent_conversation')
            .select([
                'agent_conversation.id',
                'agent_conversation.created',
                'agent_conversation.updated',
                'agent_conversation.platformId',
                'agent_conversation.projectId',
                'agent_conversation.agentId',
                'agent_conversation.flowRunId',
                'agent_conversation.aiCredits',
                'agent_conversation.title',
                'agent_conversation.modelName',
                'agent_conversation.status',
            ])
            .where('agent_conversation."projectId" = :projectId', { projectId })
            .andWhere('agent_conversation."agentId" = :agentId', { agentId })
            .andWhere('agent_conversation.source = :flowStepSource', { flowStepSource: AgentRunSource.FLOW_STEP })

        const { data, cursor: paginationCursor } = await paginator.paginate(queryBuilder)
        const flowByRunId = await flowReferencesFor(data)
        const withFlow = data.map((run) => ({
            ...run,
            flow: isNil(run.flowRunId) ? null : flowByRunId.get(run.flowRunId) ?? null,
        }))
        return paginationHelper.createPage(withFlow, paginationCursor)
    },

    async getAgentRunOrThrow({ id, projectId }: { id: string, projectId: string }): Promise<AgentRunListItem & { agentId: string }> {
        const run = await agentHelpers.conversationRepo().findOne({
            where: { id, projectId, source: AgentRunSource.FLOW_STEP },
            select: [
                'id', 'created', 'updated', 'platformId', 'projectId', 'userId',
                'agentId', 'flowRunId', 'aiCredits', 'source', 'title',
                'modelName', 'status', 'activeRunId', 'uiMessages', 'summary',
                'summarizedUpToIndex',
            ],
        })
        if (isNil(run) || isNil(run.agentId)) {
            throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityId: id, entityType: 'AgentConversation' } })
        }
        const flowByRunId = await flowReferencesFor([run])
        return {
            ...run,
            agentId: run.agentId,
            flow: isNil(run.flowRunId) ? null : flowByRunId.get(run.flowRunId) ?? null,
        }
    },

    async getConversationOrThrow({ id, platformId, userId }: ConversationIdentifier): Promise<AgentConversation> {
        // Eval conversations must never be opened or messaged through the regular (non-dry-run) chat
        // path — that would run real tools against a conversation meant to be side-effect-free.
        if (isEvalConversationId(id)) {
            throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityId: id, entityType: 'AgentConversation' } })
        }
        const conversation = await agentHelpers.getConversationOrThrow({ id, platformId, userId, log })
        if (![AgentRunSource.CHAT, AgentRunSource.AGENT, AgentRunSource.AGENT_BUILDER].includes(conversation.source)) {
            throw new ActivepiecesError({ code: ErrorCode.ENTITY_NOT_FOUND, params: { entityId: id, entityType: 'AgentConversation' } })
        }
        return conversation
    },

    async updateConversation({ id, platformId, userId, request }: UpdateConversationParams): Promise<AgentConversation> {
        const conversation = await this.getConversationOrThrow({ id, platformId, userId })
        const updates = {
            ...spreadIfDefined('title', request.title),
            ...await modelChoiceFrom({ platformId, projectId: conversation.projectId ?? null, request, log }),
        }

        if (Object.keys(updates).length > 0) {
            await agentHelpers.conversationRepo().update(conversation.id, updates)
        }
        return { ...conversation, ...updates }
    },

    async deleteConversation({ id, platformId, userId }: ConversationIdentifier): Promise<void> {
        const conversation = await this.getConversationOrThrow({ id, platformId, userId })
        if (conversation.status === AgentConversationStatus.STREAMING) {
            await agentApprovalGate.requestCancel({ conversationId: id })
            await agentHelpers.conversationRepo().update(id, {
                status: AgentConversationStatus.IDLE,
            })
        }
        await agentHelpers.conversationRepo().delete(conversation.id)
        log.info({ conversation: { id }, platform: { id: platformId }, user: { id: userId } }, '[agentConversationService] Conversation deleted')
    },

    async getMessages({ id, platformId, userId }: ConversationIdentifier): Promise<{ data: PersistedAgentMessage[] | AgentHistoryMessage[] }> {
        const conversation = await this.getConversationOrThrow({ id, platformId, userId })
        if (conversation.uiMessages) {
            return { data: conversation.uiMessages }
        }
        const messages = agentHistory.reconstruct(conversation.messages as ModelMessage[])
        return { data: messages }
    },

    async setMessageFeedback({ id, platformId, userId, messageIndex, request }: SetMessageFeedbackParams): Promise<void> {
        const conversation = await this.getConversationOrThrow({ id, platformId, userId })
        const target = conversation.uiMessages?.[messageIndex]
        if (isNil(target) || target.role !== PersistedAgentRole.ASSISTANT) {
            throw new ActivepiecesError({
                code: ErrorCode.ENTITY_NOT_FOUND,
                params: { entityType: 'AgentMessage', entityId: `${id}#${messageIndex}` },
            })
        }
        // Patch only this message's feedback via atomic jsonb_set, never a full-array rewrite — a
        // concurrent worker append during a STREAMING turn must not be clobbered by a stale snapshot.
        const repo = agentHelpers.conversationRepo()
        const table = repo.metadata.tableName
        const path = `{${messageIndex},feedback}`
        if (isNil(request.rating)) {
            await repo.query(`UPDATE "${table}" SET "uiMessages" = "uiMessages" #- $1::text[] WHERE id = $2`, [path, conversation.id])
        }
        else {
            const reasons = request.reasons?.length ? request.reasons : undefined
            const comment = request.comment?.trim() || undefined
            const feedback = { rating: request.rating, ...spreadIfDefined('reasons', reasons), ...spreadIfDefined('comment', comment) }
            const value = JSON.stringify(sanitizeObjectForPostgresql(feedback))
            await repo.query(`UPDATE "${table}" SET "uiMessages" = jsonb_set("uiMessages", $1::text[], $2::jsonb, true) WHERE id = $3`, [path, value, conversation.id])
        }
        log.info({ conversation: { id }, messageIndex, rating: request.rating }, '[agentConversationService] Message feedback recorded')
    },

})

type CreateConversationParams = {
    platformId: string
    userId: string
    request: CreateAgentConversationRequest
    id?: string
}

type ListAgentRunsParams = {
    projectId: string
    agentId: string
    cursor?: string
    limit: number
}

type ListConversationsParams = {
    platformId: string
    userId: string
    cursor?: string
    limit: number
    agentId?: string
}

async function resolveBuilderProject({ agent, requestedProjectId, platformId, userId, log }: {
    agent: Agent | null
    requestedProjectId?: string
    platformId: string
    userId: string
    log: FastifyBaseLogger
}): Promise<string> {
    if (!isNil(agent)) {
        return agent.projectId
    }
    if (isNil(requestedProjectId)) {
        throw new ActivepiecesError({
            code: ErrorCode.VALIDATION,
            params: { message: 'A builder conversation needs either an agentId to change or a projectId to build in' },
        })
    }
    return reachableProjectOrThrow({ projectId: requestedProjectId, platformId, userId, log })
}

async function reachableProjectOrThrow({ projectId, platformId, userId, log }: { projectId: string, platformId: string, userId: string, log: FastifyBaseLogger }): Promise<string> {
    const projects = await agentHelpers.getUserProjects({ platformId, userId, log })
    if (!projects.some((project) => project.id === projectId)) {
        throw new ActivepiecesError({
            code: ErrorCode.ENTITY_NOT_FOUND,
            params: { entityId: projectId, entityType: 'Project' },
        })
    }
    return projectId
}

type ConversationIdentifier = {
    id: string
    platformId: string
    userId: string
}

type UpdateConversationParams = ConversationIdentifier & {
    request: UpdateAgentConversationRequest
}

type SetMessageFeedbackParams = ConversationIdentifier & {
    messageIndex: number
    request: SetAgentMessageFeedbackRequest
}

type ModelChoice = {
    modelName?: string | null
    modelTierId?: string | null
    providerConfigId?: string | null
}

type ModelChoiceRequest = Pick<UpdateAgentConversationRequest, 'modelName' | 'modelTierId' | 'providerConfigId'>