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

3.9 KB
Raw Blame
import { AgentRunSource, apId, FileCompression, FileType } from '@activepieces/shared'
import { FastifyInstance } from 'fastify'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { agentRpcHandlers } from '../../../../src/app/ee/agent/agent-rpc-handlers'
import { knowledgeBaseService } from '../../../../src/app/knowledge-base/knowledge-base.service'
import { db } from '../../../helpers/db'
import { createMockFile } from '../../../helpers/mocks'
import { createTestContext, TestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'

let app: FastifyInstance

beforeAll(async () => {
    app = await setupTestEnvironment()
})

afterAll(async () => {
    await teardownTestEnvironment()
})

async function conversationWithFile(ctx: TestContext): Promise<{ conversationId: string, knowledgeBaseFileId: string }> {
    const conversationId = apId()
    await db.save('agent_conversation', {
        id: conversationId,
        created: new Date().toISOString(),
        updated: new Date().toISOString(),
        platformId: ctx.platform.id,
        projectId: ctx.project.id,
        userId: ctx.user.id,
        source: AgentRunSource.AGENT,
        status: 'STREAMING',
        messages: [],
        uiMessages: [],
    })
    const file = createMockFile({
        projectId: ctx.project.id,
        platformId: ctx.platform.id,
        data: Buffer.from('The office closes at six.'),
        type: FileType.KNOWLEDGE_BASE,
        compression: FileCompression.NONE,
        fileName: 'handbook.txt',
    })
    await db.save('file', file)
    const fileId = file.id
    const kbFile = await knowledgeBaseService(app.log).createFile({
        projectId: ctx.project.id,
        fileId,
        displayName: 'Employee Handbook',
    })
    return { conversationId, knowledgeBaseFileId: kbFile.id }
}

async function search({ conversationId, knowledgeBaseFileId }: { conversationId: string, knowledgeBaseFileId: string }) {
    return agentRpcHandlers(app.log).executeKnowledgeBaseTool({
        conversationId,
        toolName: 'knowledge-employee-handbook',
        knowledgeBaseFileId,
        query: 'when does the office close',
    })
}

describe('an agent asked about a file it cannot search says so', () => {
    // The old answer was "No relevant information found", which the agent passed on as the document
    // not mentioning the thing. It had not read a word of it.
    it('says the file is not indexed rather than reporting the answer is absent', async () => {
        const ctx = await createTestContext(app, { plan: { agentsEnabled: true, chatEnabled: true } })
        const target = await conversationWithFile(ctx)

        const { result } = await search(target)

        expect(String(result)).toContain('could not be indexed')
        expect(String(result)).toContain('Employee Handbook')
        expect(String(result)).not.toContain('No relevant information found')
    })

    it('points the person to uploading again, which says why the file cannot be indexed', async () => {
        const ctx = await createTestContext(app, { plan: { agentsEnabled: true, chatEnabled: true } })
        const target = await conversationWithFile(ctx)

        const { result } = await search(target)

        expect(String(result)).toContain('uploading the file again will say why')
    })

    it('does not read a file belonging to another project', async () => {
        const ctx = await createTestContext(app, { plan: { agentsEnabled: true, chatEnabled: true } })
        const other = await createTestContext(app, { plan: { agentsEnabled: true, chatEnabled: true } })
        const mine = await conversationWithFile(ctx)
        const theirs = await conversationWithFile(other)

        await expect(search({ conversationId: mine.conversationId, knowledgeBaseFileId: theirs.knowledgeBaseFileId })).rejects.toThrow()
    })
})