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

fix(chat): cleared personalization stays cleared when old research finishes (#17168)

Hazem Adelcef2abd03ea7
diff --git a/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts b/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts
index 8d9c2a2ddff..72060d80b5e 100644
--- a/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts
+++ b/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts
@@ -65,6 +65,7 @@ export const chatPersonalizationService = (log: FastifyBaseLogger) => ({
companyText: null,
role: null,
status: ChatPersonalizationStatus.SKIPPED,
+ researchToken: null,
profile: null,
useCases: null,
}
diff --git a/packages/server/api/src/app/ee/agent/personalization/personalization-rows.ts b/packages/server/api/src/app/ee/agent/personalization/personalization-rows.ts
index 5590dfda71d..f3499504bc9 100644
--- a/packages/server/api/src/app/ee/agent/personalization/personalization-rows.ts
+++ b/packages/server/api/src/app/ee/agent/personalization/personalization-rows.ts
@@ -49,10 +49,13 @@ export async function upsertFoundingUserRow({ platformId, userId, researchToken,
"updated" = now()
WHERE "chat_personalization"."researchToken" = $7
OR ("chat_personalization"."status" <> ALL($8::varchar[])
- AND "chat_personalization"."useCases" IS NULL)
+ AND "chat_personalization"."useCases" IS NULL
+ AND NOT ("chat_personalization"."status" = $9
+ AND "chat_personalization"."domain" IS NULL
+ AND "chat_personalization"."companyText" IS NULL))
RETURNING "id"
`,
- [apId(), platformId, userId, ChatPersonalizationStatus.READY, profile, useCases, researchToken, IN_FLIGHT_STATUSES],
+ [apId(), platformId, userId, ChatPersonalizationStatus.READY, profile, useCases, researchToken, IN_FLIGHT_STATUSES, ChatPersonalizationStatus.SKIPPED],
)
if (seeded.length === 0) {
log.info({ platform: { id: platformId }, user: { id: userId }, researchToken }, '[chatPersonalization] Founding-user seed skipped, the run was superseded or that row has its own research')
diff --git a/packages/server/api/test/integration/ee/agent/chat-personalization-opt-out.test.ts b/packages/server/api/test/integration/ee/agent/chat-personalization-opt-out.test.ts
index 795e1f754c7..fd8f8c06ac2 100644
--- a/packages/server/api/test/integration/ee/agent/chat-personalization-opt-out.test.ts
+++ b/packages/server/api/test/integration/ee/agent/chat-personalization-opt-out.test.ts
@@ -1,6 +1,7 @@
import { apId } from '@activepieces/core-utils'
-import { ChatPersonalizationStatus } from '@activepieces/shared'
+import { ChatPersonalizationScope, ChatPersonalizationStatus } from '@activepieces/shared'
import { FastifyInstance } from 'fastify'
+import { IsNull } from 'typeorm'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { chatPersonalizationService } from '../../../../src/app/ee/agent/personalization/chat-personalization-service'
import { db } from '../../../helpers/db'
@@ -40,6 +41,56 @@ describe('clearing chat personalization', () => {
expect(view.useCases).toEqual(USE_CASES)
})
+
+ it('ignores a research run that finishes after the user cleared personalization', async () => {
+ const ctx = await createTestContext(app)
+ await saveResearchedCompany(ctx)
+ const researchToken = apId()
+ await db.save('chat_personalization', {
+ id: apId(),
+ platformId: ctx.platform.id,
+ userId: ctx.user.id,
+ domain: 'acme.com',
+ companyText: null,
+ role: null,
+ status: ChatPersonalizationStatus.RESEARCHING,
+ researchToken,
+ profile: null,
+ useCases: null,
+ })
+ await service().upsert({ platformId: ctx.platform.id, userId: ctx.user.id, personalize: false })
+
+ await service().saveResult({ platformId: ctx.platform.id, userId: ctx.user.id, scope: ChatPersonalizationScope.USER, researchToken, status: 'READY', profile: PROFILE, useCases: USE_CASES })
+
+ const userRow = await db.findOneByOrFail<{ status: string }>('chat_personalization', { platformId: ctx.platform.id, userId: ctx.user.id })
+ expect(userRow.status).toBe(ChatPersonalizationStatus.SKIPPED)
+ })
+
+ it('ignores a company research run that finishes after the user cleared personalization', async () => {
+ const ctx = await createTestContext(app)
+ const researchToken = apId()
+ await db.save('chat_personalization', {
+ id: apId(),
+ platformId: ctx.platform.id,
+ userId: null,
+ domain: 'acme.com',
+ companyText: null,
+ role: null,
+ status: ChatPersonalizationStatus.RESEARCHING,
+ researchToken,
+ profile: null,
+ useCases: null,
+ })
+ await service().upsert({ platformId: ctx.platform.id, userId: ctx.user.id, personalize: false })
+
+ await service().saveResult({ platformId: ctx.platform.id, userId: ctx.user.id, scope: ChatPersonalizationScope.COMPANY, researchToken, status: 'READY', profile: RESEARCHED_PROFILE, useCases: RESEARCHED_USE_CASES })
+
+ const userRow = await db.findOneByOrFail<{ status: string, useCases: unknown }>('chat_personalization', { platformId: ctx.platform.id, userId: ctx.user.id })
+ const companyRow = await db.findOneByOrFail<{ status: string }>('chat_personalization', { platformId: ctx.platform.id, userId: IsNull() })
+ expect(companyRow.status).toBe(ChatPersonalizationStatus.READY)
+ expect(userRow.status).toBe(ChatPersonalizationStatus.SKIPPED)
+ expect(userRow.useCases).toBeNull()
+ })
})
function service() {
@@ -56,9 +107,15 @@ async function saveResearchedCompany(ctx: TestContext): Promise<void> {
role: null,
status: ChatPersonalizationStatus.READY,
researchToken: null,
- profile: { companyName: 'Acme', displayName: 'Acme', website: 'acme.com', description: 'a logistics company', industry: 'Transport', userRole: null },
+ profile: PROFILE,
useCases: USE_CASES,
})
}
+const PROFILE = { companyName: 'Acme', displayName: 'Acme', website: 'acme.com', description: 'a logistics company', industry: 'Transport', userRole: null }
+
+const RESEARCHED_PROFILE = { companyName: 'Acme', displayName: 'Acme', website: 'acme.com', description: 'a logistics company', industry: 'Transport' }
+
+const RESEARCHED_USE_CASES = [{ id: 'late-shipments', title: 'Track shipments', prompt: 'Post late shipments to Slack', imageId: 'answer-customers' }]
+
const USE_CASES = [{ title: 'Track shipments', description: 'Post late shipments to Slack' }]