Skip to content

Commit 825f3df

Browse files
author
ityaozm@gmail.com
committed
refactor(generator): Simplify command options handling
- Moved command options filtering and mapping to a reusable method. - Introduced `defaultHydratedOptions()` to encapsulate logic for non-empty options. - Streamlined the command array creation in `GithubModelsCliGenerator`.
1 parent 9c5e226 commit 825f3df

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

app/Generators/Generator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,20 @@ protected function defaultRunningCallback(): callable
108108
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
109109
};
110110
}
111+
112+
/**
113+
* @return list<string>
114+
*/
115+
protected function defaultHydratedOptions(): array
116+
{
117+
return collect($this->config['options'] ?? [])
118+
->filter(static function ($value): bool {
119+
return null !== $value && '' !== $value;
120+
})
121+
->map(static function ($value, string $key): array {
122+
return [$key, $value];
123+
})
124+
->flatten()
125+
->all();
126+
}
111127
}

app/Generators/GithubModelsCliGenerator.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,10 @@ public function generate(string $prompt): string
2121
return resolve(
2222
Process::class,
2323
[
24-
'command' => collect([
25-
$this->config['binary'],
26-
'models',
27-
'run',
28-
$this->config['model'],
29-
$prompt,
30-
])->merge(
31-
collect($this->config['options'])
32-
->filter(static function ($value): bool {
33-
return null !== $value && '' !== $value;
34-
})
35-
->map(static function ($value, string $key): array {
36-
return [$key, $value];
37-
})
38-
->flatten()
39-
)->all(),
24+
'command' => array_merge(
25+
[$this->config['binary'], 'models', 'run', $this->config['model'], $prompt],
26+
$this->defaultHydratedOptions()
27+
),
4028
] + $this->config['parameters']
4129
)->mustRun($this->defaultRunningCallback())->getOutput();
4230
}

0 commit comments

Comments
 (0)