| 1 | package Langertha::Raider::Plugin::Situation; | ||||||||||||||||||||||||||||||||||||||
| 2 | 20 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Inject situational context (current time, timezone, host, user) at the start of the first raid | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use POSIX qw( strftime ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 20 | ▶ | use Sys::Hostname (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 20 | extends 'Langertha::Plugin'; | |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 20 | has _injected => ( | |||||||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 16 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||
| 17 | default => 0, | ||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | 67 | ▶ | sub _situation_text { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_situation_text: called CC=5 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||
| 21 | 67 | my $now = time; | |||||||||||||||||||||||||||||||||||||
| 22 | 67 | my $local = strftime('%Y-%m-%d %H:%M', localtime($now)); | |||||||||||||||||||||||||||||||||||||
| 23 | 67 | my $offset = strftime('%z', localtime($now)); | |||||||||||||||||||||||||||||||||||||
| 24 | 67 | ▶ | my $tzname = strftime('%Z', localtime($now)) || 'local'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
strftime('%Z', localtime $now) || 'local'
MC/DC0%0/1
strftime('%Z', localtime $now) || 'local'
strftime('%Z', localtime $now)
Truth table50%1/2
A
A= strftime('%Z', localtime $now)
| |||||||||||||||||||||||||||||||||||||||
| 25 | 67 | my $host = Sys::Hostname::hostname(); | |||||||||||||||||||||||||||||||||||||
| 26 | 67 | ▶ | my $user = $ENV{USER} // $ENV{LOGNAME} // getpwuid($<) // 'unknown'; | ||||||||||||||||||||||||||||||||||||
|
Condition37%3/8
(($ENV{"USER"} // $ENV{"LOGNAME"}) // getpwuid $<) // "unknown"
($ENV{"USER"} // $ENV{"LOGNAME"}) // getpwuid $<
$ENV{"USER"} // $ENV{"LOGNAME"}
MC/DC0%0/3
(($ENV{"USER"} // $ENV{"LOGNAME"}) // getpwuid $<) // "unknown"
$ENV{"USER"}
$ENV{"LOGNAME"}
getpwuid $<
Truth table25%1/4
A // B // C
A= $ENV{"USER"}B= $ENV{"LOGNAME"}C= getpwuid $<
| |||||||||||||||||||||||||||||||||||||||
| 27 | 67 | return "[situation] $local $tzname (UTC$offset), host=$host user=$user\n\n"; | |||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | 74 | ▶ | async sub plugin_before_raid { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
plugin_before_raid: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 31 | 74 | my ($self, $messages) = @_; | |||||||||||||||||||||||||||||||||||||
| 32 | 74 | ▶ | return $messages if $self->_injected; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 33 | 66 | $self->_injected(1); | |||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | 66 | my @msgs = @$messages; | |||||||||||||||||||||||||||||||||||||
| 36 | 66 | ▶ | return \@msgs unless @msgs; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 66 | my $prefix = _situation_text(); | |||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | # Prepend to the first user-visible message. Messages may be plain strings | ||||||||||||||||||||||||||||||||||||||
| 41 | # or hashrefs with content; handle both. | ||||||||||||||||||||||||||||||||||||||
| 42 | 66 | my $first = $msgs[0]; | |||||||||||||||||||||||||||||||||||||
| 43 | 66 | ▶ | if (!ref $first) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 44 | 64 | $msgs[0] = $prefix . $first; | |||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||
| 46 | elsif (ref $first eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||
| 47 | 2 | my %copy = %$first; | |||||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if (defined $copy{content} && !ref $copy{content}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $copy{"content"} and not ref $copy{"content"}
MC/DC50%1/2
defined $copy{"content"} and not ref $copy{"content"}
defined $copy{"content"}
not ref $copy{"content"}
Truth table66%2/3
A and not(B)
A= defined $copy{"content"}B= not ref $copy{"content"}
| |||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $copy{content} = $prefix . $copy{content}; | |||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||||||||||||
| 52 | # Structured content — unshift a simple text note in front. | ||||||||||||||||||||||||||||||||||||||
| 53 | 1 | unshift @msgs, { role => 'user', content => $prefix }; | |||||||||||||||||||||||||||||||||||||
| 54 | 1 | return \@msgs; | |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $msgs[0] = \%copy; | |||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||
| 58 | 65 | return \@msgs; | |||||||||||||||||||||||||||||||||||||
| 59 | 20 | } | |||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | 20 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | 20 | 1; | |||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | =pod | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | Langertha::Raider::Plugin::Situation - Inject situational context (current time, timezone, host, user) at the start of the first raid | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | version 0.503 | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | my $raider = Langertha::Raider->new( | ||||||||||||||||||||||||||||||||||||||
| 82 | engine => $engine, | ||||||||||||||||||||||||||||||||||||||
| 83 | plugins => ['+Langertha::Raider::Plugin::Situation'], | ||||||||||||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | Prepends a small C<[situation]> context block to the very first user message | ||||||||||||||||||||||||||||||||||||||
| 89 | of a session: current local time, UTC offset, timezone, hostname, and | ||||||||||||||||||||||||||||||||||||||
| 90 | the user (C<$USER>, else C<$LOGNAME>, else the password database). The model then knows what "now" means without needing to call | ||||||||||||||||||||||||||||||||||||||
| 91 | C<bash "date">. | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | The block is injected only once per Raider instance (on the first | ||||||||||||||||||||||||||||||||||||||
| 94 | C<plugin_before_raid>) so it does not bloat subsequent raids — the model | ||||||||||||||||||||||||||||||||||||||
| 95 | already has the info in its history. | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | =over | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | =item * L<Langertha::Raider::CLI> | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =item * L<Langertha::Plugin> | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | =back | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||
| 112 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||
| 114 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 131 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||||||||||||