Skip to content

Commit c36adbb

Browse files
(feat) thinking summary for gemini, anthropic (stream) openai (text, stream) (#765)
1 parent c440cf5 commit c36adbb

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

src/Providers/Anthropic/Handlers/Stream.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ protected function handleMessageStop(array $event): StreamEndEvent
236236
timestamp: time(),
237237
finishReason: FinishReason::Stop, // Default, will be updated by message_delta
238238
usage: $this->state->usage(),
239-
citations: $this->state->citations() !== [] ? $this->state->citations() : null
239+
citations: $this->state->citations() !== [] ? $this->state->citations() : null,
240+
additionalContent: [
241+
'thinking' => $this->state->thinkingSummaries() === [] ? null : implode('', $this->state->thinkingSummaries()),
242+
]
240243
);
241244
}
242245

src/Providers/Gemini/Handlers/Stream.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ protected function processStream(Response $response, Request $request, int $dept
214214
timestamp: time(),
215215
finishReason: $finishReason,
216216
usage: $this->state->usage(),
217-
additionalContent: $groundingMetadata !== null ? ['grounding_metadata' => $groundingMetadata] : []
217+
additionalContent: Arr::whereNotNull([
218+
'grounding_metadata' => $groundingMetadata,
219+
'thoughtSummaries' => $this->state->thinkingSummaries() === [] ? null : $this->state->thinkingSummaries(),
220+
])
218221
);
219222
}
220223
}

src/Providers/OpenAI/Handlers/Stream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ protected function processStream(Response $response, Request $request, int $dept
250250
),
251251
additionalContent: Arr::whereNotNull([
252252
'response_id' => data_get($data, 'response.id'),
253+
'reasoningSummaries' => $this->state->thinkingSummaries() === [] ? null : $this->state->thinkingSummaries(),
253254
])
254255
);
255256
}

src/Providers/OpenAI/Handlers/Text.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,15 @@ protected function addStep(
161161
ClientResponse $clientResponse,
162162
array $toolResults = []
163163
): void {
164+
/** @var array<array-key, array<string, mixed>> $output */
165+
$output = data_get($data, 'output', []);
166+
164167
$this->responseBuilder->addStep(new Step(
165168
text: data_get($data, 'output.{last}.content.0.text') ?? '',
166169
finishReason: $this->mapFinishReason($data),
167-
toolCalls: ToolCallMap::map(array_filter(data_get($data, 'output', []), fn (array $output): bool => $output['type'] === 'function_call')),
170+
toolCalls: ToolCallMap::map(array_filter($output, fn (array $output): bool => $output['type'] === 'function_call')),
168171
toolResults: $toolResults,
169-
providerToolCalls: ProviderToolCallMap::map(data_get($data, 'output', [])),
172+
providerToolCalls: ProviderToolCallMap::map($output),
170173
usage: new Usage(
171174
promptTokens: data_get($data, 'usage.input_tokens', 0) - data_get($data, 'usage.input_tokens_details.cached_tokens', 0),
172175
completionTokens: data_get($data, 'usage.output_tokens'),
@@ -183,6 +186,11 @@ protected function addStep(
183186
systemPrompts: $request->systemPrompts(),
184187
additionalContent: Arr::whereNotNull([
185188
'citations' => $this->citations,
189+
'reasoningSummaries' => collect($output)
190+
->filter(fn (array $output): bool => $output['type'] === 'reasoning')
191+
->flatMap(fn (array $output): array => Arr::pluck($output['summary'] ?? [], 'text'))
192+
->filter()
193+
->toArray(),
186194
]),
187195
));
188196
}

src/Streaming/StreamState.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class StreamState
2424

2525
protected string $currentThinking = '';
2626

27+
/**
28+
* @var array<array-key, string>
29+
*/
30+
protected array $thinkingSummaries = [];
31+
2732
protected ?int $currentBlockIndex = null;
2833

2934
protected ?string $currentBlockType = null;
@@ -114,6 +119,7 @@ public function appendText(string $text): self
114119
public function appendThinking(string $thinking): self
115120
{
116121
$this->currentThinking .= $thinking;
122+
$this->thinkingSummaries[] = $thinking;
117123

118124
return $this;
119125
}
@@ -256,6 +262,14 @@ public function currentThinking(): string
256262
return $this->currentThinking;
257263
}
258264

265+
/**
266+
* @return array<array-key, string>
267+
*/
268+
public function thinkingSummaries(): array
269+
{
270+
return $this->thinkingSummaries;
271+
}
272+
259273
public function currentBlockIndex(): ?int
260274
{
261275
return $this->currentBlockIndex;

tests/Fixtures/openai/text-reasoning-effort-1.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
{
1515
"id": "rs_689b71947cbc8199af51368ed8e14a810f49e192ec3eba8e",
1616
"type": "reasoning",
17-
"summary": []
17+
"summary": [
18+
{
19+
"type": "summary_text",
20+
"text": "I should introduce myself to the user."
21+
}
22+
]
1823
},
1924
{
2025
"id": "msg_689b71959da481998b4b6284842788c80f49e192ec3eba8e",

tests/Providers/OpenAI/TextTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@
567567
it('sends reasoning effort when defined', function (): void {
568568
FixtureResponse::fakeResponseSequence('v1/responses', 'openai/text-reasoning-effort');
569569

570-
Prism::text()
570+
$response = Prism::text()
571571
->using('openai', 'gpt-5')
572572
->withPrompt('Who are you?')
573573
->withProviderOptions([
@@ -578,6 +578,10 @@
578578
->asText();
579579

580580
Http::assertSent(fn (Request $request): bool => $request->data()['reasoning']['effort'] === 'low');
581+
582+
expect($response->additionalContent['reasoningSummaries'])->toBe([
583+
'I should introduce myself to the user.',
584+
]);
581585
});
582586

583587
describe('provider tool results', function (): void {

0 commit comments

Comments
 (0)