-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #21
- Loading branch information
Showing
6 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeminiAPI\Json; | ||
|
||
use RuntimeException; | ||
|
||
class ObjectListParser | ||
{ | ||
private int $depth = 0; | ||
private bool $inString = false; | ||
private bool $inEscape = false; | ||
private string $json = ''; | ||
|
||
/** @var callable(array): void */ | ||
private $callback; // @phpstan-ignore-line | ||
|
||
/** | ||
* @phpstan-ignore-next-line | ||
* @param callable(array): void $callback | ||
*/ | ||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @return int | ||
* @throws RuntimeException | ||
*/ | ||
public function consume(string $str): int | ||
{ | ||
$offset = 0; | ||
for ($i = 0; $i < strlen($str); $i++) { | ||
if ($this->inEscape) { | ||
$this->inEscape = false; | ||
} elseif ($this->inString) { | ||
if ($str[$i] === '\\') { | ||
$this->inEscape = true; | ||
} elseif ($str[$i] === '"') { | ||
$this->inString = false; | ||
} | ||
} elseif ($str[$i] === '"') { | ||
$this->inString = true; | ||
} elseif ($str[$i] === '{') { | ||
if ($this->depth === 0) { | ||
$offset = $i; | ||
} | ||
$this->depth++; | ||
} elseif ($str[$i] === '}') { | ||
$this->depth--; | ||
if ($this->depth === 0) { | ||
$this->json .= substr($str, $offset, $i - $offset + 1); | ||
$arr = json_decode($this->json, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new RuntimeException('ObjectListParser could not decode the given message'); | ||
} | ||
|
||
($this->callback)($arr); | ||
$this->json = ''; | ||
$offset = $i + 1; | ||
} | ||
} | ||
} | ||
|
||
$this->json .= substr($str, $offset) ?: ''; | ||
|
||
return strlen($str); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeminiAPI\Requests; | ||
|
||
use GeminiAPI\Enums\ModelName; | ||
use GeminiAPI\GenerationConfig; | ||
use GeminiAPI\SafetySetting; | ||
use GeminiAPI\Traits\ArrayTypeValidator; | ||
use GeminiAPI\Resources\Content; | ||
use JsonSerializable; | ||
|
||
use function json_encode; | ||
|
||
class GenerateContentStreamRequest implements JsonSerializable, RequestInterface | ||
{ | ||
use ArrayTypeValidator; | ||
|
||
/** | ||
* @param ModelName $modelName | ||
* @param Content[] $contents | ||
* @param SafetySetting[] $safetySettings | ||
* @param GenerationConfig|null $generationConfig | ||
*/ | ||
public function __construct( | ||
public readonly ModelName $modelName, | ||
public readonly array $contents, | ||
public readonly array $safetySettings = [], | ||
public readonly ?GenerationConfig $generationConfig = null, | ||
) { | ||
$this->ensureArrayOfType($this->contents, Content::class); | ||
$this->ensureArrayOfType($this->safetySettings, SafetySetting::class); | ||
} | ||
|
||
public function getOperation(): string | ||
{ | ||
return "{$this->modelName->value}:streamGenerateContent"; | ||
} | ||
|
||
public function getHttpMethod(): string | ||
{ | ||
return 'POST'; | ||
} | ||
|
||
public function getHttpPayload(): string | ||
{ | ||
return (string) $this; | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* model: string, | ||
* contents: Content[], | ||
* safetySettings?: SafetySetting[], | ||
* generationConfig?: GenerationConfig, | ||
* } | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$arr = [ | ||
'model' => $this->modelName->value, | ||
'contents' => $this->contents, | ||
]; | ||
|
||
if (!empty($this->safetySettings)) { | ||
$arr['safetySettings'] = $this->safetySettings; | ||
} | ||
|
||
if ($this->generationConfig) { | ||
$arr['generationConfig'] = $this->generationConfig; | ||
} | ||
|
||
return $arr; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return json_encode($this) ?: ''; | ||
} | ||
} |