The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Require @DefineTool execute input to match the input schema (#28217)
Paul Rastoin67913c9fd0c4
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/__tests__/define-tool.decorator.spec.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/__tests__/define-tool.decorator.spec.tsindex d8fcd62b7e2..066974d62e5 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/__tests__/define-tool.decorator.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/__tests__/define-tool.decorator.spec.ts@@ -50,6 +50,18 @@ describe('DefineTool', () => {}}+ // @ts-expect-error execute must not require fields the inputSchema does not produce+ @DefineTool(RENAME_WEBHOOK_OPTIONS)+ class ToolWithNarrowedInput extends BaseTool<+ typeof renameWebhookInputSchema+ > {+ readonly inputSchema = renameWebhookInputSchema;++ async execute(input: { id: string; name: string }): Promise<ToolOutput> {+ return { success: true, message: input.name.toUpperCase() };+ }+ }+// @ts-expect-error a tool must implement executeclass ToolWithoutExecute extends BaseTool<typeof renameWebhookInputSchema> {readonly inputSchema = renameWebhookInputSchema;@@ -78,9 +90,10 @@ describe('DefineTool', () => {expect([NotATool,LookAlikeTool,+ ToolWithNarrowedInput,ToolWithoutExecute,ToolWithMismatchedInput,ToolWithScalarInput,- ]).toHaveLength(5);+ ]).toHaveLength(6);});});diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/define-tool.decorator.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/define-tool.decorator.tsindex 59985cf428a..a072f897d04 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/define-tool.decorator.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/decorators/define-tool.decorator.ts@@ -1,12 +1,28 @@import { applyDecorators, Injectable, SetMetadata } from '@nestjs/common';+import { type IsExactly } from 'twenty-shared/types';+import { type z } from 'zod';+import { DEFINE_TOOL_METADATA_KEY } from 'src/engine/core-modules/tool-provider/constants/define-tool-metadata-key.constant';import { type BaseTool } from 'src/engine/core-modules/tool-provider/interfaces/base-tool.interface';import { type DefineToolOptions } from 'src/engine/core-modules/tool-provider/types/define-tool-options.type';+// Method parameters are bivariant, so BaseTool alone accepts an execute input narrower than the schema+type ExecuteInputMatchesInputSchema<TTool extends BaseTool> =+ Parameters<TTool['execute']> extends []+ ? unknown+ : IsExactly<+ Parameters<TTool['execute']>[0],+ z.infer<TTool['inputSchema']>+ > extends true+ ? unknown+ : 'execute input must be exactly z.infer<typeof inputSchema>';+export const DefineTool =(options: DefineToolOptions) =>- <TTool extends new (...args: never[]) => BaseTool>(target: TTool): void => {+ <TTool extends new (...args: never[]) => BaseTool>(+ target: TTool & ExecuteInputMatchesInputSchema<InstanceType<TTool>>,+ ): void => {applyDecorators(Injectable(),SetMetadata(DEFINE_TOOL_METADATA_KEY, options),