Class GuardrailValidatingAiModelInvoker
java.lang.Object
com.flowable.agent.engine.impl.guardrail.GuardrailValidatingAiModelInvoker
- All Implemented Interfaces:
AiModelInvoker
Decorator around an
AiModelInvoker that enforces guardrails before and after the LLM invocation.
Before the LLM call (input validation):
- Parameter constraints on input parameters (cheapest, in-process)
- Operation guardrails in configured order (guardrail agents and external guardrails)
After the LLM call (output validation):
- Parameter constraints on output parameters (uses the already-parsed
AgentInvocationResult) - Operation guardrails in configured order (guardrail agents and external guardrails)
This wrapper is only applied to agent models that have guardrails configured.
Each guardrail supports failure modes: fail (throw exception), warn (log and continue),
skip (skip LLM call, set variable), reject (discard LLM output, set variable),
and default (resolve from operation's guardrailDefaults).
Validation is fail-fast: if any step fails with fail, skip, or reject, subsequent steps are skipped.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AgentEngineConfigurationprotected final org.flowable.common.engine.impl.runtime.Clockprotected final AiModelInvokerprotected ExternalGuardrailInvokerResolverprotected GuardrailAgentEvaluatorprotected final tools.jackson.databind.ObjectMapperprotected static final Stringprotected static final Stringprotected static final String -
Constructor Summary
ConstructorsConstructorDescriptionGuardrailValidatingAiModelInvoker(AiModelInvoker delegate, tools.jackson.databind.ObjectMapper objectMapper, org.flowable.common.engine.impl.runtime.Clock clock) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddGuardrailBlockedExchange(AgentInvocationResultResponse.Invocation invocation, GuardrailResult guardrailResult) protected StringbuildConstraintResultJson(boolean passed, String phase, String parameterName, String constraintType, String reason, String action) protected static StringbuildGuardrailAssertionKey(String serviceKey, String operationKey, String applyTo) Composite identifier<serviceKey>/<operationKey>/<applyTo>written into the guardrail-snapshot JSON'sguardrailKeyfield so test assertions can target service-guardrail outcomes viaguardrail.<composite>.<field>.protected StringbuildGuardrailResultJson(boolean passed, String phase, String subType, String sourceKey, String guardrailKey, String reason, String action) protected StringbuildGuardrailResultJson(boolean passed, String phase, String subType, String sourceKey, String guardrailKey, String reason, String action, String sanitizedContent) protected StringbuildGuardrailResultJson(boolean passed, String phase, String subType, String sourceKey, String guardrailKey, String reason, String action, String sanitizedContent, Double confidence) protected AgentInvocationResultbuildSkipRejectInvocationResult(AgentDefinitionModel.AgentOperation operation, GuardrailResult guardrailResult) Builds a syntheticAgentInvocationResultcontaining the skip/reject flag + reason variables when a guardrail blocks the LLM call.protected AgentInvocationResultResponsecreateBlockedResponse(List<AgentInvocationResultResponse.Exchange> guardrailExchanges, GuardrailResult guardrailResult, AgentDefinitionModel.AgentOperation operation) Creates a blocked response when an input guardrail prevents the LLM call.protected AgentInvocationResultResponsecreateBlockedResponseFromExisting(AgentInvocationResultResponse existingResponse, List<AgentInvocationResultResponse.Exchange> outputGuardrailExchanges, GuardrailResult guardrailResult, AgentDefinitionModel.AgentOperation operation) Creates a blocked response when an output guardrail rejects the LLM response.protected AgentInvocationResultResponse.ExchangecreateGuardrailExchange(String subType, String inputContent, String outputContent, Instant startTime, Instant endTime) Creates anAgentInvocationResultResponse.Exchangeof typeguardrailwith the given subType, containing an input part (the content evaluated) and an output part (the result JSON).protected StringdetermineConstraintFailureMode(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result, boolean isInput) extractOutputParameters(AgentDefinitionModel.AgentOperation operation, AgentInvocationResult invocationResult) protected static AgentDefinitionModel.AgentOperationfindOperation(AgentDefinitionModel model, String operationKey) protected StringformatViolations(List<GuardrailViolation> violations) protected ServiceRegistryRuntimeServiceprotected GuardrailResulthandleConstraintFailure(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result, boolean isInput, List<AgentInvocationResultResponse.Exchange> guardrailExchanges, String content, Instant startTime, Instant endTime) protected GuardrailResulthandleGuardrailFailure(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, String reason, String phase, String sourceLabel, String sourceId, String guardrailKey, String sourceType, List<AgentInvocationResultResponse.Exchange> guardrailExchanges, String guardrailType, String content, Instant startTime, Instant endTime, String sanitizedContent, Double confidence) invoke(AgentInvocationContext agentInvocationContext) protected booleanisOverrideDefined(String serviceKey, Map<?, ?> entryMap, String operationKey, String applyTo) protected voidprependExchanges(AgentInvocationResultResponse.Invocation invocation, List<AgentInvocationResultResponse.Exchange> exchangesToPrepend) protected StringresolveConstraintErrorCode(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result) protected StringresolveFailureMode(String onFailure, AgentDefinitionModel.GuardrailDefaults defaults, boolean isInput) resolveMockOverride(AgentInvocationContext context, String serviceKey, AgentDefinitionModel.GuardrailReference ref) voidsetAgentEngineConfiguration(AgentEngineConfiguration agentEngineConfiguration) voidsetExternalGuardrailInvokerResolver(ExternalGuardrailInvokerResolver externalGuardrailInvokerResolver) voidsetGuardrailAgentEvaluator(GuardrailAgentEvaluator guardrailAgentEvaluator) protected booleanshouldApplyGuardrail(String applyTo, String phase) protected GuardrailResultvalidateDelegateExpressionGuardrail(AgentDefinitionModel.GuardrailReference guardrailReference, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, Map<String, Object> parsedOutputParameters, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) protected GuardrailResultvalidateGuardrailAgent(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) protected GuardrailResultvalidateOperationGuardrails(AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, Map<String, Object> parsedOutputParameters, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) Validates guardrails set on the operation in their configured order.protected GuardrailResultvalidateOutputParameterConstraints(AgentDefinitionModel.AgentOperation operation, Map<String, Object> parsedOutputParameters, AgentInvocationContext context, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) protected GuardrailResultvalidateWithServiceRegistry(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges)
-
Field Details
-
SOURCE_TYPE_PARAMETER
- See Also:
-
SOURCE_TYPE_EXPRESSION
- See Also:
-
SOURCE_TYPE_SERVICE
- See Also:
-
delegate
-
objectMapper
protected final tools.jackson.databind.ObjectMapper objectMapper -
clock
protected final org.flowable.common.engine.impl.runtime.Clock clock -
guardrailAgentEvaluator
-
externalGuardrailInvokerResolver
-
agentEngineConfiguration
-
-
Constructor Details
-
GuardrailValidatingAiModelInvoker
public GuardrailValidatingAiModelInvoker(AiModelInvoker delegate, tools.jackson.databind.ObjectMapper objectMapper, org.flowable.common.engine.impl.runtime.Clock clock)
-
-
Method Details
-
invoke
- Specified by:
invokein interfaceAiModelInvoker
-
createBlockedResponse
protected AgentInvocationResultResponse createBlockedResponse(List<AgentInvocationResultResponse.Exchange> guardrailExchanges, GuardrailResult guardrailResult, AgentDefinitionModel.AgentOperation operation) Creates a blocked response when an input guardrail prevents the LLM call. The response contains the guardrail exchanges plus an assistant exchange indicating the input was skipped or the output was rejected. -
createBlockedResponseFromExisting
protected AgentInvocationResultResponse createBlockedResponseFromExisting(AgentInvocationResultResponse existingResponse, List<AgentInvocationResultResponse.Exchange> outputGuardrailExchanges, GuardrailResult guardrailResult, AgentDefinitionModel.AgentOperation operation) Creates a blocked response when an output guardrail rejects the LLM response. Preserves the existing exchanges from the delegate response and appends the output guardrail exchanges plus an assistant exchange indicating the output was rejected. -
buildSkipRejectInvocationResult
protected AgentInvocationResult buildSkipRejectInvocationResult(AgentDefinitionModel.AgentOperation operation, GuardrailResult guardrailResult) Builds a syntheticAgentInvocationResultcontaining the skip/reject flag + reason variables when a guardrail blocks the LLM call. The variable names are resolved fromoperation.guardrailDefaults(the per-rowskip*/reject*fields are surfaced only via the frontend model today; the backendAgentDefinitionModel.GuardrailReferencehas no equivalent getters yet so the defaults are authoritative). Returnsnullwhen no names are configured. -
addGuardrailBlockedExchange
protected void addGuardrailBlockedExchange(AgentInvocationResultResponse.Invocation invocation, GuardrailResult guardrailResult) -
prependExchanges
protected void prependExchanges(AgentInvocationResultResponse.Invocation invocation, List<AgentInvocationResultResponse.Exchange> exchangesToPrepend) -
extractOutputParameters
protected Map<String,Object> extractOutputParameters(AgentDefinitionModel.AgentOperation operation, AgentInvocationResult invocationResult) -
validateOutputParameterConstraints
protected GuardrailResult validateOutputParameterConstraints(AgentDefinitionModel.AgentOperation operation, Map<String, Object> parsedOutputParameters, AgentInvocationContext context, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) -
handleConstraintFailure
protected GuardrailResult handleConstraintFailure(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result, boolean isInput, List<AgentInvocationResultResponse.Exchange> guardrailExchanges, String content, Instant startTime, Instant endTime) -
resolveConstraintErrorCode
protected String resolveConstraintErrorCode(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result) -
determineConstraintFailureMode
protected String determineConstraintFailureMode(AgentDefinitionModel.AgentOperation operation, List<? extends AgentDefinitionModel.AgentParameter> parameters, GuardrailValidationResult result, boolean isInput) -
validateOperationGuardrails
protected GuardrailResult validateOperationGuardrails(AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, Map<String, Object> parsedOutputParameters, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) Validates guardrails set on the operation in their configured order. Returns a non-passed GuardrailResult if a skip/reject occurred, null otherwise. -
validateDelegateExpressionGuardrail
protected GuardrailResult validateDelegateExpressionGuardrail(AgentDefinitionModel.GuardrailReference guardrailReference, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, Map<String, Object> parsedOutputParameters, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) -
validateWithServiceRegistry
protected GuardrailResult validateWithServiceRegistry(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) -
validateGuardrailAgent
protected GuardrailResult validateGuardrailAgent(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, AgentInvocationContext context, String phase, String outputContent, List<AgentInvocationResultResponse.Exchange> guardrailExchanges) -
handleGuardrailFailure
protected GuardrailResult handleGuardrailFailure(AgentDefinitionModel.GuardrailReference ref, AgentDefinitionModel.AgentOperation operation, String reason, String phase, String sourceLabel, String sourceId, String guardrailKey, String sourceType, List<AgentInvocationResultResponse.Exchange> guardrailExchanges, String guardrailType, String content, Instant startTime, Instant endTime, String sanitizedContent, Double confidence) -
resolveFailureMode
protected String resolveFailureMode(String onFailure, AgentDefinitionModel.GuardrailDefaults defaults, boolean isInput) -
shouldApplyGuardrail
-
findOperation
protected static AgentDefinitionModel.AgentOperation findOperation(AgentDefinitionModel model, String operationKey) -
formatViolations
-
buildConstraintResultJson
-
buildGuardrailResultJson
-
buildGuardrailResultJson
-
buildGuardrailResultJson
-
createGuardrailExchange
protected AgentInvocationResultResponse.Exchange createGuardrailExchange(String subType, String inputContent, String outputContent, Instant startTime, Instant endTime) Creates anAgentInvocationResultResponse.Exchangeof typeguardrailwith the given subType, containing an input part (the content evaluated) and an output part (the result JSON). -
setGuardrailAgentEvaluator
-
setExternalGuardrailInvokerResolver
public void setExternalGuardrailInvokerResolver(ExternalGuardrailInvokerResolver externalGuardrailInvokerResolver) -
setAgentEngineConfiguration
-
getServiceRegistryRuntimeService
-
buildGuardrailAssertionKey
protected static String buildGuardrailAssertionKey(String serviceKey, String operationKey, String applyTo) Composite identifier<serviceKey>/<operationKey>/<applyTo>written into the guardrail-snapshot JSON'sguardrailKeyfield so test assertions can target service-guardrail outcomes viaguardrail.<composite>.<field>. Two service-type guardrail entries on the same agent targeting the same service+operation but different phases (input vs output) get distinct composites. ReturnsnullwhenserviceKeyis missing.Used only for the assertion DSL — mock-override matching uses the entry tuple carried in the
__flowableMockOverridesinput parameter instead. -
resolveMockOverride
protected Map<String,Object> resolveMockOverride(AgentInvocationContext context, String serviceKey, AgentDefinitionModel.GuardrailReference ref) -
isOverrideDefined
-