@@ -192,18 +192,21 @@ async function ensureConversationIncludes(
192192function createConversationContext (
193193 baseContext : GuardrailLLMContext ,
194194 conversation : NormalizedConversationEntry [ ]
195- ) : GuardrailLLMContext & { getConversationHistory : ( ) => NormalizedConversationEntry [ ] } {
195+ ) : GuardrailLLMContext & {
196+ conversationHistory : NormalizedConversationEntry [ ] ;
197+ getConversationHistory : ( ) => NormalizedConversationEntry [ ] ;
198+ } {
196199 const historySnapshot = cloneEntries ( conversation ) ;
197- const guardrailContext : GuardrailLLMContext & {
198- getConversationHistory ?: ( ) => NormalizedConversationEntry [ ] ;
199- } = {
200+ const getHistory = ( ) => cloneEntries ( historySnapshot ) ;
201+
202+ // Expose conversation_history as both a property and a method for compatibility
203+ const guardrailContext = {
200204 ...baseContext ,
205+ conversationHistory : historySnapshot ,
206+ getConversationHistory : getHistory ,
201207 } ;
202208
203- guardrailContext . getConversationHistory = ( ) => cloneEntries ( historySnapshot ) ;
204- return guardrailContext as GuardrailLLMContext & {
205- getConversationHistory : ( ) => NormalizedConversationEntry [ ] ;
206- } ;
209+ return guardrailContext ;
207210}
208211
209212function normalizeAgentInput ( input : unknown ) : NormalizedConversationEntry [ ] {
@@ -612,6 +615,11 @@ async function createInputGuardrailsFromStage(
612615) : Promise < InputGuardrail [ ] > {
613616 const guardrails : ConfiguredGuardrail [ ] = await instantiateGuardrails ( stageConfig ) ;
614617
618+ // Optimization: Check if any guardrail in this stage needs conversation history
619+ const needsConversationHistory = guardrails . some (
620+ ( g ) => g . definition . metadata ?. usesConversationHistory
621+ ) ;
622+
615623 return guardrails . map ( ( guardrail : ConfiguredGuardrail ) => ( {
616624 name : `${ stageName } : ${ guardrail . definition . name || 'Unknown Guardrail' } ` ,
617625 execute : async ( args : InputGuardrailFunctionArgs ) => {
@@ -621,8 +629,18 @@ async function createInputGuardrailsFromStage(
621629 const guardContext = ensureGuardrailContext ( context , agentContext ) ;
622630
623631 const normalizedItems = normalizeAgentInput ( input ) ;
624- const conversationHistory = await ensureConversationIncludes ( normalizedItems ) ;
625- const ctxWithConversation = createConversationContext ( guardContext , conversationHistory ) ;
632+ let ctxWithConversation : GuardrailLLMContext ;
633+ let conversationHistory : NormalizedConversationEntry [ ] ;
634+
635+ // Only load conversation history if at least one guardrail in this stage needs it
636+ if ( needsConversationHistory ) {
637+ conversationHistory = await ensureConversationIncludes ( normalizedItems ) ;
638+ ctxWithConversation = createConversationContext ( guardContext , conversationHistory ) ;
639+ } else {
640+ conversationHistory = normalizedItems ;
641+ ctxWithConversation = guardContext ;
642+ }
643+
626644 const inputText = resolveInputText ( input , conversationHistory ) ;
627645
628646 const result : GuardrailResult = await guardrail . run ( ctxWithConversation , inputText ) ;
@@ -663,6 +681,11 @@ async function createOutputGuardrailsFromStage(
663681) : Promise < OutputGuardrail [ ] > {
664682 const guardrails : ConfiguredGuardrail [ ] = await instantiateGuardrails ( stageConfig ) ;
665683
684+ // Optimization: Check if any guardrail in this stage needs conversation history
685+ const needsConversationHistory = guardrails . some (
686+ ( g ) => g . definition . metadata ?. usesConversationHistory
687+ ) ;
688+
666689 return guardrails . map ( ( guardrail : ConfiguredGuardrail ) => ( {
667690 name : `${ stageName } : ${ guardrail . definition . name || 'Unknown Guardrail' } ` ,
668691 execute : async ( args : OutputGuardrailFunctionArgs ) => {
@@ -673,8 +696,15 @@ async function createOutputGuardrailsFromStage(
673696
674697 const outputText = resolveOutputText ( agentOutput ) ;
675698 const normalizedItems = normalizeAgentOutput ( outputText ) ;
676- const conversationHistory = await ensureConversationIncludes ( normalizedItems ) ;
677- const ctxWithConversation = createConversationContext ( guardContext , conversationHistory ) ;
699+ let ctxWithConversation : GuardrailLLMContext ;
700+
701+ // Only load conversation history if at least one guardrail in this stage needs it
702+ if ( needsConversationHistory ) {
703+ const conversationHistory = await ensureConversationIncludes ( normalizedItems ) ;
704+ ctxWithConversation = createConversationContext ( guardContext , conversationHistory ) ;
705+ } else {
706+ ctxWithConversation = guardContext ;
707+ }
678708
679709 const result : GuardrailResult = await guardrail . run ( ctxWithConversation , outputText ) ;
680710
0 commit comments