The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add default likelihood settings for PDL enrichment (#26195)
Raphaël Bosi175dd2a127f3
diff --git a/packages/twenty-apps/public/people-data-labs/README.md b/packages/twenty-apps/public/people-data-labs/README.mdindex ddf2d2b2880..cac81f2d8a0 100644--- a/packages/twenty-apps/public/people-data-labs/README.md+++ b/packages/twenty-apps/public/people-data-labs/README.md@@ -8,6 +8,14 @@- **Run it anywhere** — from the command menu, as a workflow step, or straight from AI chat- **Smart matching** — finds the right profile from a LinkedIn, email, or domain, and only writes confident matches+## Enrichment defaults++Configure `PDL_PERSON_MIN_LIKELIHOOD` and `PDL_COMPANY_MIN_LIKELIHOOD` in the app's server variables to set the default minimum likelihood for people and companies. Each accepts an integer from 1 to 10.++Command menu items use these defaults. Single-record and bulk workflow nodes use them when their minimum likelihood input is empty; an explicit workflow value takes precedence.++When a variable is unset, enrichment uses the existing thresholds: 2 for strong identifiers (PDL ID, LinkedIn, email, or company domain) and 6 for name-based matches.+## 💳 BillingPay only for matches, in Twenty credits — **not found and skipped records are free.**diff --git a/packages/twenty-apps/public/people-data-labs/src/application-config.ts b/packages/twenty-apps/public/people-data-labs/src/application-config.tsindex 427d043b483..ea5e33fecbf 100644--- a/packages/twenty-apps/public/people-data-labs/src/application-config.ts+++ b/packages/twenty-apps/public/people-data-labs/src/application-config.ts@@ -1,4 +1,4 @@-import { defineApplication } from 'twenty-sdk/define';+import { defineApplication, FieldType } from 'twenty-sdk/define';import { APPLICATION_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';@@ -16,5 +16,17 @@ export default defineApplication({isSecret: true,isRequired: true,},+ PDL_PERSON_MIN_LIKELIHOOD: {+ description:+ 'Default minimum likelihood (1-10) for people enrichment. Used by command menu items and workflow nodes without an explicit minimum likelihood. When unset, uses 2 for strong identifiers and 6 for name-based matches.',+ type: FieldType.NUMBER,+ isSecret: false,+ },+ PDL_COMPANY_MIN_LIKELIHOOD: {+ description:+ 'Default minimum likelihood (1-10) for company enrichment. Used by command menu items and workflow nodes without an explicit minimum likelihood. When unset, uses 2 for strong identifiers and 6 for name-based matches.',+ type: FieldType.NUMBER,+ isSecret: false,+ },},});diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-company-match-params.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-company-match-params.test.tsindex 021a5aeb1d5..2bb7e3b94a7 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-company-match-params.test.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-company-match-params.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { COMPANY_NODE_MOCK } from 'src/logic-functions/__mocks__/company-node.mock';import { extractCompanyMatchParams } from 'src/logic-functions/utils/extract-company-match-params';@@ -6,6 +6,31 @@ import { extractCompanyMatchParams } from 'src/logic-functions/utils/extract-comconst INPUT = { records: [] };describe('extractCompanyMatchParams', () => {+ beforeEach(() => {+ vi.stubEnv('PDL_PERSON_MIN_LIKELIHOOD', undefined);+ vi.stubEnv('PDL_COMPANY_MIN_LIKELIHOOD', undefined);+ });++ afterEach(() => {+ vi.unstubAllEnvs();+ });++ it.each([+ { name: 'a PDL ID', node: { ...COMPANY_NODE_MOCK, pdlId: 'pdl-c' } },+ { name: 'a domain', node: COMPANY_NODE_MOCK },+ {+ name: 'a name only',+ node: { ...COMPANY_NODE_MOCK, domainName: null, name: 'Acme' },+ },+ ])('uses the company default when matching by $name', ({ node }) => {+ vi.stubEnv('PDL_PERSON_MIN_LIKELIHOOD', '7');+ vi.stubEnv('PDL_COMPANY_MIN_LIKELIHOOD', '8');++ expect(+ extractCompanyMatchParams({ node, input: INPUT }),+ ).toMatchObject({ minLikelihood: 8 });+ });+it('prefers an existing pdlId with the strong-identifier likelihood', () => {expect(extractCompanyMatchParams({@@ -67,6 +92,8 @@ describe('extractCompanyMatchParams', () => {});it('honors an explicit minLikelihood from the input', () => {+ vi.stubEnv('PDL_COMPANY_MIN_LIKELIHOOD', '8');+expect(extractCompanyMatchParams({node: { ...COMPANY_NODE_MOCK, name: 'Acme' },diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-person-match-params.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-person-match-params.test.tsindex 371ff1e3d8b..f6bd58dfc64 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-person-match-params.test.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/extract-person-match-params.test.ts@@ -1,9 +1,39 @@-import { describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { PERSON_NODE_MOCK } from 'src/logic-functions/__mocks__/person-node.mock';import { extractPersonMatchParams } from 'src/logic-functions/utils/extract-person-match-params';describe('extractPersonMatchParams', () => {+ beforeEach(() => {+ vi.stubEnv('PDL_PERSON_MIN_LIKELIHOOD', undefined);+ vi.stubEnv('PDL_COMPANY_MIN_LIKELIHOOD', undefined);+ });++ afterEach(() => {+ vi.unstubAllEnvs();+ });++ it.each([+ { name: 'a PDL ID', node: { ...PERSON_NODE_MOCK, pdlId: 'pdl-1' } },+ { name: 'a LinkedIn profile', node: PERSON_NODE_MOCK },+ {+ name: 'a name and company',+ node: {+ ...PERSON_NODE_MOCK,+ linkedinLink: null,+ name: { firstName: 'Jane', lastName: 'Doe' },+ company: { id: 'co-1', name: 'Acme' },+ },+ },+ ])('uses the person default when matching by $name', ({ node }) => {+ vi.stubEnv('PDL_PERSON_MIN_LIKELIHOOD', '7');+ vi.stubEnv('PDL_COMPANY_MIN_LIKELIHOOD', '8');++ expect(+ extractPersonMatchParams({ node, input: { records: [] } }),+ ).toMatchObject({ minLikelihood: 7 });+ });+it('prefers an existing pdlId and uses the strong-identifier likelihood', () => {expect(extractPersonMatchParams({@@ -53,6 +83,8 @@ describe('extractPersonMatchParams', () => {});it('honors an explicit minLikelihood from the input', () => {+ vi.stubEnv('PDL_PERSON_MIN_LIKELIHOOD', '7');+expect(extractPersonMatchParams({node: PERSON_NODE_MOCK,diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/resolve-min-likelihood.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/resolve-min-likelihood.test.tsindex cafa756be17..ad3fc97fc44 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/resolve-min-likelihood.test.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/resolve-min-likelihood.test.ts@@ -1,5 +1,6 @@import { describe, expect, it } from 'vitest';+import { PdlConfigError } from 'src/logic-functions/errors/pdl-config-error';import { PdlInvalidInputError } from 'src/logic-functions/errors/pdl-invalid-input-error';import { resolveMinLikelihood } from 'src/logic-functions/utils/resolve-min-likelihood';@@ -16,11 +17,85 @@ describe('resolveMinLikelihood', () => {},);+ it.each(['', ' '])(+ 'keeps identifier-dependent defaults when the configured default is %j',+ (defaultMinLikelihood) => {+ expect(+ resolveMinLikelihood({+ inputMinLikelihood: undefined,+ defaultMinLikelihood,+ hasStrongIdentifier: true,+ }),+ ).toBe(2);+ expect(+ resolveMinLikelihood({+ inputMinLikelihood: undefined,+ defaultMinLikelihood,+ hasStrongIdentifier: false,+ }),+ ).toBe(6);+ },+ );++ it.each(['1', '7', '10', ' 7 '])(+ 'uses the configured default of %j regardless of identifier strength',+ (defaultMinLikelihood) => {+ expect(+ resolveMinLikelihood({+ inputMinLikelihood: undefined,+ defaultMinLikelihood,+ hasStrongIdentifier: true,+ }),+ ).toBe(Number(defaultMinLikelihood));+ expect(+ resolveMinLikelihood({+ inputMinLikelihood: null,+ defaultMinLikelihood,+ hasStrongIdentifier: false,+ }),+ ).toBe(Number(defaultMinLikelihood));+ },+ );++ it.each(['8', 'invalid'])(+ 'uses an explicit likelihood over the configured default of %j',+ (defaultMinLikelihood) => {+ expect(+ resolveMinLikelihood({+ inputMinLikelihood: 4,+ defaultMinLikelihood,+ hasStrongIdentifier: true,+ }),+ ).toBe(4);+ },+ );++ it.each(['-1', '0', '11', '1.5', 'NaN', 'Infinity', '7invalid'])(+ 'rejects an invalid configured default of %j',+ (defaultMinLikelihood) => {+ expect(() =>+ resolveMinLikelihood({+ inputMinLikelihood: undefined,+ defaultMinLikelihood,+ hasStrongIdentifier: true,+ }),+ ).toThrow(+ new PdlConfigError(+ 'Default minimum likelihood must be an integer between 1 and 10.',+ ),+ );+ },+ );+it.each([1, 6, 10])('uses an explicit likelihood of %s regardless of identifier strength',(inputMinLikelihood) => {expect(- resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: true }),+ resolveMinLikelihood({+ inputMinLikelihood,+ defaultMinLikelihood: '7',+ hasStrongIdentifier: true,+ }),).toBe(inputMinLikelihood);expect(resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: false }),diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-company-match-params.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-company-match-params.tsindex 95986136093..bab7fb9cb78 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-company-match-params.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-company-match-params.ts@@ -21,6 +21,7 @@ export const extractCompanyMatchParams = ({pdlId: existingPdlId,minLikelihood: resolveMinLikelihood({inputMinLikelihood: input.minLikelihood,+ defaultMinLikelihood: process.env.PDL_COMPANY_MIN_LIKELIHOOD,hasStrongIdentifier: true,}),};@@ -46,6 +47,7 @@ export const extractCompanyMatchParams = ({...companyMatchParams,minLikelihood: resolveMinLikelihood({inputMinLikelihood: input.minLikelihood,+ defaultMinLikelihood: process.env.PDL_COMPANY_MIN_LIKELIHOOD,hasStrongIdentifier,}),};diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-person-match-params.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-person-match-params.tsindex 608a76cfb52..e8a85f24c58 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-person-match-params.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/extract-person-match-params.ts@@ -20,6 +20,7 @@ export const extractPersonMatchParams = ({pdlId: existingPdlId,minLikelihood: resolveMinLikelihood({inputMinLikelihood: input.minLikelihood,+ defaultMinLikelihood: process.env.PDL_PERSON_MIN_LIKELIHOOD,hasStrongIdentifier: true,}),};@@ -53,6 +54,7 @@ export const extractPersonMatchParams = ({...personMatchParams,minLikelihood: resolveMinLikelihood({inputMinLikelihood: input.minLikelihood,+ defaultMinLikelihood: process.env.PDL_PERSON_MIN_LIKELIHOOD,hasStrongIdentifier,}),};diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/resolve-min-likelihood.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/resolve-min-likelihood.tsindex 03b398b2505..add30423344 100644--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/resolve-min-likelihood.ts+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/resolve-min-likelihood.ts@@ -1,4 +1,6 @@+import { PdlConfigError } from 'src/logic-functions/errors/pdl-config-error';import { PdlInvalidInputError } from 'src/logic-functions/errors/pdl-invalid-input-error';+import { toText } from 'src/logic-functions/utils/to-text';import { isDefined } from 'src/utils/is-defined';const MIN_LIKELIHOOD = 1;@@ -8,27 +10,42 @@ const WEAK_IDENTIFIER_MIN_LIKELIHOOD = 6;export const resolveMinLikelihood = ({inputMinLikelihood,+ defaultMinLikelihood,hasStrongIdentifier,}: {inputMinLikelihood: number | null | undefined;+ defaultMinLikelihood?: string;hasStrongIdentifier: boolean;}): number => {- if (!isDefined(inputMinLikelihood)) {+ const configuredDefaultMinLikelihood = toText(defaultMinLikelihood);+ const minLikelihood =+ inputMinLikelihood ??+ (isDefined(configuredDefaultMinLikelihood)+ ? Number(configuredDefaultMinLikelihood)+ : undefined);++ if (!isDefined(minLikelihood)) {return hasStrongIdentifier? DEFAULT_MIN_LIKELIHOOD: WEAK_IDENTIFIER_MIN_LIKELIHOOD;}const isValidLikelihood =- Number.isInteger(inputMinLikelihood) &&- inputMinLikelihood >= MIN_LIKELIHOOD &&- inputMinLikelihood <= MAX_LIKELIHOOD;+ Number.isInteger(minLikelihood) &&+ minLikelihood >= MIN_LIKELIHOOD &&+ minLikelihood <= MAX_LIKELIHOOD;++ if (isValidLikelihood) {+ return minLikelihood;+ }- if (!isValidLikelihood) {+ if (isDefined(inputMinLikelihood)) {throw new PdlInvalidInputError(`Minimum likelihood must be an integer between ${MIN_LIKELIHOOD} and ${MAX_LIKELIHOOD}.`,);}- return inputMinLikelihood;+ throw new PdlConfigError(+ `Default minimum likelihood must be an integer between ${MIN_LIKELIHOOD} and ${MAX_LIKELIHOOD}.`,+ );};