@@ -7,7 +7,7 @@ import mustache from 'mustache';
77import { CustomFunction , UrlFunction , Prisma , User , SystemPrompt } from '@prisma/client' ;
88import { PrismaService } from 'prisma/prisma.service' ;
99import {
10- ArgumentTypes ,
10+ ArgumentsMetadata ,
1111 Auth ,
1212 Body ,
1313 FunctionArgument ,
@@ -39,7 +39,8 @@ export class FunctionService {
3939 private readonly httpService : HttpService ,
4040 private readonly eventService : EventService ,
4141 private readonly aiService : AiService ,
42- ) { }
42+ ) {
43+ }
4344
4445 create ( data : Omit < Prisma . UrlFunctionCreateInput , 'createdAt' > ) : Promise < UrlFunction > {
4546 return this . prisma . urlFunction . create ( {
@@ -256,15 +257,16 @@ export class FunctionService {
256257 }
257258 }
258259
259- private toArgument ( argument : string , argumentTypes : ArgumentTypes ) : FunctionArgument {
260+ private toArgument ( argument : string , argumentsMetadata : ArgumentsMetadata ) : FunctionArgument {
260261 return {
261262 name : argument ,
262- type : argumentTypes [ argument ] || 'string' ,
263+ type : argumentsMetadata [ argument ] ?. type || 'string' ,
264+ payload : argumentsMetadata [ argument ] ?. payload || false ,
263265 } ;
264266 }
265267
266268 getArguments ( urlFunction : UrlFunction ) : FunctionArgument [ ] {
267- const toArgument = ( arg : string ) => this . toArgument ( arg , JSON . parse ( urlFunction . argumentTypes || '{}' ) ) ;
269+ const toArgument = ( arg : string ) => this . toArgument ( arg , JSON . parse ( urlFunction . argumentsMetadata || '{}' ) ) ;
268270 let args = [ ] ;
269271
270272 args = args . concat ( urlFunction . url . match ( ARGUMENT_PATTERN ) ?. map ( toArgument ) || [ ] ) ;
@@ -401,18 +403,40 @@ export class FunctionService {
401403 }
402404
403405 private getArgumentsMap ( urlFunction : UrlFunction , args : any [ ] ) {
404- const normalizeArg = ( arg ) => {
406+ const normalizeArg = ( arg : any ) => {
405407 if ( typeof arg === 'string' ) {
406408 return arg . replaceAll ( '\r\n' , '\n' ) . trim ( ) ;
407409 } else {
408410 return arg ;
409411 }
410412 } ;
411413
412- return this . getArguments ( urlFunction ) . reduce (
413- ( result , arg , index ) => Object . assign ( result , { [ arg . name ] : normalizeArg ( args [ index ] ) } ) ,
414- { } ,
415- ) ;
414+ const functionArgs = this . getArguments ( urlFunction ) ;
415+ const getPayloadArgs = ( ) => {
416+ const payloadArgs = functionArgs . filter ( ( arg ) => arg . payload ) ;
417+ if ( payloadArgs . length === 0 ) {
418+ return { } ;
419+ }
420+ const payload = args [ args . length - 1 ] ;
421+ if ( typeof payload !== 'object' ) {
422+ this . logger . debug ( `Expecting payload as object, but it is not: ${ JSON . stringify ( payload ) } ` ) ;
423+ return { } ;
424+ }
425+ return payloadArgs . reduce (
426+ ( result , arg ) => Object . assign ( result , { [ arg . name ] : normalizeArg ( payload [ toCamelCase ( arg . name ) ] ) } ) ,
427+ { } ,
428+ ) ;
429+ } ;
430+
431+ return {
432+ ...functionArgs
433+ . filter ( ( arg ) => ! arg . payload )
434+ . reduce (
435+ ( result , arg , index ) => Object . assign ( result , { [ arg . name ] : normalizeArg ( args [ index ] ) } ) ,
436+ { } ,
437+ ) ,
438+ ...getPayloadArgs ( ) ,
439+ } ;
416440 }
417441
418442 private getAuthorizationHeaders ( auth : Auth | null ) {
@@ -541,7 +565,7 @@ export class FunctionService {
541565 } ) ;
542566 }
543567
544- async updateUrlFunction ( user : User , urlFunction : UrlFunction , name : string | null , context : string | null , description : string | null , argumentTypes : ArgumentTypes | null ) {
568+ async updateUrlFunction ( user : User , urlFunction : UrlFunction , name : string | null , context : string | null , description : string | null , argumentsMetadata : ArgumentsMetadata | null ) {
545569 if ( name != null || context != null ) {
546570 name = await this . resolveFunctionName ( user , name , urlFunction . context , false ) ;
547571
@@ -579,7 +603,7 @@ export class FunctionService {
579603 name : name || urlFunction . name ,
580604 context : context == null ? urlFunction . context : context ,
581605 description : description == null ? urlFunction . description : description ,
582- argumentTypes : JSON . stringify ( this . resolveArgumentTypes ( urlFunction . argumentTypes , argumentTypes ) ) ,
606+ argumentsMetadata : JSON . stringify ( this . resolveArgumentTypes ( urlFunction . argumentsMetadata , argumentsMetadata ) ) ,
583607 responseType,
584608 } ,
585609 } ) ;
@@ -666,10 +690,10 @@ export class FunctionService {
666690 excludedIds == null
667691 ? undefined
668692 : {
669- id : {
670- notIn : excludedIds ,
671- } ,
693+ id : {
694+ notIn : excludedIds ,
672695 } ,
696+ } ,
673697 } ,
674698 } ) ;
675699 if ( urlFunction ) {
@@ -687,10 +711,10 @@ export class FunctionService {
687711 excludedIds == null
688712 ? undefined
689713 : {
690- id : {
691- notIn : excludedIds ,
692- } ,
714+ id : {
715+ notIn : excludedIds ,
693716 } ,
717+ } ,
694718 } ,
695719 } ) ;
696720 if ( customFunction ) {
@@ -856,10 +880,10 @@ export class FunctionService {
856880 }
857881 }
858882
859- private resolveArgumentTypes ( argumentTypes : string | null , updatedArgumentTypes : ArgumentTypes | null ) {
883+ private resolveArgumentTypes ( argumentsMetadata : string | null , updatedArgumentsMetadata : ArgumentsMetadata | null ) {
860884 return {
861- ...JSON . parse ( argumentTypes || '{}' ) ,
862- ...updatedArgumentTypes ,
885+ ...JSON . parse ( argumentsMetadata || '{}' ) ,
886+ ...updatedArgumentsMetadata ,
863887 } ;
864888 }
865889
@@ -881,11 +905,11 @@ export class FunctionService {
881905 }
882906
883907 this . logger . debug ( `Creating new SystemPrompt...` ) ;
884- return this . prisma . systemPrompt . create ( {
885- data : {
886- userId : userId ,
887- content : prompt ,
888- } ,
889- } ) ;
908+ return this . prisma . systemPrompt . create ( {
909+ data : {
910+ userId : userId ,
911+ content : prompt ,
912+ } ,
913+ } ) ;
890914 }
891915}
0 commit comments