-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelClient.php
More file actions
138 lines (115 loc) · 4.01 KB
/
ModelClient.php
File metadata and controls
138 lines (115 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\ClaudeCode;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Platform\Bridge\ClaudeCode\Exception\CliNotFoundException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\ModelClientInterface;
use Symfony\AI\Platform\Result\RawResultInterface;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
/**
* Spawns the Claude Code CLI as a subprocess and returns the result.
*
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class ModelClient implements ModelClientInterface
{
/**
* @var array<string, string>
*/
private const OPTION_FLAG_MAP = [
'tools' => '--allowedTools',
'allowed_tools' => '--allowedTools',
];
/**
* @param array<string, string> $environment
*/
public function __construct(
private readonly ?string $cliBinary = null,
private readonly ?string $workingDirectory = null,
private readonly ?float $timeout = 300,
private readonly array $environment = [],
private readonly LoggerInterface $logger = new NullLogger(),
) {
}
public function supports(Model $model): bool
{
return $model instanceof ClaudeCode;
}
public function request(Model $model, array|string $payload, array $options = []): RawResultInterface
{
if (!isset($options['model'])) {
$options['model'] = $model->getName();
}
$prompt = $this->extractPrompt($payload);
// Merge payload fields (e.g. system_prompt from the normalizer) into
// options, giving explicit options priority.
if (\is_array($payload)) {
$options = array_merge($payload, $options);
unset($options['prompt']);
}
$cwd = $options['cwd'] ?? $this->workingDirectory;
unset($options['cwd'], $options['stream']);
$command = $this->buildCommand($prompt, $options);
$this->logger->info('Spawning Claude Code CLI subprocess.', [
'command' => implode(' ', array_map('escapeshellarg', $command)),
'cwd' => $cwd,
]);
$process = new Process($command, $cwd, $this->environment, null, $this->timeout);
$process->start();
return new RawProcessResult($process);
}
/**
* @param array<string, mixed> $options
*
* @return string[]
*/
public function buildCommand(string $prompt, array $options = []): array
{
$command = [$this->getCliBinary(), '--output-format', 'stream-json', '--verbose', '--include-partial-messages'];
foreach ($options as $key => $value) {
$flag = self::OPTION_FLAG_MAP[$key] ?? '--'.str_replace('_', '-', $key);
if (\is_array($value)) {
foreach ($value as $item) {
$command[] = $flag;
$command[] = (string) $item;
}
} elseif (true === $value) {
$command[] = $flag;
} elseif (false !== $value) {
$command[] = $flag;
$command[] = (string) $value;
}
}
$command[] = '-p';
$command[] = $prompt;
return $command;
}
private function getCliBinary(): string
{
$binary = $this->cliBinary ?? (new ExecutableFinder())->find('claude');
if (null === $binary || !is_executable($binary)) {
throw new CliNotFoundException();
}
return $binary;
}
/**
* @param array<string|int, mixed>|string $payload
*/
private function extractPrompt(array|string $payload): string
{
if (\is_string($payload)) {
return $payload;
}
return (string) ($payload['prompt'] ?? json_encode($payload, \JSON_THROW_ON_ERROR));
}
}