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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(math-helper): clarify the inputs and validate numeric ranges (#15679)
Talal Jaber02a6ab9dc6e1
diff --git a/bun.lock b/bun.lockindex 77f33076c0e..78b6598a934 100644--- a/bun.lock+++ b/bun.lock@@ -10493,7 +10493,7 @@},"packages/pieces/core/math-helper": {"name": "@activepieces/piece-math-helper",- "version": "0.0.31",+ "version": "0.1.0","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",diff --git a/packages/pieces/core/math-helper/package.json b/packages/pieces/core/math-helper/package.jsonindex 794fded106d..796208c6b44 100644--- a/packages/pieces/core/math-helper/package.json+++ b/packages/pieces/core/math-helper/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-math-helper",- "version": "0.0.31",+ "version": "0.1.0","main": "./dist/src/index.js","types": "./dist/src/index.d.ts","bundleDeps": true,diff --git a/packages/pieces/core/math-helper/src/index.ts b/packages/pieces/core/math-helper/src/index.tsindex b6c5aebdaf7..69f65c82265 100644--- a/packages/pieces/core/math-helper/src/index.ts+++ b/packages/pieces/core/math-helper/src/index.ts@@ -6,13 +6,9 @@ import { modulo } from './lib/actions/modulo';import { multiplication } from './lib/actions/multiplication';import { subtraction } from './lib/actions/subtraction';-const markdownDescription = `-Perform mathematical operations.-`;-export const math = createPiece({displayName: 'Math Helper',- description: markdownDescription,+ description: 'Add, subtract, multiply, divide, or pick a random number.',auth: PieceAuth.None(),minimumSupportedRelease: '0.36.1',logoUrl: 'https://cdn.activepieces.com/pieces/new-core/math-helper.svg',diff --git a/packages/pieces/core/math-helper/src/lib/actions/addition.ts b/packages/pieces/core/math-helper/src/lib/actions/addition.tsindex 20ea3214eb4..13ed397b311 100644--- a/packages/pieces/core/math-helper/src/lib/actions/addition.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/addition.ts@@ -12,17 +12,15 @@ export const addition = createAction({outputSchema: additionActionOutputSchema,auth: PieceAuth.None(),displayName: 'Addition',- description: 'Add the first number and the second number',+ description: 'Add two numbers together.',aiMetadata: { description: 'Compute the sum of exactly two numbers, returning first_number + second_number. Pick this only for a two-operand addition; use the sibling Subtraction, Multiplication, Division, or Modulo actions for other operations, and the Code piece for summing a list or evaluating any multi-term formula. Both operands are required and must be numeric; read-only and idempotent, with no external side effects.', idempotent: true },props: {first_number: Property.Number({displayName: 'First Number',- description: undefined,required: true,}),second_number: Property.Number({displayName: 'Second Number',- description: undefined,required: true,}),},diff --git a/packages/pieces/core/math-helper/src/lib/actions/division.ts b/packages/pieces/core/math-helper/src/lib/actions/division.tsindex 558ced74276..faec13a6cdb 100644--- a/packages/pieces/core/math-helper/src/lib/actions/division.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/division.ts@@ -14,17 +14,17 @@ export const division = createAction({outputSchema: divisionActionOutputSchema,auth: PieceAuth.None(),displayName: 'Division',- description: 'Divide first number by the second number',+ description: 'Divide the first number by the second number.',aiMetadata: { description: 'Compute the quotient of two numbers, returning first_number / second_number as a floating-point value rather than integer division. Pick this for a two-operand division such as an average, ratio, or unit price; use the sibling Modulo action when you need the remainder instead, or the other Math Helper siblings for the remaining operations. Critical constraint: second_number is validated and must not be zero, or the step fails before computing; read-only and idempotent.', idempotent: true },props: {first_number: Property.Number({displayName: 'First Number',- description: undefined,+ description: 'The number to divide.',required: true,}),second_number: Property.Number({displayName: 'Second Number',- description: undefined,+ description: 'The number to divide by. Cannot be zero.',required: true,}),},diff --git a/packages/pieces/core/math-helper/src/lib/actions/generateRandom.ts b/packages/pieces/core/math-helper/src/lib/actions/generateRandom.tsindex 5fae2f48cd4..8bcb569ba72 100644--- a/packages/pieces/core/math-helper/src/lib/actions/generateRandom.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/generateRandom.ts@@ -11,24 +11,31 @@ export const generateRandom = createAction({classification: 'READ',outputSchema: generateRandomActionOutputSchema,auth: PieceAuth.None(),- displayName: 'Generate Random Number',- description: 'Generate random number between two numbers (inclusive)',- aiMetadata: { description: 'Draw a pseudo-random integer uniformly from the inclusive range between first_number (the minimum) and second_number (the maximum). Pick this for sampling, jitter, or picking an arbitrary index; it is not cryptographically secure, so use the Crypto piece for tokens, secrets, or IDs, and the arithmetic siblings for deterministic math. It returns whole numbers only, and the range is honoured only when second_number is at least first_number; not idempotent, since each call returns a different value for identical inputs.', idempotent: false },+ displayName: 'Random Number',+ description: 'Pick a random whole number from a range, ends included.',+ aiMetadata: { description: 'Draw a pseudo-random integer uniformly from the inclusive range between first_number (the minimum) and second_number (the maximum). Pick this for sampling, jitter, or picking an arbitrary index; it is not cryptographically secure, so use the Crypto piece for tokens, secrets, or IDs, and the arithmetic siblings for deterministic math. It returns whole numbers only; decimal bounds are rounded inward and reversed bounds are swapped, so the result always lies inside the range, and the step fails when the range holds no whole number at all (1.2 to 1.8); not idempotent, since each call returns a different value for identical inputs.', idempotent: false },props: {first_number: Property.Number({- displayName: 'First Number',- description: undefined,+ displayName: 'Minimum',+ description: 'Lowest whole number that can be returned.',required: true,}),second_number: Property.Number({- displayName: 'Second Number',- description: undefined,+ displayName: 'Maximum',+ description: 'Highest whole number that can be returned.',required: true,}),},async run(context) {- const min = context.propsValue['first_number'];- const max = context.propsValue['second_number'];- return Math.floor(Math.random() * (max - min + 1) + min);+ const first = context.propsValue['first_number'];+ const second = context.propsValue['second_number'];+ const low = Math.ceil(Math.min(first, second));+ const high = Math.floor(Math.max(first, second));+ if (high < low) {+ throw new Error(+ 'There is no whole number between the minimum and the maximum.',+ );+ }+ return Math.floor(Math.random() * (high - low + 1) + low);},});diff --git a/packages/pieces/core/math-helper/src/lib/actions/modulo.ts b/packages/pieces/core/math-helper/src/lib/actions/modulo.tsindex c6b6d215f69..ae9823e88c9 100644--- a/packages/pieces/core/math-helper/src/lib/actions/modulo.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/modulo.ts@@ -3,6 +3,8 @@ import {PieceAuth,Property,} from '@activepieces/pieces-framework';+import { propsValidation } from '@activepieces/pieces-common';+import * as z from 'zod/mini';import { moduloActionOutputSchema } from '../output-schemas';export const modulo = createAction({@@ -12,21 +14,26 @@ export const modulo = createAction({outputSchema: moduloActionOutputSchema,auth: PieceAuth.None(),displayName: 'Modulo',- description: 'Get the remainder of the first number divided by second number',- aiMetadata: { description: 'Compute the remainder of first_number divided by second_number using JavaScript % semantics, so the sign of the result follows the dividend (-7 with divisor 3 yields -1, not 2). Pick this for divisibility or every-Nth checks and for cycling an index over a fixed range; use the sibling Division action when you want the quotient instead. Unlike Division, second_number is not validated here, so a zero divisor returns NaN rather than failing the step; read-only and idempotent.', idempotent: true },+ description: 'Get the remainder after dividing the first number by the second.',+ aiMetadata: { description: 'Compute the remainder of first_number divided by second_number using JavaScript % semantics, so the sign of the result follows the dividend (-7 with divisor 3 yields -1, not 2). Pick this for divisibility or every-Nth checks and for cycling an index over a fixed range; use the sibling Division action when you want the quotient instead. Like Division, second_number is validated and must not be zero, or the step fails before computing; read-only and idempotent.', idempotent: true },props: {first_number: Property.Number({displayName: 'First Number',- description: undefined,+ description: 'The number to divide.',required: true,}),second_number: Property.Number({displayName: 'Second Number',- description: undefined,+ description: 'The number to divide by. Cannot be zero.',required: true,}),},async run(context) {+ await propsValidation.validateZod(context.propsValue, {+ second_number: z.number().check(+ z.refine((val) => val !== 0, 'Second number cannot be zero'),+ ),+ });return (context.propsValue['first_number'] % context.propsValue['second_number']);diff --git a/packages/pieces/core/math-helper/src/lib/actions/multiplication.ts b/packages/pieces/core/math-helper/src/lib/actions/multiplication.tsindex 3b6e912024f..59dad7480a6 100644--- a/packages/pieces/core/math-helper/src/lib/actions/multiplication.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/multiplication.ts@@ -12,17 +12,15 @@ export const multiplication = createAction({outputSchema: multiplicationActionOutputSchema,auth: PieceAuth.None(),displayName: 'Multiplication',- description: 'Multiply first number by the second number',+ description: 'Multiply two numbers together.',aiMetadata: { description: 'Compute the product of exactly two numbers, returning first_number * second_number. Pick this for a two-operand multiplication, including percentage or unit-rate scaling (e.g. an amount times 0.15); use the sibling Addition, Subtraction, Division, or Modulo actions for other operations, and the Code piece for exponents or multi-term formulas. Both operands are required and must be numeric; read-only and idempotent, with no external side effects.', idempotent: true },props: {first_number: Property.Number({displayName: 'First Number',- description: undefined,required: true,}),second_number: Property.Number({displayName: 'Second Number',- description: undefined,required: true,}),},diff --git a/packages/pieces/core/math-helper/src/lib/actions/subtraction.ts b/packages/pieces/core/math-helper/src/lib/actions/subtraction.tsindex 39d09216693..2608a95d15a 100644--- a/packages/pieces/core/math-helper/src/lib/actions/subtraction.ts+++ b/packages/pieces/core/math-helper/src/lib/actions/subtraction.ts@@ -12,17 +12,17 @@ export const subtraction = createAction({outputSchema: subtractionActionOutputSchema,auth: PieceAuth.None(),displayName: 'Subtraction',- description: 'Subtract the first number from the second number',- aiMetadata: { description: 'Compute the difference of two numbers. Critical: the operand order is the reverse of what the prop names suggest, because it returns second_number - first_number; put the amount being subtracted in first_number and the value it is subtracted from in second_number. Pick this for a two-operand subtraction only, using the sibling Addition, Multiplication, Division, or Modulo actions for other operations and the Code piece for multi-term formulas; read-only and idempotent.', idempotent: true },+ description: 'Subtract one number from another.',+ aiMetadata: { description: 'Compute the difference of two numbers. It returns second_number - first_number: first_number is the amount being subtracted (labelled "Number to Subtract") and second_number is the value it is subtracted from (labelled "Starting Number"). Pick this for a two-operand subtraction only, using the sibling Addition, Multiplication, Division, or Modulo actions for other operations and the Code piece for multi-term formulas; read-only and idempotent.', idempotent: true },props: {first_number: Property.Number({- displayName: 'First Number',- description: undefined,+ displayName: 'Number to Subtract',+ description: 'This amount is taken away from the starting number.',required: true,}),second_number: Property.Number({- displayName: 'Second Number',- description: undefined,+ displayName: 'Starting Number',+ description: 'The result is this number minus the number above.',required: true,}),},diff --git a/packages/pieces/core/math-helper/src/lib/output-schemas.ts b/packages/pieces/core/math-helper/src/lib/output-schemas.tsindex 00838582f62..ca895fd30e2 100644--- a/packages/pieces/core/math-helper/src/lib/output-schemas.ts+++ b/packages/pieces/core/math-helper/src/lib/output-schemas.ts@@ -19,7 +19,7 @@ export const additionActionOutputSchema = wholeNumberOutput(export const subtractionActionOutputSchema = wholeNumberOutput('difference','Difference',- 'The second number minus the first number.',+ 'The starting number minus the number to subtract.',);export const multiplicationActionOutputSchema = wholeNumberOutput(@@ -43,5 +43,5 @@ export const moduloActionOutputSchema = wholeNumberOutput(export const generateRandomActionOutputSchema = wholeNumberOutput('randomNumber','Random Number',- 'A pseudo-random integer between the two numbers, inclusive.',+ 'A random whole number between the minimum and maximum, both included.',);diff --git a/packages/pieces/core/math-helper/test/math-helper.test.ts b/packages/pieces/core/math-helper/test/math-helper.test.tsindex 610291d81fd..3403e2e3742 100644--- a/packages/pieces/core/math-helper/test/math-helper.test.ts+++ b/packages/pieces/core/math-helper/test/math-helper.test.ts@@ -108,6 +108,13 @@ describe('modulo', () => {});expect(await modulo.run(ctx)).toBe(-1);});++ test('throws on zero divisor', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 10, second_number: 0 },+ });+ await expect(modulo.run(ctx)).rejects.toThrow();+ });});describe('generateRandom', () => {@@ -120,4 +127,46 @@ describe('generateRandom', () => {expect(result).toBeLessThanOrEqual(10);});+ test('stays inside decimal bounds', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 1.5, second_number: 3.5 },+ });+ for (let i = 0; i < 50; i++) {+ const result = await generateRandom.run(ctx);+ expect(result).toBeGreaterThanOrEqual(2);+ expect(result).toBeLessThanOrEqual(3);+ }+ });++ test('accepts reversed bounds', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 10, second_number: 1 },+ });+ for (let i = 0; i < 50; i++) {+ const result = await generateRandom.run(ctx);+ expect(result).toBeGreaterThanOrEqual(1);+ expect(result).toBeLessThanOrEqual(10);+ }+ });++ test('returns the bound when both are equal', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 7, second_number: 7 },+ });+ expect(await generateRandom.run(ctx)).toBe(7);+ });++ test('throws when the range holds no whole number', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 1.2, second_number: 1.8 },+ });+ await expect(generateRandom.run(ctx)).rejects.toThrow();+ });++ test('throws when both bounds are the same decimal', async () => {+ const ctx = createMockActionContext({+ propsValue: { first_number: 2.5, second_number: 2.5 },+ });+ await expect(generateRandom.run(ctx)).rejects.toThrow();+ });});