Skip to content

Commit f9e0406

Browse files
authored
feat(OpenAI): Add Containers API (#636)
* wip: add containers * chore: build out container api * feat: build out client/resource for Containers * fix: correct typing * feat: add pest tests
1 parent e790121 commit f9e0406

File tree

16 files changed

+917
-0
lines changed

16 files changed

+917
-0
lines changed

src/Client.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OpenAI\Resources\Batches;
1515
use OpenAI\Resources\Chat;
1616
use OpenAI\Resources\Completions;
17+
use OpenAI\Resources\Containers;
1718
use OpenAI\Resources\Edits;
1819
use OpenAI\Resources\Embeddings;
1920
use OpenAI\Resources\Files;
@@ -68,6 +69,16 @@ public function chat(): Chat
6869
return new Chat($this->transporter);
6970
}
7071

72+
/**
73+
* Create and manage containers for use with the Code Interpreter tool.
74+
*
75+
* @see https://platform.openai.com/docs/api-reference/containers
76+
*/
77+
public function containers(): Containers
78+
{
79+
return new Containers($this->transporter);
80+
}
81+
7182
/**
7283
* Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
7384
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Contracts\Resources;
6+
7+
use OpenAI\Responses\Containers\CreateContainer;
8+
use OpenAI\Responses\Containers\DeleteContainer;
9+
use OpenAI\Responses\Containers\ListContainers;
10+
use OpenAI\Responses\Containers\RetrieveContainer;
11+
12+
interface ContainersContract
13+
{
14+
/**
15+
* Creates a container for use with the Code Interpreter tool.
16+
*
17+
* @see https://platform.openai.com/docs/api-reference/containers/createContainers
18+
*
19+
* @param array<string, mixed> $parameters
20+
*/
21+
public function create(array $parameters): CreateContainer;
22+
23+
/**
24+
* Retrieves a container with the given ID.
25+
*
26+
* @see https://platform.openai.com/docs/api-reference/containers/retrieveContainer
27+
*/
28+
public function retrieve(string $id): RetrieveContainer;
29+
30+
/**
31+
* Delete a container with the given ID.
32+
*
33+
* @see https://platform.openai.com/docs/api-reference/containers/deleteContainer
34+
*/
35+
public function delete(string $id): DeleteContainer;
36+
37+
/**
38+
* List containers
39+
*
40+
* @see https://platform.openai.com/docs/api-reference/containers/listContainers
41+
*
42+
* @param array<string, mixed> $parameters
43+
*/
44+
public function list(array $parameters = []): ListContainers;
45+
}

src/Resources/Containers.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Resources;
6+
7+
use OpenAI\Contracts\Resources\ContainersContract;
8+
use OpenAI\Responses\Containers\CreateContainer;
9+
use OpenAI\Responses\Containers\DeleteContainer;
10+
use OpenAI\Responses\Containers\ListContainers;
11+
use OpenAI\Responses\Containers\RetrieveContainer;
12+
use OpenAI\ValueObjects\Transporter\Payload;
13+
use OpenAI\ValueObjects\Transporter\Response;
14+
15+
/**
16+
* @phpstan-import-type CreateContainerType from CreateContainer
17+
* @phpstan-import-type RetrieveContainerType from RetrieveContainer
18+
* @phpstan-import-type DeleteContainerType from DeleteContainer
19+
* @phpstan-import-type ListContainersType from ListContainers
20+
*/
21+
final class Containers implements ContainersContract
22+
{
23+
use Concerns\Transportable;
24+
25+
/**
26+
* Creates a container for use with the Code Interpreter tool.
27+
*
28+
* @see https://platform.openai.com/docs/api-reference/containers/createContainers
29+
*
30+
* @param array<string, mixed> $parameters
31+
*/
32+
public function create(array $parameters): CreateContainer
33+
{
34+
$payload = Payload::create('containers', $parameters);
35+
36+
/** @var Response<CreateContainerType> $response */
37+
$response = $this->transporter->requestObject($payload);
38+
39+
return CreateContainer::from($response->data(), $response->meta());
40+
}
41+
42+
/**
43+
* Retrieves a container with the given ID.
44+
*
45+
* @see https://platform.openai.com/docs/api-reference/containers/retrieveContainer
46+
*/
47+
public function retrieve(string $id): RetrieveContainer
48+
{
49+
$payload = Payload::retrieve('containers', $id);
50+
51+
/** @var Response<RetrieveContainerType> $response */
52+
$response = $this->transporter->requestObject($payload);
53+
54+
return RetrieveContainer::from($response->data(), $response->meta());
55+
}
56+
57+
/**
58+
* Delete a container with the given ID.
59+
*
60+
* @see https://platform.openai.com/docs/api-reference/containers/deleteContainer
61+
*/
62+
public function delete(string $id): DeleteContainer
63+
{
64+
$payload = Payload::delete('containers', $id);
65+
66+
/** @var Response<DeleteContainerType> $response */
67+
$response = $this->transporter->requestObject($payload);
68+
69+
return DeleteContainer::from($response->data(), $response->meta());
70+
}
71+
72+
/**
73+
* List containers
74+
*
75+
* @see https://platform.openai.com/docs/api-reference/containers/listContainers
76+
*
77+
* @param array<string, mixed> $parameters
78+
*/
79+
public function list(array $parameters = []): ListContainers
80+
{
81+
$payload = Payload::list('containers', $parameters);
82+
83+
/** @var Response<ListContainersType> $response */
84+
$response = $this->transporter->requestObject($payload);
85+
86+
return ListContainers::from($response->data(), $response->meta());
87+
}
88+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Containers;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Contracts\ResponseHasMetaInformationContract;
9+
use OpenAI\Responses\Concerns\ArrayAccessible;
10+
use OpenAI\Responses\Concerns\HasMetaInformation;
11+
use OpenAI\Responses\Containers\Objects\ExpiresAfter;
12+
use OpenAI\Responses\Meta\MetaInformation;
13+
use OpenAI\Testing\Responses\Concerns\Fakeable;
14+
15+
/**
16+
* @phpstan-import-type ExpiresAfterType from ExpiresAfter
17+
*
18+
* @phpstan-type CreateContainerType array{id: string, object: 'container', created_at: int, status: string, expires_after: ExpiresAfterType, last_active_at: int, name: string}
19+
*
20+
* @implements ResponseContract<CreateContainerType>
21+
*/
22+
final class CreateContainer implements ResponseContract, ResponseHasMetaInformationContract
23+
{
24+
/**
25+
* @use ArrayAccessible<CreateContainerType>
26+
*/
27+
use ArrayAccessible;
28+
29+
use Fakeable;
30+
use HasMetaInformation;
31+
32+
/**
33+
* @param 'container' $object
34+
*/
35+
private function __construct(
36+
public readonly string $id,
37+
public readonly string $object,
38+
public readonly int $createdAt,
39+
public readonly string $status,
40+
public readonly ExpiresAfter $expiresAfter,
41+
public readonly int $lastActiveAt,
42+
public readonly string $name,
43+
private readonly MetaInformation $meta,
44+
) {}
45+
46+
/**
47+
* @param CreateContainerType $attributes
48+
*/
49+
public static function from(array $attributes, MetaInformation $meta): self
50+
{
51+
return new self(
52+
id: $attributes['id'],
53+
object: $attributes['object'],
54+
createdAt: $attributes['created_at'],
55+
status: $attributes['status'],
56+
expiresAfter: ExpiresAfter::from($attributes['expires_after']),
57+
lastActiveAt: $attributes['last_active_at'],
58+
name: $attributes['name'],
59+
meta: $meta,
60+
);
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
public function toArray(): array
67+
{
68+
return [
69+
'id' => $this->id,
70+
'object' => $this->object,
71+
'created_at' => $this->createdAt,
72+
'status' => $this->status,
73+
'expires_after' => $this->expiresAfter->toArray(),
74+
'last_active_at' => $this->lastActiveAt,
75+
'name' => $this->name,
76+
];
77+
}
78+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Containers;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Contracts\ResponseHasMetaInformationContract;
9+
use OpenAI\Responses\Concerns\ArrayAccessible;
10+
use OpenAI\Responses\Concerns\HasMetaInformation;
11+
use OpenAI\Responses\Meta\MetaInformation;
12+
use OpenAI\Testing\Responses\Concerns\Fakeable;
13+
14+
/**
15+
* @phpstan-type DeleteContainerType array{id: string, object: string, deleted: bool}
16+
*
17+
* @implements ResponseContract<DeleteContainerType>
18+
*/
19+
final class DeleteContainer implements ResponseContract, ResponseHasMetaInformationContract
20+
{
21+
/**
22+
* @use ArrayAccessible<DeleteContainerType>
23+
*/
24+
use ArrayAccessible;
25+
26+
use Fakeable;
27+
use HasMetaInformation;
28+
29+
private function __construct(
30+
public readonly string $id,
31+
public readonly string $object,
32+
public readonly bool $deleted,
33+
private readonly MetaInformation $meta,
34+
) {}
35+
36+
/**
37+
* @param DeleteContainerType $attributes
38+
*/
39+
public static function from(array $attributes, MetaInformation $meta): self
40+
{
41+
return new self(
42+
id: $attributes['id'],
43+
object: $attributes['object'],
44+
deleted: $attributes['deleted'],
45+
meta: $meta,
46+
);
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function toArray(): array
53+
{
54+
return [
55+
'id' => $this->id,
56+
'object' => $this->object,
57+
'deleted' => $this->deleted,
58+
];
59+
}
60+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Containers;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Contracts\ResponseHasMetaInformationContract;
9+
use OpenAI\Responses\Concerns\ArrayAccessible;
10+
use OpenAI\Responses\Concerns\HasMetaInformation;
11+
use OpenAI\Responses\Meta\MetaInformation;
12+
use OpenAI\Testing\Responses\Concerns\Fakeable;
13+
14+
/**
15+
* @phpstan-import-type RetrieveContainerType from RetrieveContainer
16+
*
17+
* @phpstan-type ListContainersType array{object: 'list', data: RetrieveContainerType[], first_id: string|null, last_id: string|null, has_more: bool}
18+
*
19+
* @implements ResponseContract<ListContainersType>
20+
*/
21+
final class ListContainers implements ResponseContract, ResponseHasMetaInformationContract
22+
{
23+
/**
24+
* @use ArrayAccessible<ListContainersType>
25+
*/
26+
use ArrayAccessible;
27+
28+
use Fakeable;
29+
use HasMetaInformation;
30+
31+
/**
32+
* @param 'list' $object
33+
* @param RetrieveContainer[] $data
34+
*/
35+
private function __construct(
36+
public readonly string $object,
37+
public readonly array $data,
38+
public readonly ?string $firstId,
39+
public readonly ?string $lastId,
40+
public readonly bool $hasMore,
41+
private readonly MetaInformation $meta,
42+
) {}
43+
44+
/**
45+
* @param ListContainersType $attributes
46+
*/
47+
public static function from(array $attributes, MetaInformation $meta): self
48+
{
49+
return new self(
50+
object: $attributes['object'],
51+
data: array_map(
52+
fn (array $container): RetrieveContainer => RetrieveContainer::from($container, $meta),
53+
$attributes['data']
54+
),
55+
firstId: $attributes['first_id'] ?? null,
56+
lastId: $attributes['last_id'] ?? null,
57+
hasMore: $attributes['has_more'],
58+
meta: $meta,
59+
);
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function toArray(): array
66+
{
67+
return [
68+
'object' => $this->object,
69+
'data' => array_map(
70+
fn (RetrieveContainer $container): array => $container->toArray(),
71+
$this->data
72+
),
73+
'first_id' => $this->firstId,
74+
'last_id' => $this->lastId,
75+
'has_more' => $this->hasMore,
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)