Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Structured/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function aggregateToolResults(): array
return $this->steps
->flatMap(fn (Step $step): array => $step->toolResults)
->values()
->toArray();
->all();
}

protected function calculateTotalUsage(): Usage
Expand Down
138 changes: 138 additions & 0 deletions tests/Structured/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Prism\Prism\Structured\ResponseBuilder;
use Prism\Prism\Structured\Step;
use Prism\Prism\ValueObjects\Meta;
use Prism\Prism\ValueObjects\ToolCall;
use Prism\Prism\ValueObjects\ToolResult;
use Prism\Prism\ValueObjects\Usage;

test('throws a PrismStructuredDecodingException if the response is not valid json', function (): void {
Expand Down Expand Up @@ -89,3 +91,139 @@

expect($response->structured)->toBe(['value' => 42]);
});

test('StructuredResponseBuilder aggregates tool calls from multiple steps as ToolCall objects', function (): void {
$builder = new ResponseBuilder;

$toolCall1 = new ToolCall(
id: 'call_1',
name: 'get_weather',
arguments: '{"location":"NYC"}'
);

$toolCall2 = new ToolCall(
id: 'call_2',
name: 'get_temperature',
arguments: ['location' => 'LA']
);

$toolCall3 = new ToolCall(
id: 'call_3',
name: 'get_humidity',
arguments: '{"location":"SF"}'
);

$builder->addStep(new Step(
text: '',
finishReason: FinishReason::ToolCalls,
usage: new Usage(promptTokens: 10, completionTokens: 5),
meta: new Meta('step1', 'test-model'),
messages: [],
systemPrompts: [],
toolCalls: [$toolCall1, $toolCall2],
));

$builder->addStep(new Step(
text: '{"result":"success"}',
finishReason: FinishReason::Stop,
usage: new Usage(promptTokens: 5, completionTokens: 3),
meta: new Meta('step2', 'test-model'),
messages: [],
systemPrompts: [],
toolCalls: [$toolCall3],
));

$response = $builder->toResponse();

expect($response->toolCalls)->toHaveCount(3)
->and($response->toolCalls[0])->toBeInstanceOf(ToolCall::class)
->and($response->toolCalls[0]->id)->toBe('call_1')
->and($response->toolCalls[0]->name)->toBe('get_weather')
->and($response->toolCalls[1])->toBeInstanceOf(ToolCall::class)
->and($response->toolCalls[1]->id)->toBe('call_2')
->and($response->toolCalls[1]->name)->toBe('get_temperature')
->and($response->toolCalls[2])->toBeInstanceOf(ToolCall::class)
->and($response->toolCalls[2]->id)->toBe('call_3')
->and($response->toolCalls[2]->name)->toBe('get_humidity');
});

test('StructuredResponseBuilder aggregates tool results from multiple steps as ToolResult objects', function (): void {
$builder = new ResponseBuilder;

$toolResult1 = new ToolResult(
toolCallId: 'call_1',
toolName: 'get_weather',
args: ['location' => 'NYC'],
result: ['temperature' => 75, 'condition' => 'sunny']
);

$toolResult2 = new ToolResult(
toolCallId: 'call_2',
toolName: 'get_temperature',
args: ['location' => 'LA'],
result: 80
);

$toolResult3 = new ToolResult(
toolCallId: 'call_3',
toolName: 'get_humidity',
args: ['location' => 'SF'],
result: 'High'
);

$builder->addStep(new Step(
text: '',
finishReason: FinishReason::ToolCalls,
usage: new Usage(promptTokens: 10, completionTokens: 5),
meta: new Meta('step1', 'test-model'),
messages: [],
systemPrompts: [],
toolResults: [$toolResult1, $toolResult2],
));

$builder->addStep(new Step(
text: '{"result":"success"}',
finishReason: FinishReason::Stop,
usage: new Usage(promptTokens: 5, completionTokens: 3),
meta: new Meta('step2', 'test-model'),
messages: [],
systemPrompts: [],
toolResults: [$toolResult3],
));

$response = $builder->toResponse();

expect($response->toolResults)->toHaveCount(3)
->and($response->toolResults[0])->toBeInstanceOf(ToolResult::class)
->and($response->toolResults[0]->toolCallId)->toBe('call_1')
->and($response->toolResults[0]->toolName)->toBe('get_weather')
->and($response->toolResults[0]->result)->toBe(['temperature' => 75, 'condition' => 'sunny'])
->and($response->toolResults[1])->toBeInstanceOf(ToolResult::class)
->and($response->toolResults[1]->toolCallId)->toBe('call_2')
->and($response->toolResults[1]->toolName)->toBe('get_temperature')
->and($response->toolResults[1]->result)->toBe(80)
->and($response->toolResults[2])->toBeInstanceOf(ToolResult::class)
->and($response->toolResults[2]->toolCallId)->toBe('call_3')
->and($response->toolResults[2]->toolName)->toBe('get_humidity')
->and($response->toolResults[2]->result)->toBe('High');
});

test('StructuredResponseBuilder returns empty arrays when no tool calls or results exist', function (): void {
$builder = new ResponseBuilder;

$builder->addStep(new Step(
text: '{"value":42}',
finishReason: FinishReason::Stop,
usage: new Usage(promptTokens: 10, completionTokens: 5),
meta: new Meta('step1', 'test-model'),
messages: [],
systemPrompts: [],
));

$response = $builder->toResponse();

expect($response->toolCalls)->toBeArray()
->and($response->toolCalls)->toBeEmpty()
->and($response->toolResults)->toBeArray()
->and($response->toolResults)->toBeEmpty();
});