1212use OpenAI \Testing \Responses \Concerns \Fakeable ;
1313
1414/**
15- * @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
15+ * @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} }>
1616 */
1717final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
1818{
1919 /**
20- * @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
20+ * @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} }>
2121 */
2222 use ArrayAccessible;
2323
@@ -31,12 +31,13 @@ private function __construct(
3131 public readonly int $ created ,
3232 public readonly array $ data ,
3333 private readonly MetaInformation $ meta ,
34+ public readonly ?ImageResponseUsage $ usage = null ,
3435 ) {}
3536
3637 /**
3738 * Acts as static factory, and returns a new Response instance.
3839 *
39- * @param array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>} $attributes
40+ * @param array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} } $attributes
4041 */
4142 public static function from (array $ attributes , MetaInformation $ meta ): self
4243 {
@@ -48,6 +49,7 @@ public static function from(array $attributes, MetaInformation $meta): self
4849 $ attributes ['created ' ],
4950 $ results ,
5051 $ meta ,
52+ isset ($ attributes ['usage ' ]) ? ImageResponseUsage::from ($ attributes ['usage ' ]) : null ,
5153 );
5254 }
5355
@@ -56,12 +58,18 @@ public static function from(array $attributes, MetaInformation $meta): self
5658 */
5759 public function toArray (): array
5860 {
59- return [
61+ $ result = [
6062 'created ' => $ this ->created ,
6163 'data ' => array_map (
6264 static fn (CreateResponseData $ result ): array => $ result ->toArray (),
6365 $ this ->data ,
6466 ),
6567 ];
68+
69+ if ($ this ->usage !== null ) {
70+ $ result ['usage ' ] = $ this ->usage ->toArray ();
71+ }
72+
73+ return $ result ;
6674 }
6775}
0 commit comments