Skip to content

The open alternative to Salesforce, designed for AI.

Expose minimum likelihood in enrichment workflows (#26040)

Raphaël Bosi712d1c11848a
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-companies.function.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-companies.function.ts
index a47594508d5..a92cdeeb299 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-companies.function.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-companies.function.ts
@@ -38,6 +38,10 @@ export default defineLogicFunction({
label: 'Update fields',
enum: [...UPDATE_FIELDS_OPTION_VALUES],
},
+ minLikelihood: {
+ type: 'number',
+ label: 'Minimum likelihood (1-10)',
+ },
},
},
],
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-company.function.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-company.function.ts
index d0de5b5419c..3feb0e16019 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-company.function.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-company.function.ts
@@ -32,6 +32,10 @@ export default defineLogicFunction({
label: 'Update fields',
enum: [...UPDATE_FIELDS_OPTION_VALUES],
},
+ minLikelihood: {
+ type: 'number',
+ label: 'Minimum likelihood (1-10)',
+ },
},
},
],
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-people.function.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-people.function.ts
index 734c02b48d9..3bc5eecfa7e 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-people.function.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-people.function.ts
@@ -38,6 +38,10 @@ export default defineLogicFunction({
label: 'Update fields',
enum: [...UPDATE_FIELDS_OPTION_VALUES],
},
+ minLikelihood: {
+ type: 'number',
+ label: 'Minimum likelihood (1-10)',
+ },
},
},
],
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-person.function.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-person.function.ts
index 0b3d4170d39..aaa7e31e7ac 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-person.function.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/enrich-person.function.ts
@@ -32,6 +32,10 @@ export default defineLogicFunction({
label: 'Update fields',
enum: [...UPDATE_FIELDS_OPTION_VALUES],
},
+ minLikelihood: {
+ type: 'number',
+ label: 'Minimum likelihood (1-10)',
+ },
},
},
],
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/handlers/__tests__/enrich-people.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/handlers/__tests__/enrich-people.test.ts
index 7ffe872758d..c9f9e1d20d6 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/handlers/__tests__/enrich-people.test.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/handlers/__tests__/enrich-people.test.ts
@@ -74,6 +74,21 @@ beforeEach(() => {
});
describe('enrichPeopleCore', () => {
+ it('rejects an invalid likelihood before calling PDL or writing records', async () => {
+ const captured: Captured = {};
+ const client = buildClient({ people: [PERSON_NODE_MOCK], captured });
+
+ await expect(
+ enrichPeopleCore({
+ input: { records: [{ id: 'p1' }], minLikelihood: 11 },
+ client,
+ }),
+ ).rejects.toThrow('Minimum likelihood must be an integer between 1 and 10.');
+
+ expect(enrichPeopleMock).not.toHaveBeenCalled();
+ expect(captured).toEqual({});
+ });
+
it('fills empty standard fields and writes pdl metadata via updatePerson on a match', async () => {
enrichPeopleMock.mockResolvedValue([
{
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.ts
new file mode 100644
index 00000000000..cafa756be17
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/resolve-min-likelihood.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from 'vitest';
+
+import { PdlInvalidInputError } from 'src/logic-functions/errors/pdl-invalid-input-error';
+import { resolveMinLikelihood } from 'src/logic-functions/utils/resolve-min-likelihood';
+
+describe('resolveMinLikelihood', () => {
+ it.each([undefined, null])(
+ 'keeps identifier-dependent defaults when the input is %s',
+ (inputMinLikelihood) => {
+ expect(
+ resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: true }),
+ ).toBe(2);
+ expect(
+ resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: false }),
+ ).toBe(6);
+ },
+ );
+
+ it.each([1, 6, 10])(
+ 'uses an explicit likelihood of %s regardless of identifier strength',
+ (inputMinLikelihood) => {
+ expect(
+ resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: true }),
+ ).toBe(inputMinLikelihood);
+ expect(
+ resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: false }),
+ ).toBe(inputMinLikelihood);
+ },
+ );
+
+ it.each([-1, 0, 11, 1.5, NaN, Infinity])(
+ 'rejects an invalid likelihood of %s',
+ (inputMinLikelihood) => {
+ expect(() =>
+ resolveMinLikelihood({ inputMinLikelihood, hasStrongIdentifier: true }),
+ ).toThrow(
+ new PdlInvalidInputError(
+ 'Minimum likelihood must be an integer between 1 and 10.',
+ ),
+ );
+ },
+ );
+});
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.ts
index f0f6515b484..03b398b2505 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,3 +1,8 @@
+import { PdlInvalidInputError } from 'src/logic-functions/errors/pdl-invalid-input-error';
+import { isDefined } from 'src/utils/is-defined';
+
+const MIN_LIKELIHOOD = 1;
+const MAX_LIKELIHOOD = 10;
const DEFAULT_MIN_LIKELIHOOD = 2;
const WEAK_IDENTIFIER_MIN_LIKELIHOOD = 6;
@@ -5,8 +10,25 @@ export const resolveMinLikelihood = ({
inputMinLikelihood,
hasStrongIdentifier,
}: {
- inputMinLikelihood: number | undefined;
+ inputMinLikelihood: number | null | undefined;
hasStrongIdentifier: boolean;
-}): number =>
- inputMinLikelihood ??
- (hasStrongIdentifier ? DEFAULT_MIN_LIKELIHOOD : WEAK_IDENTIFIER_MIN_LIKELIHOOD);
+}): number => {
+ if (!isDefined(inputMinLikelihood)) {
+ return hasStrongIdentifier
+ ? DEFAULT_MIN_LIKELIHOOD
+ : WEAK_IDENTIFIER_MIN_LIKELIHOOD;
+ }
+
+ const isValidLikelihood =
+ Number.isInteger(inputMinLikelihood) &&
+ inputMinLikelihood >= MIN_LIKELIHOOD &&
+ inputMinLikelihood <= MAX_LIKELIHOOD;
+
+ if (!isValidLikelihood) {
+ throw new PdlInvalidInputError(
+ `Minimum likelihood must be an integer between ${MIN_LIKELIHOOD} and ${MAX_LIKELIHOOD}.`,
+ );
+ }
+
+ return inputMinLikelihood;
+};