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

4.6 KB
Raw Blame
import { Agent, AgentConversation, AgentConversationStatus, AgentRunSource, Platform, Project, User } from '@activepieces/shared'
import { EntitySchema } from 'typeorm'
import { ApIdSchema, BaseColumnSchemaPart } from '../../database/database-common'

export type AgentConversationWithRelations = AgentConversation & {
    platform: Platform
    project: Project
    user: User
    agent: Agent
}

export const AgentConversationEntity = new EntitySchema<AgentConversationWithRelations>({
    name: 'agent_conversation',
    columns: {
        ...BaseColumnSchemaPart,
        platformId: {
            ...ApIdSchema,
            nullable: false,
        },
        projectId: {
            ...ApIdSchema,
            nullable: true,
        },
        userId: {
            ...ApIdSchema,
            nullable: false,
        },
        flowRunId: {
            ...ApIdSchema,
            nullable: true,
        },
        waitpointId: {
            ...ApIdSchema,
            nullable: true,
        },
        aiCredits: {
            type: Number,
            nullable: true,
        },
        agentId: {
            ...ApIdSchema,
            nullable: true,
        },
        source: {
            type: String,
            nullable: false,
            default: AgentRunSource.CHAT,
        },
        title: {
            type: String,
            nullable: true,
        },
        modelName: {
            type: String,
            nullable: true,
        },
        modelTierId: {
            ...ApIdSchema,
            nullable: true,
        },
        providerConfigId: {
            ...ApIdSchema,
            nullable: true,
        },
        status: {
            type: String,
            nullable: false,
            default: AgentConversationStatus.IDLE,
        },
        activeRunId: {
            type: String,
            nullable: true,
        },
        messages: {
            type: 'jsonb',
            nullable: false,
            default: '[]',
        },
        uiMessages: {
            type: 'jsonb',
            nullable: true,
        },
        summary: {
            type: 'text',
            nullable: true,
        },
        summarizedUpToIndex: {
            type: Number,
            nullable: true,
        },
    },
    indices: [
        {
            name: 'idx_agent_conversation_platform_user_created_id',
            columns: ['platformId', 'userId', 'created', 'id'],
        },
        {
            name: 'idx_agent_conversation_flow_step_created',
            columns: ['created', 'projectId'],
            where: `source = '${AgentRunSource.FLOW_STEP}'`,
        },
        {
            name: 'idx_agent_conversation_agent_runs_created_id',
            columns: ['projectId', 'agentId', 'created', 'id'],
            where: `source = '${AgentRunSource.FLOW_STEP}' AND "agentId" IS NOT NULL`,
        },
        {
            name: 'idx_agent_conversation_agent_user_created_id',
            columns: ['agentId', 'userId', 'created', 'id'],
            where: '"agentId" IS NOT NULL',
        },
        {
            name: 'idx_agent_conversation_streaming_updated',
            columns: ['updated'],
            where: `status = '${AgentConversationStatus.STREAMING}'`,
        },
        {
            name: 'idx_agent_conversation_platform_provider_config',
            columns: ['platformId', 'providerConfigId'],
            where: '"providerConfigId" IS NOT NULL',
        },
    ],
    relations: {
        platform: {
            type: 'many-to-one',
            target: 'platform',
            cascade: true,
            onDelete: 'CASCADE',
            joinColumn: {
                name: 'platformId',
                foreignKeyConstraintName: 'fk_agent_conversation_platform_id',
            },
        },
        project: {
            type: 'many-to-one',
            target: 'project',
            cascade: true,
            onDelete: 'SET NULL',
            joinColumn: {
                name: 'projectId',
                foreignKeyConstraintName: 'fk_agent_conversation_project_id',
            },
        },
        agent: {
            type: 'many-to-one',
            target: 'agent',
            onDelete: 'CASCADE',
            joinColumn: {
                name: 'agentId',
                foreignKeyConstraintName: 'fk_agent_conversation_agent_id',
            },
        },
        user: {
            type: 'many-to-one',
            target: 'user',
            cascade: true,
            onDelete: 'CASCADE',
            joinColumn: {
                name: 'userId',
                foreignKeyConstraintName: 'fk_agent_conversation_user_id',
            },
        },
    },
})