Skip to content
Open
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
25 changes: 21 additions & 4 deletions src/Providers/OpenRouter/Maps/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ protected function mapMessage(Message $message): void
protected function mapSystemMessage(SystemMessage $message): void
{
$cacheType = $message->providerOptions('cacheType');
// OpenRouter supports extended cache TTL (e.g. '1h') via cacheTtl provider option
$cacheTtl = $message->providerOptions('cacheTtl');

// OpenRouter supports cache_control in content array format (same as Anthropic)
if ($cacheType) {
Expand All @@ -73,7 +75,10 @@ protected function mapSystemMessage(SystemMessage $message): void
[
'type' => 'text',
'text' => $message->content,
'cache_control' => ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType],
'cache_control' => array_filter([
'type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType,
'ttl' => $cacheTtl,
]),
],
],
];
Expand All @@ -88,7 +93,11 @@ protected function mapSystemMessage(SystemMessage $message): void
protected function mapToolResultMessage(ToolResultMessage $message): void
{
$cacheType = $message->providerOptions('cacheType');
$cacheControl = $cacheType ? ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType] : null;
$cacheTtl = $message->providerOptions('cacheTtl');
$cacheControl = $cacheType ? array_filter([
'type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType,
'ttl' => $cacheTtl,
]) : null;

$toolResults = $message->toolResults;
$totalResults = count($toolResults);
Expand Down Expand Up @@ -126,7 +135,11 @@ protected function mapToolResultMessage(ToolResultMessage $message): void
protected function mapUserMessage(UserMessage $message): void
{
$cacheType = $message->providerOptions('cacheType');
$cacheControl = $cacheType ? ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType] : null;
$cacheTtl = $message->providerOptions('cacheTtl');
$cacheControl = $cacheType ? array_filter([
'type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType,
'ttl' => $cacheTtl,
]) : null;

$imageParts = array_map(fn (Image $image): array => (new ImageMapper($image))->toPayload(), $message->images());
// NOTE: mirrored from Gemini's multimodal mapper so we stay consistent across providers.
Expand All @@ -153,6 +166,7 @@ protected function mapUserMessage(UserMessage $message): void
protected function mapAssistantMessage(AssistantMessage $message): void
{
$cacheType = $message->providerOptions('cacheType');
$cacheTtl = $message->providerOptions('cacheTtl');

$toolCalls = array_map(fn (ToolCall $toolCall): array => [
'id' => $toolCall->id,
Expand All @@ -171,7 +185,10 @@ protected function mapAssistantMessage(AssistantMessage $message): void
[
'type' => 'text',
'text' => $message->content,
'cache_control' => ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType],
'cache_control' => array_filter([
'type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType,
'ttl' => $cacheTtl,
]),
],
],
'tool_calls' => $toolCalls,
Expand Down
Loading