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): clearing personalization hides the company cards (#16859)

Hazem Adel36a88681f474
diff --git a/brain/knowledge/flows-execution/chat-personalization.md b/brain/knowledge/flows-execution/chat-personalization.md
index 1c483935c62..29b536bafda 100644
--- a/brain/knowledge/flows-execution/chat-personalization.md
+++ b/brain/knowledge/flows-execution/chat-personalization.md
@@ -9,6 +9,7 @@ The first-run onboarding inside chat. A user with no row is asked "Who am I team
**UNSET** the synthesized status meaning no row exists, so this person has never been asked. Never persisted.
**DISMISSED_LEGACY** written by the shipping backfill to every user who existed before the feature. Terminal, and deliberately distinct from SKIPPED.
**SKIPPED** the user saw the question and declined, or research was not allowed to run.
+**Opt-out** a user row that is SKIPPED with no domain and no company text, written by Clear. It hides the company row's cards and profile from that user, and turning personalization back on replaces it (`hasOptedOut`).
**Company row** the `userId IS NULL` row holding the company answer and its research; one per platform.
**User row** a per-person row; a teammate's own role-targeted result wins over the company row once READY.
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 9f20d13c820..8d9c2a2ddff 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
@@ -22,7 +22,7 @@ import { userService } from '../../../user/user-service'
import { agentHelpers } from '../agent-helpers'
import { normalizeRoleTitle, normalizeWebsite } from './personalization-normalize'
import { apolloApiKey, claimForResearch, enqueueResearchJob, guardsAllowResearch, PERSONALIZATION_PROVIDER_SCOPE, PREFILL_TTL_SECONDS, prefillKey, readPrefill, recoverIfStale, RESEARCH_STALENESS_TIMEOUT_MS, startPrefillLookup, validateResult } from './personalization-research'
-import { callerMayEditCompany, findRow, IN_FLIGHT_STATUSES, personalizationRepo, upsertFoundingUserRow, UpsertParams, writeCompanyRow, writeUserRow } from './personalization-rows'
+import { callerMayEditCompany, findRow, hasOptedOut, IN_FLIGHT_STATUSES, personalizationRepo, upsertFoundingUserRow, UpsertParams, writeCompanyRow, writeUserRow } from './personalization-rows'
import { emitProgress, PersonalizationIdentity, toScopeEnum, toView } from './personalization-view'
@@ -159,13 +159,14 @@ export const chatPersonalizationService = (log: FastifyBaseLogger) => ({
const fresh = Date.now() - new Date(userRow.updated).getTime() < RESEARCH_STALENESS_TIMEOUT_MS
const terminal = [ChatPersonalizationStatus.READY, ChatPersonalizationStatus.SKIPPED].includes(userRow.status)
const roleChanged = !isNil(role) && role !== userRow.role
- if (terminal || (fresh && !roleChanged)) {
+ const keepCurrentRow = !hasOptedOut(userRow) && (terminal || (fresh && !roleChanged))
+ if (keepCurrentRow) {
if (roleChanged) {
await personalizationRepo().update({ platformId, userId }, { role })
}
return this.getEffectiveView({ platformId, userId })
}
- await personalizationRepo().update({ platformId, userId }, { status: ChatPersonalizationStatus.PENDING, researchToken, ...spreadIfDefined('role', role) })
+ await personalizationRepo().update({ platformId, userId }, { status: ChatPersonalizationStatus.PENDING, researchToken, domain: companyRow.domain, companyText: companyRow.companyText, ...spreadIfDefined('role', role) })
}
else {
const { error } = await tryCatch(() => personalizationRepo().insert({
@@ -213,7 +214,8 @@ export const chatPersonalizationService = (log: FastifyBaseLogger) => ({
recoverIfStale({ row: foundCompanyRow, platformId, userId, scope: ChatPersonalizationScope.COMPANY, log }),
])
const personalStatus = userRow?.status ?? ChatPersonalizationStatus.UNSET
- if (userRow?.status === ChatPersonalizationStatus.READY) {
+ const userRowWins = !isNil(userRow) && (userRow.status === ChatPersonalizationStatus.READY || hasOptedOut(userRow))
+ if (userRowWins) {
return toView({ row: userRow, scope: ChatPersonalizationScope.USER, inputsRow: companyRow ?? userRow, role: userRow.role ?? null, personalStatus, prefill: null })
}
if (!isNil(companyRow)) {
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 bf10ed18dae..5590dfda71d 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
@@ -131,6 +131,10 @@ export async function writeUserRow({ platformId, userId, patch }: {
}
+export function hasOptedOut(row: ChatPersonalization): boolean {
+ return row.status === ChatPersonalizationStatus.SKIPPED && isNil(row.domain) && isNil(row.companyText)
+}
+
export type UpsertParams = {
platformId: string
userId: string
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
new file mode 100644
index 00000000000..795e1f754c7
--- /dev/null
+++ b/packages/server/api/test/integration/ee/agent/chat-personalization-opt-out.test.ts
@@ -0,0 +1,64 @@
+import { apId } from '@activepieces/core-utils'
+import { ChatPersonalizationStatus } from '@activepieces/shared'
+import { FastifyInstance } from 'fastify'
+import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+import { chatPersonalizationService } from '../../../../src/app/ee/agent/personalization/chat-personalization-service'
+import { db } from '../../../helpers/db'
+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()
+})
+
+describe('clearing chat personalization', () => {
+ it('hides the company cards and profile once the user clears them', async () => {
+ const ctx = await createTestContext(app)
+ await saveResearchedCompany(ctx)
+
+ await service().upsert({ platformId: ctx.platform.id, userId: ctx.user.id, personalize: false })
+ const view = await service().getEffectiveView({ platformId: ctx.platform.id, userId: ctx.user.id })
+
+ expect(view.status).toBe(ChatPersonalizationStatus.SKIPPED)
+ expect(view.useCases).toEqual([])
+ expect(view.profile).toBeNull()
+ })
+
+ it('brings the company cards back when the user turns personalization on again', async () => {
+ const ctx = await createTestContext(app)
+ await saveResearchedCompany(ctx)
+ await service().upsert({ platformId: ctx.platform.id, userId: ctx.user.id, personalize: false })
+
+ await service().upsert({ platformId: ctx.platform.id, userId: ctx.user.id, personalize: true })
+ const view = await service().getEffectiveView({ platformId: ctx.platform.id, userId: ctx.user.id })
+
+ expect(view.useCases).toEqual(USE_CASES)
+ })
+})
+
+function service() {
+ return chatPersonalizationService(app.log)
+}
+
+async function saveResearchedCompany(ctx: TestContext): Promise<void> {
+ await db.save('chat_personalization', {
+ id: apId(),
+ platformId: ctx.platform.id,
+ userId: null,
+ domain: 'acme.com',
+ companyText: null,
+ role: null,
+ status: ChatPersonalizationStatus.READY,
+ researchToken: null,
+ profile: { companyName: 'Acme', displayName: 'Acme', website: 'acme.com', description: 'a logistics company', industry: 'Transport', userRole: null },
+ useCases: USE_CASES,
+ })
+}
+
+const USE_CASES = [{ title: 'Track shipments', description: 'Post late shipments to Slack' }]