Skip to content

Commit 3e96019

Browse files
committed
Fix Laravel context errors
1 parent 1f18eb1 commit 3e96019

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

public/logo.svg

Lines changed: 12 additions & 0 deletions
Loading

src/Pollingo.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ private function __construct(
4545
* @param Translator<T>|null $translator
4646
* @return self<T>
4747
*/
48-
public static function make(string $apiKey = '', string $model = 'gpt-4o', ?Translator $translator = null): self
48+
public static function make(?string $apiKey = null, string $model = 'gpt-4o', ?Translator $translator = null): self
4949
{
50-
if ($apiKey === '' && $translator === null) {
50+
// Ensure apiKey is a valid string, even if empty
51+
$apiKey = $apiKey ?? '';
52+
53+
// Only throw exception if both apiKey is empty AND translator is null
54+
// This allows for blank string apiKeys if they're intentionally set that way in config
55+
if (($apiKey === '' || $apiKey === null) && $translator === null) {
5156
throw new InvalidArgumentException('Either apiKey or translator must be provided');
5257
}
5358

@@ -56,7 +61,7 @@ public static function make(string $apiKey = '', string $model = 'gpt-4o', ?Tran
5661
}
5762

5863
/** @var OpenAITranslator<T> */
59-
$defaultTranslator = new OpenAITranslator($apiKey ?? '', $model);
64+
$defaultTranslator = new OpenAITranslator($apiKey, $model);
6065

6166
/** @var self<T> */
6267
return new self($defaultTranslator);

src/PollingoServiceProvider.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@ public function register(): void
2525
$this->app->singleton(Pollingo::class, function ($app) {
2626
$config = $app['config']['pollingo'];
2727

28+
// Debugging to see the actual config
29+
\Log::debug('Pollingo config:', ['config' => $config]);
30+
31+
$apiKey = $config['openai_api_key'] ?? null;
32+
// If apiKey is null, try to get it from nested structure
33+
if ($apiKey === null && isset($config['openai']['api_key'])) {
34+
$apiKey = $config['openai']['api_key'];
35+
}
36+
37+
// Ensure apiKey is a string, not null, to avoid type error
38+
$apiKey = $apiKey ?? '';
39+
40+
$model = $config['openai_model'] ?? $config['openai']['model'] ?? 'gpt-4o';
41+
2842
/**
2943
* @var Pollingo<TKey> $pollingo
3044
*/
3145
$pollingo = Pollingo::make(
32-
apiKey: $config['openai_api_key'],
33-
model: $config['openai_model'],
46+
apiKey: $apiKey,
47+
model: $model,
3448
);
3549

3650
return $pollingo;
@@ -42,8 +56,8 @@ public function register(): void
4256

4357
/** @var OpenAITranslator<TKey> */
4458
$translator = new OpenAITranslator(
45-
apiKey: $config['openai']['api_key'],
46-
model: $config['openai']['model'],
59+
apiKey: $config['openai']['api_key'] ?? '',
60+
model: $config['openai']['model'] ?? 'gpt-4o',
4761
);
4862

4963
return $translator;

0 commit comments

Comments
 (0)