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
8 changes: 7 additions & 1 deletion src/Capability/Formatter/PromptResultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ public function format(mixed $promptGenerationResult): array
if ($item instanceof PromptMessage) {
$result[] = $item;
} else {
$result = array_merge($result, $this->format($item));
foreach ($this->format($item) as $key => $message) {
if (\is_int($key)) {
$result[] = $message;
} else {
$result[$key] = $message;
}
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Capability/Formatter/PromptResultFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@

class PromptResultFormatterTest extends TestCase
{
public function testMixedMessagesPreserveNestedKeysAndOrder(): void
{
$first = new PromptMessage(Role::User, new TextContent('first'));
$second = new PromptMessage(Role::Assistant, new TextContent('second'));
$third = new PromptMessage(Role::User, new TextContent('third'));

$result = (new PromptResultFormatter())->format([
$first,
['named' => $second, 8 => $first],
['named' => $third, 5 => $second],
]);

$this->assertSame([0, 'named', 1, 2], array_keys($result));
$this->assertSame($first, $result[0]);
$this->assertSame($third, $result['named']);
$this->assertSame($first, $result[1]);
$this->assertSame($second, $result[2]);
}

public function testFormatPromptMessage(): void
{
$message = new PromptMessage(Role::User, new TextContent('hello'));
Expand Down
Loading