App-Raider

 view release on metacpan or  search on metacpan

lib/App/Raider/Plugin/Situation.pm  view on Meta::CPAN

package App::Raider::Plugin::Situation;
our $VERSION = '0.003';
# ABSTRACT: Inject situational context (current time, timezone, host, user) at the start of the first raid

use Moose;
use Future::AsyncAwait;
use POSIX qw( strftime );
use Sys::Hostname ();

extends 'Langertha::Plugin';


has _injected => (
  is      => 'rw',
  isa     => 'Bool',
  default => 0,
);

sub _situation_text {
  my $now = time;
  my $local  = strftime('%Y-%m-%d %H:%M', localtime($now));
  my $offset = strftime('%z',            localtime($now));
  my $tzname = strftime('%Z',            localtime($now)) || 'local';
  my $host   = Sys::Hostname::hostname();
  my $user   = $ENV{USER} // $ENV{LOGNAME} // getpwuid($<) // 'unknown';
  return "[situation] $local $tzname (UTC$offset), host=$host user=$user\n\n";
}

async sub plugin_before_raid {
  my ($self, $messages) = @_;
  return $messages if $self->_injected;
  $self->_injected(1);

  my @msgs = @$messages;
  return \@msgs unless @msgs;

  my $prefix = _situation_text();

  # Prepend to the first user-visible message. Messages may be plain strings
  # or hashrefs with content; handle both.
  my $first = $msgs[0];
  if (!ref $first) {
    $msgs[0] = $prefix . $first;
  }
  elsif (ref $first eq 'HASH') {
    my %copy = %$first;
    if (defined $copy{content} && !ref $copy{content}) {
      $copy{content} = $prefix . $copy{content};
    }
    else {
      # Structured content — unshift a simple text note in front.
      unshift @msgs, { role => 'user', content => $prefix };
      return \@msgs;
    }
    $msgs[0] = \%copy;
  }
  return \@msgs;
}

__PACKAGE__->meta->make_immutable;

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Raider::Plugin::Situation - Inject situational context (current time, timezone, host, user) at the start of the first raid

=head1 VERSION

version 0.003

=head1 SYNOPSIS

    my $raider = Langertha::Raider->new(
        engine  => $engine,
        plugins => ['+App::Raider::Plugin::Situation'],
    );

=head1 DESCRIPTION



( run in 2.866 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )