Skip to content

Commit 9eadda1

Browse files
feat(OpenAI): Add responses streaming types (#711)
* feat responses streaming types Signed-off-by: Mads Kildegård <mkildegaard99@gmail.com> * fixed tests Signed-off-by: Mads Kildegård <mkildegaard99@gmail.com> --------- Signed-off-by: Mads Kildegård <mkildegaard99@gmail.com>
1 parent 3362ab0 commit 9eadda1

34 files changed

+223
-70
lines changed

src/Responses/Responses/CreateStreamedResponse.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OpenAI\Responses\Responses\Streaming\ReasoningTextDone;
3434
use OpenAI\Responses\Responses\Streaming\RefusalDelta;
3535
use OpenAI\Responses\Responses\Streaming\RefusalDone;
36+
use OpenAI\Responses\Responses\Streaming\Response;
3637
use OpenAI\Responses\Responses\Streaming\WebSearchCall;
3738
use OpenAI\Testing\Responses\Concerns\FakeableForStreamedResponse;
3839

@@ -52,7 +53,7 @@ final class CreateStreamedResponse implements ResponseContract
5253

5354
private function __construct(
5455
public readonly string $event,
55-
public readonly CreateResponse|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
56+
public readonly Response|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
5657
) {}
5758

5859
/**
@@ -69,7 +70,7 @@ public static function from(array $attributes): self
6970
'response.in_progress',
7071
'response.completed',
7172
'response.failed',
72-
'response.incomplete' => CreateResponse::from($attributes['response'], $meta), // @phpstan-ignore-line
73+
'response.incomplete' => Response::from($attributes, $meta), // @phpstan-ignore-line
7374
'response.output_item.added',
7475
'response.output_item.done' => OutputItem::from($attributes, $meta), // @phpstan-ignore-line
7576
'response.content_part.added',

src/Responses/Responses/Streaming/CodeInterpreterCall.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type CodeInterpreterCallType array{item_id: string, output_index: int}
15+
* @phpstan-type CodeInterpreterCallType array{type: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<CodeInterpreterCallType>
1818
*/
@@ -27,6 +27,7 @@ final class CodeInterpreterCall implements ResponseContract, ResponseHasMetaInfo
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $itemId,
3132
public readonly int $outputIndex,
3233
private readonly MetaInformation $meta,
@@ -38,6 +39,7 @@ private function __construct(
3839
public static function from(array $attributes, MetaInformation $meta): self
3940
{
4041
return new self(
42+
type: $attributes['type'],
4143
itemId: $attributes['item_id'],
4244
outputIndex: $attributes['output_index'],
4345
meta: $meta,
@@ -50,6 +52,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5052
public function toArray(): array
5153
{
5254
return [
55+
'type' => $this->type,
5356
'item_id' => $this->itemId,
5457
'output_index' => $this->outputIndex,
5558
];

src/Responses/Responses/Streaming/CodeInterpreterCodeDelta.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type CodeInterpreterCodeDeltaType array{delta: string, item_id: string, output_index: int}
15+
* @phpstan-type CodeInterpreterCodeDeltaType array{type: string, delta: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<CodeInterpreterCodeDeltaType>
1818
*/
@@ -27,6 +27,7 @@ final class CodeInterpreterCodeDelta implements ResponseContract, ResponseHasMet
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $delta,
3132
public readonly string $itemId,
3233
public readonly int $outputIndex,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
delta: $attributes['delta'],
4345
itemId: $attributes['item_id'],
4446
outputIndex: $attributes['output_index'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'delta' => $this->delta,
5659
'item_id' => $this->itemId,
5760
'output_index' => $this->outputIndex,

src/Responses/Responses/Streaming/CodeInterpreterCodeDone.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type CodeInterpreterCodeDoneType array{code: string, item_id: string, output_index: int}
15+
* @phpstan-type CodeInterpreterCodeDoneType array{type: string, code: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<CodeInterpreterCodeDoneType>
1818
*/
@@ -27,6 +27,7 @@ final class CodeInterpreterCodeDone implements ResponseContract, ResponseHasMeta
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $code,
3132
public readonly string $itemId,
3233
public readonly int $outputIndex,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
code: $attributes['code'],
4345
itemId: $attributes['item_id'],
4446
outputIndex: $attributes['output_index'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'code' => $this->code,
5659
'item_id' => $this->itemId,
5760
'output_index' => $this->outputIndex,

src/Responses/Responses/Streaming/ContentPart.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @phpstan-import-type OutputTextType from OutputMessageContentOutputText
1818
* @phpstan-import-type ContentRefusalType from OutputMessageContentRefusal
1919
*
20-
* @phpstan-type ContentPartType array{content_index: int, item_id: string, output_index: int, part: OutputTextType|ContentRefusalType}
20+
* @phpstan-type ContentPartType array{type: string, content_index: int, item_id: string, output_index: int, part: OutputTextType|ContentRefusalType}
2121
*
2222
* @implements ResponseContract<ContentPartType>
2323
*/
@@ -32,6 +32,7 @@ final class ContentPart implements ResponseContract, ResponseHasMetaInformationC
3232
use HasMetaInformation;
3333

3434
private function __construct(
35+
public readonly string $type,
3536
public readonly int $contentIndex,
3637
public readonly string $itemId,
3738
public readonly int $outputIndex,
@@ -50,6 +51,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5051
};
5152

5253
return new self(
54+
type: $attributes['type'],
5355
contentIndex: $attributes['content_index'],
5456
itemId: $attributes['item_id'],
5557
outputIndex: $attributes['output_index'],
@@ -64,6 +66,7 @@ public static function from(array $attributes, MetaInformation $meta): self
6466
public function toArray(): array
6567
{
6668
return [
69+
'type' => $this->type,
6770
'content_index' => $this->contentIndex,
6871
'item_id' => $this->itemId,
6972
'output_index' => $this->outputIndex,

src/Responses/Responses/Streaming/Error.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type ErrorType array{code: string|null, message: string, param: string|null}
15+
* @phpstan-type ErrorType array{type: string, code: string|null, message: string, param: string|null}
1616
*
1717
* @implements ResponseContract<ErrorType>
1818
*/
@@ -27,6 +27,7 @@ final class Error implements ResponseContract, ResponseHasMetaInformationContrac
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly ?string $code,
3132
public readonly string $message,
3233
public readonly ?string $param,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
code: $attributes['code'],
4345
message: $attributes['message'],
4446
param: $attributes['param'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'code' => $this->code,
5659
'message' => $this->message,
5760
'param' => $this->param,

src/Responses/Responses/Streaming/FileSearchCall.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type FileSearchCallType array{item_id: string, output_index: int}
15+
* @phpstan-type FileSearchCallType array{type: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<FileSearchCallType>
1818
*/
@@ -27,6 +27,7 @@ final class FileSearchCall implements ResponseContract, ResponseHasMetaInformati
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $itemId,
3132
public readonly int $outputIndex,
3233
private readonly MetaInformation $meta,
@@ -38,6 +39,7 @@ private function __construct(
3839
public static function from(array $attributes, MetaInformation $meta): self
3940
{
4041
return new self(
42+
type: $attributes['type'],
4143
itemId: $attributes['item_id'],
4244
outputIndex: $attributes['output_index'],
4345
meta: $meta,
@@ -50,6 +52,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5052
public function toArray(): array
5153
{
5254
return [
55+
'type' => $this->type,
5356
'item_id' => $this->itemId,
5457
'output_index' => $this->outputIndex,
5558
];

src/Responses/Responses/Streaming/FunctionCallArgumentsDelta.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type FunctionCallArgumentsDeltaType array{delta: string, item_id: string, output_index: int}
15+
* @phpstan-type FunctionCallArgumentsDeltaType array{type: string, delta: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<FunctionCallArgumentsDeltaType>
1818
*/
@@ -27,6 +27,7 @@ final class FunctionCallArgumentsDelta implements ResponseContract, ResponseHasM
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $delta,
3132
public readonly string $itemId,
3233
public readonly int $outputIndex,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
delta: $attributes['delta'],
4345
itemId: $attributes['item_id'],
4446
outputIndex: $attributes['output_index'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'delta' => $this->delta,
5659
'item_id' => $this->itemId,
5760
'output_index' => $this->outputIndex,

src/Responses/Responses/Streaming/FunctionCallArgumentsDone.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type FunctionCallArgumentsDoneType array{arguments: string, item_id: string, output_index: int}
15+
* @phpstan-type FunctionCallArgumentsDoneType array{type: string, arguments: string, item_id: string, output_index: int}
1616
*
1717
* @implements ResponseContract<FunctionCallArgumentsDoneType>
1818
*/
@@ -27,6 +27,7 @@ final class FunctionCallArgumentsDone implements ResponseContract, ResponseHasMe
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly string $arguments,
3132
public readonly string $itemId,
3233
public readonly int $outputIndex,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
arguments: $attributes['arguments'],
4345
itemId: $attributes['item_id'],
4446
outputIndex: $attributes['output_index'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'arguments' => $this->arguments,
5659
'item_id' => $this->itemId,
5760
'output_index' => $this->outputIndex,

src/Responses/Responses/Streaming/ImageGenerationPart.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @phpstan-type ImageGenerationPartType array{output_index: int, item_id: string, sequence_number: int}
15+
* @phpstan-type ImageGenerationPartType array{type: string, output_index: int, item_id: string, sequence_number: int}
1616
*
1717
* @implements ResponseContract<ImageGenerationPartType>
1818
*/
@@ -27,6 +27,7 @@ final class ImageGenerationPart implements ResponseContract, ResponseHasMetaInfo
2727
use HasMetaInformation;
2828

2929
private function __construct(
30+
public readonly string $type,
3031
public readonly int $outputIndex,
3132
public readonly string $itemId,
3233
public readonly int $sequenceNumber,
@@ -39,6 +40,7 @@ private function __construct(
3940
public static function from(array $attributes, MetaInformation $meta): self
4041
{
4142
return new self(
43+
type: $attributes['type'],
4244
outputIndex: $attributes['output_index'],
4345
itemId: $attributes['item_id'],
4446
sequenceNumber: $attributes['sequence_number'],
@@ -52,6 +54,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5254
public function toArray(): array
5355
{
5456
return [
57+
'type' => $this->type,
5558
'output_index' => $this->outputIndex,
5659
'item_id' => $this->itemId,
5760
'sequence_number' => $this->sequenceNumber,

0 commit comments

Comments
 (0)