| 1 | package Praxo::Middleware; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Exporter 'import'; | ||
| 6 | use Praxo::Json (); | ||
| 7 | use Praxo::JsonSchema (); | ||
| 8 | use Praxo::Errors qw(praxo_error); | ||
| 9 | |||
| 10 | our @EXPORT_OK = qw( | ||
| 11 | PiiRedactor StructuredOutput CallLimit ToolSelection fallback_model | ||
| 12 | redact redact_value structured_apply | ||
| 13 | ); | ||
| 14 | |||
| 15 | package Praxo::Middleware::PiiRedactor; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ($class) = @_; | ||
| 19 | return bless { | ||
| 20 | rules => [ | ||
| 21 | [qr/[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}/, '[REDACTED:email]'], | ||
| 22 | [qr/\b\d{3}-\d{2}-\d{4}\b/, '[REDACTED:ssn]'], | ||
| 23 | [qr/\b(?:\d{4}[ -]?){3}\d{4}\b/, '[REDACTED:credit_card]'], | ||
| 24 | ], | ||
| 25 | }, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub redact { | ||
| 29 | my ($self, $text) = @_; | ||
| 30 | my $output = $text // ''; | ||
| 31 | for my $rule (@{ $self->{rules} }) { | ||
| 32 | my ($pattern, $replacement) = @$rule; | ||
| 33 | $output =~ s/$pattern/$replacement/g; | ||
| 34 | } | ||
| 35 | return $output; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub redact_value { | ||
| 39 | my ($self, $value) = @_; | ||
| 40 | return $self->redact($value) if defined($value) && !ref($value); | ||
| 41 | if (ref($value) eq 'HASH') { | ||
| 42 | return { map { $_ => $self->redact_value($value->{$_}) } keys %$value }; | ||
| 43 | } | ||
| 44 | if (ref($value) eq 'ARRAY') { | ||
| 45 | return [ map { $self->redact_value($_) } @$value ]; | ||
| 46 | } | ||
| 47 | return $value; | ||
| 48 | } | ||
| 49 | |||
| 50 | package Praxo::Middleware::StructuredOutput; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, %args) = @_; | ||
| 54 | return bless { | ||
| 55 | schema => $args{schema} // {}, | ||
| 56 | maxRepairs => $args{max_repairs} // 0, | ||
| 57 | repairer => $args{repairer}, | ||
| 58 | }, $class; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub apply { | ||
| 62 | my ($self, $text) = @_; | ||
| 63 | my $output = $text // ''; | ||
| 64 | for my $attempt (0 .. $self->{maxRepairs}) { | ||
| 65 | my $data = eval { _extract_json($output) }; | ||
| 66 | if (!$@ && !@{ Praxo::JsonSchema::validate($self->{schema}, $data) }) { | ||
| 67 | return ($output, undef); | ||
| 68 | } | ||
| 69 | last if $attempt == $self->{maxRepairs} && !$self->{repairer}; | ||
| 70 | last unless $self->{repairer}; | ||
| 71 | my $repaired = $self->{repairer}->($output); | ||
| 72 | last unless defined($repaired) && $repaired =~ /\S/; | ||
| 73 | $output = $repaired; | ||
| 74 | } | ||
| 75 | return (undef, 'AGENT_STRUCTURED_OUTPUT'); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _extract_json { | ||
| 79 | my ($text) = @_; | ||
| 80 | my $value = $text // ''; | ||
| 81 | $value =~ s/^\s+|\s+$//g; | ||
| 82 | if ($value =~ /^```/) { | ||
| 83 | $value =~ s/^```[^\n]*\n//; | ||
| 84 | $value =~ s/^`+//; | ||
| 85 | $value =~ s/`+\s*$//; | ||
| 86 | $value =~ s/^\s+|\s+$//g; | ||
| 87 | } | ||
| 88 | my $start = index($value, '{'); | ||
| 89 | my $array = index($value, '['); | ||
| 90 | $start = $array if $array >= 0 && ($start < 0 || $array < $start); | ||
| 91 | Praxo::Errors::praxo_error('AGENT_STRUCTURED_OUTPUT', 'output does not contain JSON') if $start < 0; | ||
| 92 | return Praxo::Json::parse(substr($value, $start)); | ||
| 93 | } | ||
| 94 | |||
| 95 | package Praxo::Middleware::CallLimit; | ||
| 96 | |||
| 97 | sub new { | ||
| 98 | my ($class, %args) = @_; | ||
| 99 | return bless { | ||
| 100 | modelLimit => $args{model_limit} // 0, | ||
| 101 | toolLimit => $args{tool_limit} // 0, | ||
| 102 | models => 0, | ||
| 103 | tools => 0, | ||
| 104 | }, $class; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub before_model { | ||
| 108 | my ($self) = @_; | ||
| 109 | $self->{models}++; | ||
| 110 | Praxo::Errors::praxo_error('AGENT_MODEL_CALL_LIMIT', 'model call limit') | ||
| 111 | if $self->{modelLimit} > 0 && $self->{models} > $self->{modelLimit}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub before_tool { | ||
| 115 | my ($self) = @_; | ||
| 116 | $self->{tools}++; | ||
| 117 | Praxo::Errors::praxo_error('AGENT_TOOL_CALL_LIMIT', 'tool call limit') | ||
| 118 | if $self->{toolLimit} > 0 && $self->{tools} > $self->{toolLimit}; | ||
| 119 | } | ||
| 120 | |||
| 121 | package Praxo::Middleware::ToolSelection; | ||
| 122 | |||
| 123 | sub new { | ||
| 124 | my ($class, $selected) = @_; | ||
| 125 | return bless { selected => { map { $_ => 1 } @{ $selected // [] } } }, $class; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub filter { | ||
| 129 | my ($self, $tools) = @_; | ||
| 130 | return [ grep { $self->{selected}{ $_->{name} } } @$tools ]; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub reject { | ||
| 134 | my ($self, $name) = @_; | ||
| 135 | Praxo::Errors::praxo_error('AGENT_TOOL_NOT_SELECTED', 'tool not selected') | ||
| 136 | unless $self->{selected}{$name}; | ||
| 137 | } | ||
| 138 | |||
| 139 | package Praxo::Middleware; | ||
| 140 | |||
| 141 | sub redact { | ||
| 142 | my ($text) = @_; | ||
| 143 | return Praxo::Middleware::PiiRedactor->new->redact($text); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub redact_value { | ||
| 147 | my ($value) = @_; | ||
| 148 | return Praxo::Middleware::PiiRedactor->new->redact_value($value); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub structured_apply { | ||
| 152 | my ($schema, $text, $max_repairs, $repairer) = @_; | ||
| 153 | my $output = Praxo::Middleware::StructuredOutput->new( | ||
| 154 | schema => $schema, | ||
| 155 | max_repairs => $max_repairs // 0, | ||
| 156 | repairer => $repairer, | ||
| 157 | ); | ||
| 158 | return $output->apply($text); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub fallback_model { | ||
| 162 | my ($fallback) = @_; | ||
| 163 | return sub { | ||
| 164 | my ($next) = @_; | ||
| 165 | return sub { | ||
| 166 | my ($request) = @_; | ||
| 167 | my $response = eval { $next->($request) }; | ||
| 168 | return $response unless $@; | ||
| 169 | my $error = $@; | ||
| 170 | die $error unless $fallback; | ||
| 171 | return $fallback->($request); | ||
| 172 | }; | ||
| 173 | }; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; |