Skip to content

Commit d6b873e

Browse files
committed
[Agent] Code groming arround tall call
1 parent 95751b8 commit d6b873e

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

src/agent/src/Toolbox/AgentProcessor.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public function processOutput(Output $output): void
8080

8181
if ($result instanceof GenericStreamResponse) {
8282
$output->setResult(
83-
new ToolboxStreamResponse($result->getContent(), $this->handleToolCallsCallback($output))
83+
new ToolboxStreamResponse(
84+
$result->getContent(),
85+
fn (ToolCallResult $result, ?AssistantMessage $streamedAssistantResponse = null) => $this->handleToolCallsCallback($output, $result, $streamedAssistantResponse)
86+
)
8487
);
8588

8689
return;
@@ -90,7 +93,7 @@ public function processOutput(Output $output): void
9093
return;
9194
}
9295

93-
$output->setResult($this->handleToolCallsCallback($output)($result));
96+
$output->setResult($this->handleToolCallsCallback($output, $result));
9497
}
9598

9699
/**
@@ -101,40 +104,38 @@ private function isFlatStringArray(array $tools): bool
101104
return array_reduce($tools, fn (bool $carry, mixed $item) => $carry && \is_string($item), true);
102105
}
103106

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

110-
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
111-
$messages->add($streamedAssistantResponse);
112-
}
112+
if (null !== $streamedAssistantResponse && '' !== $streamedAssistantResponse->getContent()) {
113+
$messages->add($streamedAssistantResponse);
114+
}
113115

114-
do {
115-
$toolCalls = $result->getContent();
116-
$messages->add(Message::ofAssistant(toolCalls: $toolCalls));
116+
do {
117+
$toolCalls = $result->getContent();
118+
$messages->add(Message::ofAssistant(toolCalls: $toolCalls));
117119

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

125-
$event = new ToolCallsExecuted(...$results);
126-
$this->eventDispatcher?->dispatch($event);
127+
$event = new ToolCallsExecuted(...$results);
128+
$this->eventDispatcher?->dispatch($event);
127129

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

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

137-
return $result;
138-
};
139+
return $result;
139140
}
140141
}

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class Toolbox implements ToolboxInterface
4343
*
4444
* @var Tool[]
4545
*/
46-
private array $map;
46+
private array $toolsMetadata;
4747

4848
/**
4949
* @param iterable<object> $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)