Skip to content

Commit

Permalink
Refactor from single to multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schmitt committed Feb 5, 2024
1 parent c18e3d8 commit 4f8aff8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
30 changes: 30 additions & 0 deletions Classes/AccountRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Sitegeist\Flow\OpenAiClientFactory;

use Neos\Flow\Annotations as Flow;

#[Flow\Proxy(false)]
class AccountRecord
{
public function __construct(
public readonly string $name,
public readonly string $apiKey,
public readonly string $organisation,
) {
}

/**
* @param array<string,mixed> $configuration
*/
public static function fromConfiguration(string $name, array $configuration): self
{
return new self(
$name,
$configuration['apiKey'],
$configuration['organisation'],
);
}
}
35 changes: 35 additions & 0 deletions Classes/AccountRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Sitegeist\Flow\OpenAiClientFactory;

use Neos\Flow\Annotations as Flow;

#[Flow\Scope('singleton')]
class AccountRepository
{
#[Flow\InjectConfiguration(path: 'accounts')]
protected ?array $accounts = [];

/**
* @return array<AccountRecord>
*/
public function findAll(): array
{
return array_map(
fn (array $data, string $name): AccountRecord => AccountRecord::fromConfiguration($name, $data),
$this->accounts,
array_keys($this->accounts)
);
}

public function findById(string $id): AccountRecord
{
if (array_key_exists($id, $this->accounts)) {
return AccountRecord::fromConfiguration($id, $this->accounts[$id]);
}

throw new \Exception('Unknown account "' . $id . '"', 1707149688);
}
}
17 changes: 6 additions & 11 deletions Classes/OpenAiClientFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Sitegeist\Flow\OpenAiClientFactory;
Expand All @@ -10,22 +11,16 @@

class OpenAiClientFactory
{
#[Flow\InjectConfiguration(path: 'apiKey')]
protected ?string $apiKey = '';

#[Flow\InjectConfiguration(path: 'organisation')]
protected ?string $organisation = '';

#[Flow\Inject]
protected ClientInterface $client;
protected ClientInterface $httpClient;

public function createClient(): ClientContract
public function createClientForAccountRecord(AccountRecord $record): ClientContract
{
$factory = (new Factory())
->withHttpClient($this->client)
->withApiKey($this->apiKey)
->withHttpClient($this->httpClient)
->withApiKey($record->apiKey)
->withHttpHeader('OpenAI-Beta', 'assistants=v1')
->withOrganization($this->organisation);
->withOrganization($record->organisation);
return $factory->make();
}
}
7 changes: 0 additions & 7 deletions Configuration/Objects.yaml

This file was deleted.

1 change: 0 additions & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ Sitegeist:
OpenAiClientFactory:
accounts: []
# name:
# discriminator: 'aja-%ENV:ENVIRONMENT_NAME%'
# apiKey: ~
# organisation: ~

0 comments on commit 4f8aff8

Please sign in to comment.