| 1 | package Praxo::Toolcall; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Exporter 'import'; | ||
| 6 | use Praxo::Json (); | ||
| 7 | |||
| 8 | our @EXPORT_OK = qw(parse_toolcall strip_markup parse_input_json); | ||
| 9 | |||
| 10 | sub parse_toolcall { | ||
| 11 | my ($content) = @_; | ||
| 12 | my $call = _parse_json_call($content) | ||
| 13 | // _parse_xml($content) | ||
| 14 | // _parse_tag($content) | ||
| 15 | // _parse_plain($content); | ||
| 16 | return undef unless $call; | ||
| 17 | $call->{callId} = 'tc-' . int(time() * 1000); | ||
| 18 | return $call; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub strip_markup { | ||
| 22 | my ($content) = @_; | ||
| 23 | my $output = $content; | ||
| 24 | $output =~ s/<tool_call\s+name="[^"]+"\s*>.*?(?:<\/tool_call>|$)//sg; | ||
| 25 | $output =~ s/<tool_call_invoke>.*?(?:<\/tool_call_invoke>|$)//sg; | ||
| 26 | $output =~ s/<[a-zA-Z][\w-]*(?:\.[\w-]+)+>\s*\{.*?(?:<\/[a-zA-Z][\w.-]*>|$)//sg; | ||
| 27 | $output =~ s/\n{3,}/\n\n/g; | ||
| 28 | $output =~ s/^\s+|\s+$//g; | ||
| 29 | return $output; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub parse_input_json { | ||
| 33 | my ($raw) = @_; | ||
| 34 | my $trimmed = $raw; | ||
| 35 | $trimmed =~ s/^\s+|\s+$//g; | ||
| 36 | my $value = eval { Praxo::Json::parse($trimmed) }; | ||
| 37 | return $value if ref($value) eq 'HASH'; | ||
| 38 | (my $escaped = $raw) =~ s/\\/\\\\/g; | ||
| 39 | $escaped =~ s/^\s+|\s+$//g; | ||
| 40 | $value = eval { Praxo::Json::parse($escaped) }; | ||
| 41 | return ref($value) eq 'HASH' ? $value : undef; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _parse_json_call { | ||
| 45 | my ($content) = @_; | ||
| 46 | my $value = eval { Praxo::Json::parse($content) }; | ||
| 47 | return undef unless ref($value) eq 'HASH'; | ||
| 48 | my $name = $value->{name}; | ||
| 49 | return undef unless defined($name) && !ref($name) && length($name); | ||
| 50 | return { | ||
| 51 | name => $name, | ||
| 52 | input => ref($value->{input}) eq 'HASH' ? $value->{input} : {}, | ||
| 53 | operation => $value->{operation} // '', | ||
| 54 | resource => $value->{resource} // '', | ||
| 55 | risk => $value->{risk} // '', | ||
| 56 | reason => $value->{reason} // '', | ||
| 57 | idempotency_key => $value->{idempotency_key} // '', | ||
| 58 | }; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _parse_xml { | ||
| 62 | my ($content) = @_; | ||
| 63 | return undef unless $content =~ /<tool_call\s+name="([^"]+)"\s*>\s*(.*?)\s*<\/tool_call>/s; | ||
| 64 | my ($name, $raw) = ($1, $2); | ||
| 65 | my $input = parse_input_json($raw); | ||
| 66 | return undef unless $input; | ||
| 67 | return { name => $name, input => $input, operation => '', resource => '', | ||
| 68 | risk => '', reason => '', idempotency_key => '' }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _parse_tag { | ||
| 72 | my ($content) = @_; | ||
| 73 | return undef unless $content =~ /<([a-zA-Z][\w-]*(?:\.[\w-]+)+)>\s*(\{.*?)\s*(?:<\/[a-zA-Z][\w.-]*>|$)/s; | ||
| 74 | my ($name, $raw) = ($1, $2); | ||
| 75 | my $input = parse_input_json($raw); | ||
| 76 | return undef unless $input; | ||
| 77 | return { name => $name, input => $input, operation => '', resource => '', | ||
| 78 | risk => '', reason => '', idempotency_key => '' }; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _parse_plain { | ||
| 82 | my ($content) = @_; | ||
| 83 | return undef unless $content =~ /^\s*([a-zA-Z][\w.-]*)\s+(\{.*\})\s*$/m; | ||
| 84 | my ($name, $raw) = ($1, $2); | ||
| 85 | return undef unless index($name, '.') >= 0; | ||
| 86 | my $input = parse_input_json($raw); | ||
| 87 | return undef unless $input; | ||
| 88 | return { name => $name, input => $input, operation => '', resource => '', | ||
| 89 | risk => '', reason => '', idempotency_key => '' }; | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; |