Claude-Agent

 view release on metacpan or  search on metacpan

lib/Claude/Agent/Hook/Result.pm  view on Meta::CPAN


sub proceed {
    return {};
}

sub allow {
    my ($class, %args) = @_;
    return _permission('allow', %args);
}

sub deny {
    my ($class, %args) = @_;
    return _permission('deny', %args);
}

sub ask {
    my ($class, %args) = @_;
    return _permission('ask', %args);
}

sub defer {
    my ($class, %args) = @_;
    return _permission('defer', %args);
}

sub _permission {
    my ($decision, %args) = @_;
    my %specific = (
        hookEventName      => $args{event} // 'PreToolUse',
        permissionDecision => $decision,
    );
    $specific{permissionDecisionReason} = $args{reason} if defined $args{reason};
    $specific{updatedInput} = $args{updated_input} if defined $args{updated_input};
    return { hookSpecificOutput => \%specific };
}

sub block {
    my ($class, %args) = @_;
    my $result = { decision => 'block' };
    $result->{reason} = $args{reason} if defined $args{reason};
    return $result;
}

sub stop {
    my ($class, %args) = @_;
    my $result = { continue => \0 };
    $result->{stopReason} = $args{reason} if defined $args{reason};
    return $result;
}

sub add_context {
    my ($class, $text, %args) = @_;
    return {
        hookSpecificOutput => {
            hookEventName     => $args{event} // 'PreToolUse',
            additionalContext => $text,
        },
    };
}

sub update_tool_output {
    my ($class, $output, %args) = @_;
    return {
        hookSpecificOutput => {
            hookEventName     => $args{event} // 'PostToolUse',
            updatedToolOutput => $output,
        },
    };
}

sub system_message {
    my ($class, $text) = @_;
    return { systemMessage => $text };
}

sub suppress_output {
    return { suppressOutput => \1 };
}

sub async {
    my ($class, %args) = @_;
    my $result = { async => \1 };
    $result->{asyncTimeout} = $args{timeout} if defined $args{timeout};
    return $result;
}

sub merge {
    my ($class, @results) = @_;
    my %merged;
    for my $result (@results) {
        next unless ref $result eq 'HASH';
        for my $key (keys %$result) {
            if ($key eq 'hookSpecificOutput' && ref $merged{$key} eq 'HASH' && ref $result->{$key} eq 'HASH') {
                $merged{$key} = { %{$merged{$key}}, %{$result->{$key}} };
            }
            else {
                $merged{$key} = $result->{$key};
            }
        }
    }
    return \%merged;
}

1;

__END__

=head1 NAME

Claude::Agent::Hook::Result - Build hook callback results

=head1 SYNOPSIS

    use Claude::Agent::Hook::Result;

    return Claude::Agent::Hook::Result->proceed;

    return Claude::Agent::Hook::Result->allow(
        updated_input => { command => 'safe command' },
        reason        => 'Modified for safety',
    );



( run in 0.793 second using v1.01-cache-2.11-cpan-145b269f2f3 )