Skip to content

Commit 8cd7562

Browse files
committed
[Agent] Code groming arround tall call
1 parent f9da9c0 commit 8cd7562

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public function processOutput(Output $output): void
7878
{
7979
if ($output->getResult() instanceof GenericStreamResponse) {
8080
$output->setResult(
81-
new ToolboxStreamResponse($output->getResult()->getContent(), $this->handleToolCallsCallback($output))
81+
new ToolboxStreamResponse(
82+
$output->getResult()->getContent(),
83+
fn (ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null) => $this->handleToolCallsCallback($output, $result, $streamedAssistantResponse)
84+
)
8285
);
8386

8487
return;
@@ -88,7 +91,7 @@ public function processOutput(Output $output): void
8891
return;
8992
}
9093

91-
$output->setResult($this->handleToolCallsCallback($output)($output->getResult()));
94+
$output->setResult($this->handleToolCallsCallback($output, $output->getResult()));
9295
}
9396

9497
/**
@@ -99,40 +102,39 @@ private function isFlatStringArray(array $tools): bool
99102
return array_reduce($tools, fn (bool $carry, mixed $item) => $carry && \is_string($item), true);
100103
}
101104

102-
private function handleToolCallsCallback(Output $output): \Closure
105+
private function handleToolCallsCallback(Output $output, ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null): ResultInterface
103106
{
104-
return function (ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null) use ($output): ResultInterface {
105-
++$this->nestingLevel;
106-
$messages = $this->keepToolMessages ? $output->getMessageBag() : clone $output->getMessageBag();
107+
++$this->nestingLevel;
108+
$messages = $this->keepToolMessages ? $output->getMessageBag() : clone $output->getMessageBag();
107109

108-
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
109-
$messages->add($streamedAssistantResponse);
110-
}
110+
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
111+
$messages->add($streamedAssistantResponse);
112+
}
111113

112-
do {
113-
$toolCalls = $result->getContent();
114-
$messages->add(Message::ofAssistant(toolCalls: $toolCalls));
114+
do {
115+
$toolCalls = $result->getContent();
116+
$messages->add(Message::ofAssistant(toolCalls: $toolCalls));
115117

116-
$results = [];
117-
foreach ($toolCalls as $toolCall) {
118-
$results[] = $toolResult = $this->toolbox->execute($toolCall);
119-
$messages->add(Message::ofToolCall($toolCall, $this->resultConverter->convert($toolResult)));
120-
array_push($this->sources, ...$toolResult->getSources());
121-
}
118+
$results = [];
119+
foreach ($toolCalls as $toolCall) {
120+
$results[] = $toolResult = $this->toolbox->execute($toolCall);
121+
// dd($toolResult);
122+
$messages->add(Message::ofToolCall($toolCall, $this->resultConverter->convert($toolResult)));
123+
array_push($this->sources, ...$toolResult->getSources());
124+
}
122125

123-
$event = new ToolCallsExecuted(...$results);
124-
$this->eventDispatcher?->dispatch($event);
126+
$event = new ToolCallsExecuted(...$results);
127+
$this->eventDispatcher?->dispatch($event);
125128

126-
$result = $event->hasResult() ? $event->getResult() : $this->agent->call($messages, $output->getOptions());
127-
} while ($result instanceof ToolCallResult);
129+
$result = $event->hasResult() ? $event->getResult() : $this->agent->call($messages, $output->getOptions());
130+
} while ($result instanceof ToolCallResult);
128131

129-
--$this->nestingLevel;
130-
if ($this->includeSources && 0 === $this->nestingLevel) {
131-
$result->getMetadata()->add('sources', $this->sources);
132-
$this->sources = [];
133-
}
132+
--$this->nestingLevel;
133+
if ($this->includeSources && 0 === $this->nestingLevel) {
134+
$result->getMetadata()->add('sources', $this->sources);
135+
$this->sources = [];
136+
}
134137

135-
return $result;
136-
};
138+
return $result;
137139
}
138140
}

src/agent/src/Toolbox/ToolFactory/MemoryToolFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public function addTool(string|object $class, string $name, string $description,
3333
}
3434

3535
/**
36-
* @param class-string $reference
36+
* @param class-string $className
3737
*/
38-
public function getTool(string $reference): iterable
38+
public function getTool(string $className): iterable
3939
{
40-
if (!isset($this->tools[$reference])) {
41-
throw ToolException::invalidReference($reference);
40+
if (!isset($this->tools[$className])) {
41+
throw ToolException::invalidReference($className);
4242
}
4343

44-
foreach ($this->tools[$reference] as $tool) {
45-
yield $this->convertAttribute($reference, $tool);
44+
foreach ($this->tools[$className] as $tool) {
45+
yield $this->convertAttribute($className, $tool);
4646
}
4747
}
4848
}

src/agent/src/Toolbox/Toolbox.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class Toolbox implements ToolboxInterface
3434
/**
3535
* List of executable tools.
3636
*
37-
* @var list<mixed>
37+
* @var list<object>
3838
*/
3939
private readonly array $tools;
4040

@@ -43,10 +43,10 @@ final class Toolbox implements ToolboxInterface
4343
*
4444
* @var Tool[]
4545
*/
46-
private array $map;
46+
private array $toolsMetadata;
4747

4848
/**
49-
* @param iterable<mixed> $tools
49+
* @param iterable<object> $tools
5050
*/
5151
public function __construct(
5252
iterable $tools,
@@ -60,18 +60,18 @@ public function __construct(
6060

6161
public function getTools(): array
6262
{
63-
if (isset($this->map)) {
64-
return $this->map;
63+
if (isset($this->toolsMetadata)) {
64+
return $this->toolsMetadata;
6565
}
6666

67-
$map = [];
67+
$toolsMetadata = [];
6868
foreach ($this->tools as $tool) {
6969
foreach ($this->toolFactory->getTool($tool::class) as $metadata) {
70-
$map[] = $metadata;
70+
$toolsMetadata[] = $metadata;
7171
}
7272
}
7373

74-
return $this->map = $map;
74+
return $this->toolsMetadata = $toolsMetadata;
7575
}
7676

7777
public function execute(ToolCall $toolCall): ToolResult

src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private function isToolCallsStreamFinished(array $data): bool
173173
private function convertChoice(array $choice): ToolCallResult|TextResult
174174
{
175175
if ('tool_calls' === $choice['finish_reason']) {
176-
return new ToolCallResult(...array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
176+
return new ToolCallResult(...array_map($this->convertToolCall(...), $choice['message']['tool_calls']));
177177
}
178178

179179
if (\in_array($choice['finish_reason'], ['stop', 'length'], true)) {

0 commit comments

Comments
 (0)