@@ -1023,10 +1023,10 @@ export function funcCreateOrEditTool(server: McpServer) {
10231023 changeSetId,
10241024 schemaId,
10251025 } ) ;
1026+ const schemaVariantId = responseUnlockSchema . data . unlockedVariantId ;
10261027
10271028 // prepare the function create parameters
1028- const schemaVariantId = responseUnlockSchema . data . unlockedVariantId ;
1029- const createFuncParams = {
1029+ const baseParams = {
10301030 workspaceId : WORKSPACE_ID ,
10311031 changeSetId,
10321032 schemaId,
@@ -1036,7 +1036,7 @@ export function funcCreateOrEditTool(server: McpServer) {
10361036 // use the correct funciton create endpoint based on the type of function
10371037 if ( functionType === "qualification" ) {
10381038 const responseCreate = await siSchemasApi . createVariantQualification ( {
1039- ...createFuncParams ,
1039+ ...baseParams ,
10401040 createVariantQualificationFuncV1Request : {
10411041 ...requestBody ,
10421042 code : functionCode ?? DEFAULT_QUALIFICATION_FUNCTION ,
@@ -1046,7 +1046,7 @@ export function funcCreateOrEditTool(server: McpServer) {
10461046 touchedFuncId = responseCreate . data . funcId ;
10471047 } else if ( functionType === "codegen" ) {
10481048 const responseCreate = await siSchemasApi . createVariantCodegen ( {
1049- ...createFuncParams ,
1049+ ...baseParams ,
10501050 createVariantCodegenFuncV1Request : {
10511051 ...requestBody ,
10521052 code : functionCode ?? DEFAULT_CODEGEN_FUNCTION ,
@@ -1056,7 +1056,7 @@ export function funcCreateOrEditTool(server: McpServer) {
10561056 touchedFuncId = responseCreate . data . funcId ;
10571057 } else if ( functionType === "management" ) {
10581058 const responseCreate = await siSchemasApi . createVariantManagement ( {
1059- ...createFuncParams ,
1059+ ...baseParams ,
10601060 createVariantManagementFuncV1Request : {
10611061 ...requestBody ,
10621062 code : functionCode ?? DEFAULT_MANAGEMENT_FUNCTION ,
@@ -1070,16 +1070,28 @@ export function funcCreateOrEditTool(server: McpServer) {
10701070 message : "Action kind is required for action functions."
10711071 } ) ;
10721072 }
1073- // else if (actionKind !== "Manual") {
1074- // TODO: Aaron - preemptively protect the user against duplicated action functions
1075- // Currently if the user attempts to make a duplicated action function, there will be an error.
1076- // To catch this error before it happens, we would need to do the following -
1077- // - Get all the variantFuncIds for the unlocked schema variant
1078- // - Get the function details for each of those funcIds
1079- // - Check if any of those functions are action functions of the same kind as the one being created
1080- // }
1073+ else if ( actionKind !== "Manual" ) {
1074+ // Before attempting to create this action, check if an action of the same type already exists.
1075+ const responseGetVariant = await siSchemasApi . getVariant ( {
1076+ ...baseParams ,
1077+ } ) ;
1078+
1079+ let canMakeAction = true ;
1080+ responseGetVariant . data . variantFuncs . forEach ( ( func ) => {
1081+ if ( func . funcKind . kind === "action" && func . funcKind . actionKind === actionKind ) {
1082+ canMakeAction = false ;
1083+ }
1084+ } ) ;
1085+
1086+ if ( ! canMakeAction ) {
1087+ return errorResponse ( {
1088+ message : "An action of the same kind already exists and only one action of each kind is allowed, except for Manual." ,
1089+ hints : "Tell the user that they can't make more than one of this kind of action and ask if they want to make an action of a different kind or edit the existing action."
1090+ } ) ;
1091+ }
1092+ }
10811093 const responseCreate = await siSchemasApi . createVariantAction ( {
1082- ...createFuncParams ,
1094+ ...baseParams ,
10831095 createVariantActionFuncV1Request : {
10841096 ...requestBody ,
10851097 code : functionCode ?? DEFAULT_ACTION_FUNCTION ,
@@ -1100,13 +1112,6 @@ export function funcCreateOrEditTool(server: McpServer) {
11001112 } ;
11011113 return successResponse ( data ) ;
11021114 } catch ( error ) {
1103- const anyError = error as any ;
1104- if ( anyError ?. response ?. data && JSON . stringify ( anyError . response . data ) . includes ( "action with kind" ) ) {
1105- return errorResponse ( {
1106- message : "An action of the same kind already exists and only one action of each kind is allowed, except for Manual." ,
1107- hints : "Tell the user that they can't make more than one of this kind of action and ask if they want to make an action of a different kind."
1108- } ) ;
1109- }
11101115 return errorResponse ( error ) ;
11111116 }
11121117 } ) ;
0 commit comments