Skip to content

Commit

Permalink
refactor(Generators): make BitoGenerator class final and change visib…
Browse files Browse the repository at this point in the history
…ility of properties

- Change visibility of properties in BitoGenerator class
- Make BitoGenerator class final
  • Loading branch information
guanguans committed Jun 21, 2023
1 parent ccd6ea5 commit ea27423
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions app/Generators/BitoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
use Illuminate\Console\OutputStyle;
use Symfony\Component\Process\Process;

class BitoGenerator implements GeneratorContract
final class BitoGenerator implements GeneratorContract
{
/**
* @var array
*/
protected $config = [];
private $config = [];

/**
* @var \Illuminate\Console\OutputStyle
*/
protected $output;
private $outputStyle;

public function __construct(array $config)
{
$this->config = $config;
$this->output = resolve(OutputStyle::class);
$this->outputStyle = resolve(OutputStyle::class);
}

/**
Expand All @@ -48,7 +48,7 @@ public function generate(string $prompt): string
Process::class,
['command' => [$this->config['path'] ?: 'bito', '-p', $promptFile]] + $this->config['parameters']
)->mustRun(function (string $type, string $data): void {
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
})->getOutput();
}
}
6 changes: 3 additions & 3 deletions app/Generators/OpenAIGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class OpenAIGenerator implements GeneratorContract
/**
* @var \Illuminate\Console\OutputStyle
*/
protected $output;
protected $outputStyle;

public function __construct(array $config)
{
$this->config = $config;
$this->openAI = new OpenAI(Arr::only($config, ['http_options', 'retry', 'base_url', 'api_key']));
$this->output = resolve(OutputStyle::class);
$this->outputStyle = resolve(OutputStyle::class);
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ protected function getWriter(?string &$messages): \Closure
// (正常|错误|流)响应
$rowResponse = (array) json_decode(OpenAI::sanitizeData($data), true);
$messages .= $text = $this->extractCompletion($rowResponse);
$this->output->write($text);
$this->outputStyle->write($text);
};
}
}
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
JsonThrowOnErrorRector::class => [
__DIR__.'/app/Generators/OpenAIGenerator.php',
],
ChangeAndIfToEarlyReturnRector::class => [
__DIR__.'/app/ConfigManager.php',
],
StaticClosureRector::class => [
__DIR__.'/tests',
],
Expand Down

0 comments on commit ea27423

Please sign in to comment.