@@ -358,98 +358,98 @@ describe('GuardrailAgent', () => {
358358
359359 // Test the guardrail function
360360 const guardrailFunction = agent . inputGuardrails [ 0 ] ;
361- const result = await guardrailFunction . execute ( 'test input' ) ;
361+ const result = await guardrailFunction . execute ( 'test input' ) ;
362362
363- expect ( result ) . toHaveProperty ( 'outputInfo' ) ;
364- expect ( result ) . toHaveProperty ( 'tripwireTriggered' ) ;
365- expect ( typeof result . tripwireTriggered ) . toBe ( 'boolean' ) ;
366- } ) ;
363+ expect ( result ) . toHaveProperty ( 'outputInfo' ) ;
364+ expect ( result ) . toHaveProperty ( 'tripwireTriggered' ) ;
365+ expect ( typeof result . tripwireTriggered ) . toBe ( 'boolean' ) ;
366+ } ) ;
367367
368- it ( 'passes the latest user message text to guardrails for conversation inputs' , async ( ) => {
369- process . env . OPENAI_API_KEY = 'test' ;
370- const config = {
371- version : 1 ,
372- input : {
368+ it ( 'passes the latest user message text to guardrails for conversation inputs' , async ( ) => {
369+ process . env . OPENAI_API_KEY = 'test' ;
370+ const config = {
373371 version : 1 ,
374- guardrails : [ { name : 'Moderation' , config : { } } ] ,
375- } ,
376- } ;
372+ input : {
373+ version : 1 ,
374+ guardrails : [ { name : 'Moderation' , config : { } } ] ,
375+ } ,
376+ } ;
377377
378- const { instantiateGuardrails } = await import ( '../../runtime' ) ;
379- const runSpy = vi . fn ( ) . mockResolvedValue ( {
380- tripwireTriggered : false ,
381- info : { guardrail_name : 'Moderation' } ,
382- } ) ;
378+ const { instantiateGuardrails } = await import ( '../../runtime' ) ;
379+ const runSpy = vi . fn ( ) . mockResolvedValue ( {
380+ tripwireTriggered : false ,
381+ info : { guardrail_name : 'Moderation' } ,
382+ } ) ;
383383
384- vi . mocked ( instantiateGuardrails ) . mockImplementationOnce ( ( ) =>
385- Promise . resolve ( [
384+ vi . mocked ( instantiateGuardrails ) . mockImplementationOnce ( ( ) =>
385+ Promise . resolve ( [
386+ {
387+ definition : {
388+ name : 'Moderation' ,
389+ description : 'Moderation guardrail' ,
390+ mediaType : 'text/plain' ,
391+ configSchema : z . object ( { } ) ,
392+ checkFn : vi . fn ( ) ,
393+ metadata : { } ,
394+ ctxRequirements : z . object ( { } ) ,
395+ schema : ( ) => ( { } ) ,
396+ instantiate : vi . fn ( ) ,
397+ } ,
398+ config : { } ,
399+ run : runSpy ,
400+ } as unknown as Parameters < typeof instantiateGuardrails > [ 0 ] extends Promise < infer T >
401+ ? T extends readonly ( infer U ) [ ]
402+ ? U
403+ : never
404+ : never ,
405+ ] )
406+ ) ;
407+
408+ const agent = ( await GuardrailAgent . create (
409+ config ,
410+ 'Conversation Agent' ,
411+ 'Handle multi-turn conversations'
412+ ) ) as MockAgent ;
413+
414+ const guardrail = agent . inputGuardrails [ 0 ] as unknown as {
415+ execute : ( args : { input : unknown ; context ?: unknown } ) => Promise < {
416+ outputInfo : Record < string , unknown > ;
417+ tripwireTriggered : boolean ;
418+ } > ;
419+ } ;
420+
421+ const conversation = [
422+ { role : 'system' , content : 'You are helpful.' } ,
423+ { role : 'user' , content : [ { type : 'input_text' , text : 'First question?' } ] } ,
424+ { role : 'assistant' , content : [ { type : 'output_text' , text : 'An answer.' } ] } ,
386425 {
387- definition : {
388- name : 'Moderation' ,
389- description : 'Moderation guardrail' ,
390- mediaType : 'text/plain' ,
391- configSchema : z . object ( { } ) ,
392- checkFn : vi . fn ( ) ,
393- metadata : { } ,
394- ctxRequirements : z . object ( { } ) ,
395- schema : ( ) => ( { } ) ,
396- instantiate : vi . fn ( ) ,
397- } ,
398- config : { } ,
399- run : runSpy ,
400- } as unknown as Parameters < typeof instantiateGuardrails > [ 0 ] extends Promise < infer T >
401- ? T extends readonly ( infer U ) [ ]
402- ? U
403- : never
404- : never ,
405- ] )
406- ) ;
407-
408- const agent = ( await GuardrailAgent . create (
409- config ,
410- 'Conversation Agent' ,
411- 'Handle multi-turn conversations'
412- ) ) as MockAgent ;
413-
414- const guardrail = agent . inputGuardrails [ 0 ] as unknown as {
415- execute : ( args : { input : unknown ; context ?: unknown } ) => Promise < {
416- outputInfo : Record < string , unknown > ;
417- tripwireTriggered : boolean ;
418- } > ;
419- } ;
420-
421- const conversation = [
422- { role : 'system' , content : 'You are helpful.' } ,
423- { role : 'user' , content : [ { type : 'input_text' , text : 'First question?' } ] } ,
424- { role : 'assistant' , content : [ { type : 'output_text' , text : 'An answer.' } ] } ,
425- {
426- role : 'user' ,
427- content : [
428- { type : 'input_text' , text : 'Latest user message' } ,
429- { type : 'input_text' , text : 'with additional context.' } ,
430- ] ,
431- } ,
432- ] ;
426+ role : 'user' ,
427+ content : [
428+ { type : 'input_text' , text : 'Latest user message' } ,
429+ { type : 'input_text' , text : 'with additional context.' } ,
430+ ] ,
431+ } ,
432+ ] ;
433433
434- const result = await guardrail . execute ( { input : conversation , context : { } } ) ;
434+ const result = await guardrail . execute ( { input : conversation , context : { } } ) ;
435435
436- expect ( runSpy ) . toHaveBeenCalledTimes ( 1 ) ;
437- const [ ctxArgRaw , dataArg ] = runSpy . mock . calls [ 0 ] as [ unknown , string ] ;
438- const ctxArg = ctxArgRaw as { getConversationHistory ?: ( ) => unknown [ ] } ;
439- expect ( dataArg ) . toBe ( 'Latest user message with additional context.' ) ;
440- expect ( typeof ctxArg . getConversationHistory ) . toBe ( 'function' ) ;
436+ expect ( runSpy ) . toHaveBeenCalledTimes ( 1 ) ;
437+ const [ ctxArgRaw , dataArg ] = runSpy . mock . calls [ 0 ] as [ unknown , string ] ;
438+ const ctxArg = ctxArgRaw as { getConversationHistory ?: ( ) => unknown [ ] } ;
439+ expect ( dataArg ) . toBe ( 'Latest user message with additional context.' ) ;
440+ expect ( typeof ctxArg . getConversationHistory ) . toBe ( 'function' ) ;
441441
442- const history = ctxArg . getConversationHistory ?.( ) as Array < { content ?: unknown } > | undefined ;
443- expect ( Array . isArray ( history ) ) . toBe ( true ) ;
444- expect ( history && history [ history . length - 1 ] ?. content ) . toBe (
445- 'Latest user message with additional context.'
446- ) ;
442+ const history = ctxArg . getConversationHistory ?.( ) as Array < { content ?: unknown } > | undefined ;
443+ expect ( Array . isArray ( history ) ) . toBe ( true ) ;
444+ expect ( history && history [ history . length - 1 ] ?. content ) . toBe (
445+ 'Latest user message with additional context.'
446+ ) ;
447447
448- expect ( result . tripwireTriggered ) . toBe ( false ) ;
449- expect ( result . outputInfo . input ) . toBe ( 'Latest user message with additional context.' ) ;
450- } ) ;
448+ expect ( result . tripwireTriggered ) . toBe ( false ) ;
449+ expect ( result . outputInfo . input ) . toBe ( 'Latest user message with additional context.' ) ;
450+ } ) ;
451451
452- it ( 'should handle guardrail execution errors based on raiseGuardrailErrors setting' , async ( ) => {
452+ it ( 'should handle guardrail execution errors based on raiseGuardrailErrors setting' , async ( ) => {
453453 process . env . OPENAI_API_KEY = 'test' ;
454454 const config = {
455455 version : 1 ,
0 commit comments