Skip to content

Commit ae13f07

Browse files
committed
feat: initial work on realtime tokens
1 parent 59e27ca commit ae13f07

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Contracts\Resources;
6+
7+
interface RealtimeContract
8+
{
9+
/**
10+
* Create an ephemeral API token for real time sessions.
11+
*
12+
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create
13+
*
14+
* @param array<string, mixed> $parameters
15+
*/
16+
public function token(array $parameters = []);
17+
18+
/**
19+
* Create a new response.
20+
*
21+
* @see https://platform.openai.com/docs/api-reference/realtime-sessions/create-transcription
22+
*
23+
* @param array<string, mixed> $parameters
24+
*/
25+
public function transcribeToken(array $parameters = []);
26+
}

src/Resources/Realtime.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Resources;
6+
7+
use OpenAI\Contracts\Resources\RealtimeContract;
8+
use OpenAI\Responses\Responses\CreateResponse;
9+
use OpenAI\Responses\Responses\ListInputItems;
10+
use OpenAI\Responses\Responses\RetrieveResponse;
11+
12+
/**
13+
* @phpstan-import-type CreateResponseType from CreateResponse
14+
* @phpstan-import-type RetrieveResponseType from RetrieveResponse
15+
* @phpstan-import-type ListInputItemsType from ListInputItems
16+
*/
17+
final class Realtime implements RealtimeContract
18+
{
19+
use Concerns\Streamable;
20+
use Concerns\Transportable;
21+
22+
public function token(array $parameters = [])
23+
{
24+
// TODO: Implement token() method.
25+
}
26+
27+
public function transcribeToken(array $parameters = [])
28+
{
29+
// TODO: Implement transcribeToken() method.
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Realtime;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-type ClientSecretType array{expires_at: int, value: string}
13+
*
14+
* @implements ResponseContract<ClientSecretType>
15+
*/
16+
final class SessionClientSecret implements ResponseContract
17+
{
18+
/**
19+
* @use ArrayAccessible<ClientSecretType>
20+
*/
21+
use ArrayAccessible;
22+
23+
use Fakeable;
24+
25+
private function __construct(
26+
public readonly int $expiresAt,
27+
public readonly string $value,
28+
) {}
29+
30+
/**
31+
* @param ClientSecretType $attributes
32+
*/
33+
public static function from(array $attributes): self
34+
{
35+
return new self(
36+
expiresAt: $attributes['expires_at'],
37+
value: $attributes['value'],
38+
);
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function toArray(): array
45+
{
46+
return [
47+
'expires_at' => $this->expiresAt,
48+
'value' => $this->value,
49+
];
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Realtime;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-import-type ClientSecretType from SessionClientSecret
13+
*
14+
* @phpstan-type SessionType array{client_secret: ClientSecretType, input_audio_format: 'pcm16'|'g711_ulaw'|'g711_alaw'}
15+
*
16+
* @implements ResponseContract<SessionType>
17+
*/
18+
final class SessionResponse implements ResponseContract
19+
{
20+
/**
21+
* @use ArrayAccessible<SessionType>
22+
*/
23+
use ArrayAccessible;
24+
25+
use Fakeable;
26+
27+
private function __construct(
28+
public readonly SessionClientSecret $clientSecret,
29+
public readonly string $inputAudioFormat,
30+
) {}
31+
32+
/**
33+
* @param SessionType $attributes
34+
*/
35+
public static function from(array $attributes): self
36+
{
37+
return new self(
38+
clientSecret: SessionClientSecret::from($attributes['client_secret']),
39+
inputAudioFormat: $attributes['input_audio_format'],
40+
);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
'client_secret' => $this->clientSecret->toArray(),
50+
'input_audio_format' => $this->inputAudioFormat,
51+
];
52+
}
53+
}

tests/Resources/Realtime.php

Whitespace-only changes.

0 commit comments

Comments
 (0)