App-Greple-xlate

 view release on metacpan or  search on metacpan

lib/App/Greple/xlate.pm  view on Meta::CPAN

        [ source => change_span($old_source, $new_source) ],
        [ target => change_span($old_translation, $new_translation) ],
    ) {
        my($label, $offset, $old, $new) = @$change;
        printf STDERR "  %s \@%d: %s -> %s\n",
            $label, $offset, review_quote($old), review_quote($new);
    }
}

sub cache_update {
    binmode STDERR, ':encoding(utf8)';

    my $region = ref $_[0] eq 'HASH' ? shift : { texts => [ @_ ] };
    my @from = @{$region->{texts}};
    my @pristine = @from;
    my $context = $region->{context};

    if ($context) {
        my $refs = @{$context->{hits_before} // []}
                 + @{$context->{hits_after} // []};
        my $olds = @{$context->{old_pairs} // []};

t/bin/llm  view on Meta::CPAN

##   badjson - return non-JSON text
##   swap    - swap the first two {{ ... }} expressions in each element
##             (mimics legitimate word-order changes in translation)
## LLM_STUB_LOG: append {"argv":[...],"stdin":"..."} JSON line per call
##

use strict;
use warnings;
use JSON::PP;

binmode STDOUT, ':encoding(utf8)';
binmode STDERR, ':encoding(utf8)';

my $mode = $ENV{LLM_STUB_MODE} // 'ok';

# "llm models" subcommand (used by the failure diagnosis)
if (@ARGV and $ARGV[0] eq 'models') {
    if ($mode eq 'nomodel') {
        print "OpenAI Chat: gpt-4o-mini\n";
    } else {
        print "OpenAI Responses: gpt-5.6-terra\n";
        print "OpenAI Chat: test-model\n";
    }
    exit 0;
}

my $input = do { binmode STDIN, ':encoding(utf8)'; local $/; <STDIN> } // '';

if (my $log = $ENV{LLM_STUB_LOG}) {
    open my $fh, '>>:encoding(utf8)', $log or die "$log: $!\n";
    print $fh JSON::PP->new->canonical->encode({ argv => \@ARGV, stdin => $input }), "\n";
    close $fh;
}

if ($mode eq 'fail') {
    print STDERR "stub llm: simulated failure\n";
    exit 1;

t/runner/Runner.pm  view on Meta::CPAN

use IO::File;

sub run {
    my $obj = shift;
    use IO::File;
    my $pid = (my $fh = new IO::File)->open('-|') // die "open: $@\n";
    if ($pid == 0) {
	if (my $stdin = $obj->{STDIN}) {
	    open STDIN, "<&=", $stdin->fileno or die "open: $!\n";
	    $stdin->fcntl(F_SETFD, 0) or die "fcntl F_SETFD: $!\n";
	    binmode STDIN, ':encoding(utf8)';
	}
	open STDERR, ">&STDOUT";
	$obj->execute;
	exit 1;
    }
    $obj->{STDIN} = undef;
    binmode $fh, ':encoding(utf8)';
    $obj->{stdout} = do { local $/; <$fh> };
    my $child = wait;
    $child != $pid and die "child = $child, pid = $pid";
    $obj->{pid} = $pid;
    $obj->{result} = $?;
    $obj;
}

sub status {
    my $obj = shift;

t/runner/Runner.pm  view on Meta::CPAN

}

*result = \&stdout; # for backward compatibility

sub setstdin {
    my $obj = shift;
    my $data = shift;
    my $stdin = $obj->{STDIN} //= do {
	my $fh = new_tmpfile IO::File or die "new_tmpfile: $!\n";
	$fh->fcntl(F_SETFD, 0) or die "fcntl F_SETFD: $!\n";
	binmode $fh, ':encoding(utf8)';
	$fh;
    };
    $stdin->seek(0, 0)  or die "seek: $!\n";
    $stdin->truncate(0) or die "truncate: $!\n";
    $stdin->print($data);
    $stdin->seek(0, 0)  or die "seek: $!\n";
    $obj;
}

##



( run in 0.811 second using v1.01-cache-2.11-cpan-364913b4093 )