Langertha

 view release on metacpan or  search on metacpan

t/43_stream_tool_calls.t  view on Meta::CPAN


subtest 'OpenAI: parallel calls, fragments interleaved by index' => sub {
  my $engine = Langertha::Engine::OpenAI->new( api_key => 'k', model => 'gpt-4o-mini' );
  my $c = sub { +{ id => 'chatcmpl-1', model => 'gpt-4o-mini', choices => [ { index => 0, @_ } ] } };
  my $body = sse(
    $c->( delta => { role => 'assistant', tool_calls => [
      { index => 0, id => 'call_a', type => 'function', function => { name => 'get_weather', arguments => '' } } ] } ),
    $c->( delta => { tool_calls => [ { index => 0, function => { arguments => '{"city":' } } ] } ),
    $c->( delta => { tool_calls => [
      { index => 1, id => 'call_b', type => 'function', function => { name => 'get_time', arguments => '' } } ] } ),
    $c->( delta => { tool_calls => [ { index => 1, function => { arguments => '{"tz":"CET"}' } } ] } ),
    $c->( delta => { tool_calls => [ { index => 0, function => { arguments => '"Paris"}' } } ] } ),
    $c->( delta => {}, finish_reason => 'tool_calls' ),
  );
  my $reply = reply_calls( $engine, { id => 'chatcmpl-1', model => 'gpt-4o-mini', choices => [ {
    index => 0, finish_reason => 'tool_calls', message => { role => 'assistant', content => undef, tool_calls => [
      { id => 'call_a', type => 'function', function => { name => 'get_weather', arguments => '{"city":"Paris"}' } },
      { id => 'call_b', type => 'function', function => { name => 'get_time',    arguments => '{"tz":"CET"}' } },
    ] } } ] } );

  for my $path ( 'sync', 'buffer' ) {
    my $chunks;
    if ( $path eq 'sync' ) { $chunks = $engine->process_stream_data($body) }
    else {
      my $buffer = $body;
      $chunks = $engine->_process_stream_buffer( \$buffer, 'sse', 1, {} );
    }
    is_deeply( hashes( $engine->aggregate_tool_calls($chunks) ), $reply,

t/43_stream_tool_calls.t  view on Meta::CPAN

};

# ---------------------------------------------------------------------------
# Gemini: functionCall parts arrive whole, on the chunk that carries them.
# ---------------------------------------------------------------------------

subtest 'Gemini: functionCall parts' => sub {
  my $engine = Langertha::Engine::Gemini->new( api_key => 'k', model => 'gemini-3-flash-preview' );
  my @parts = (
    { functionCall => { name => 'get_weather', args => { city => 'Paris' } } },
    { functionCall => { name => 'get_time', args => { tz => 'CET' } } },
  );
  my $reply = reply_calls( $engine, { candidates => [ { content => { role => 'model',
    parts => [ { text => 'Checking.' }, @parts ] }, finishReason => 'STOP' } ] } );
  my $chunks = $engine->process_stream_data( sse(
    { candidates => [ { content => { role => 'model', parts => [ { text => 'Checking.' } ] } } ] },
    { candidates => [ { content => { role => 'model', parts => [ @parts ] }, finishReason => 'STOP' } ],
      usageMetadata => { promptTokenCount => 5, candidatesTokenCount => 9, totalTokenCount => 14 } },
  ) );
  is_deeply( hashes( $engine->aggregate_tool_calls($chunks) ), $reply,
    'both calls equal the non-streaming reply' );



( run in 0.804 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )