'assistant', '_meta' => ['provider' => 'test'], 'content' => [ ['type' => 'text', 'text' => 'I will check.'], ['type' => 'tool_use', 'id' => 'call-1', 'name' => 'weather', 'input' => ['city' => 'Paris']], ], ]); $user = SamplingMessage::fromArray([ 'role' => 'user', 'content' => [[ 'type' => 'tool_result', 'toolUseId' => 'call-1', 'content' => [['type' => 'text', 'text' => '21 C']], 'structuredContent' => ['temperature' => 21], ]], ]); $this->assertInstanceOf(ToolUseContent::class, $assistant->content[1]); $this->assertSame(['provider' => 'test'], $assistant->meta); $this->assertSame(['provider' => 'test'], $assistant->jsonSerialize()['_meta']); $this->assertInstanceOf(ToolResultContent::class, $user->content[0]); $this->assertSame(['temperature' => 21], $user->content[0]->structuredContent); $this->assertEquals($assistant, SamplingMessage::fromArray(json_decode(json_encode($assistant), true))); $this->assertEquals($user, SamplingMessage::fromArray(json_decode(json_encode($user), true))); } public function testSingleContentBlockKeepsItsShape(): void { $message = SamplingMessage::fromArray(['role' => 'user', 'content' => ['type' => 'text', 'text' => 'hi']]); $this->assertInstanceOf(TextContent::class, $message->content); $this->assertSame('{"role":"user","content":{"type":"text","text":"hi"}}', json_encode($message)); $this->assertCount(1, $message->getContentBlocks()); } public function testSingleElementListKeepsItsShape(): void { $message = SamplingMessage::fromArray(['role' => 'user', 'content' => [['type' => 'text', 'text' => 'hi']]]); $this->assertIsArray($message->content); $this->assertSame('{"role":"user","content":[{"type":"text","text":"hi"}]}', json_encode($message)); $this->assertCount(1, $message->getContentBlocks()); } /** * The tool-flow rules span the whole message list, so a single message is never * rejected for carrying the "wrong" block — see CreateSamplingMessageRequestTest. */ public function testToolBlocksAreAcceptedRegardlessOfRole(): void { $message = new SamplingMessage(Role::User, new ToolUseContent('call-1', 'weather', [])); $this->assertInstanceOf(ToolUseContent::class, $message->content); } public function testFilteredContentStillSerializesAsAnArray(): void { $blocks = [new TextContent('thinking'), new ToolUseContent('call-1', 'weather', [])]; // array_filter() preserves keys, so this list starts at index 1. $toolUses = array_filter($blocks, static fn ($block): bool => $block instanceof ToolUseContent); $message = new SamplingMessage(Role::Assistant, $toolUses); $this->assertSame( '{"role":"assistant","content":[{"type":"tool_use","id":"call-1","name":"weather","input":{}}]}', json_encode($message), ); } public function testEmptyContentListIsRejected(): void { $this->expectException(InvalidArgumentException::class); new SamplingMessage(Role::User, []); } public function testEmptyContentListIsRejectedWhenHydrating(): void { $this->expectException(InvalidArgumentException::class); SamplingMessage::fromArray(['role' => 'user', 'content' => []]); } public function testUnknownContentTypeIsRejected(): void { $this->expectException(InvalidArgumentException::class); SamplingMessage::fromArray(['role' => 'user', 'content' => ['type' => 'nope']]); } public function testUnknownRoleIsRejected(): void { $this->expectException(InvalidArgumentException::class); /* @phpstan-ignore argument.type */ SamplingMessage::fromArray(['role' => 'system', 'content' => ['type' => 'text', 'text' => 'hi']]); } }