1111
1212namespace Symfony \AI \AiBundle \Profiler ;
1313
14- use Symfony \AI \Agent \Toolbox \ToolResult ;
15- use Symfony \AI \Platform \Metadata \ Metadata ;
14+ use Symfony \AI \Agent \Toolbox \ToolboxInterface ;
15+ use Symfony \AI \Platform \Model ;
1616use Symfony \AI \Platform \Tool \Tool ;
1717use Symfony \Bundle \FrameworkBundle \DataCollector \AbstractDataCollector ;
1818use Symfony \Component \HttpFoundation \Request ;
1919use Symfony \Component \HttpFoundation \Response ;
2020use Symfony \Component \HttpKernel \DataCollector \LateDataCollectorInterface ;
21+ use Symfony \Component \VarDumper \Cloner \Data ;
2122
2223/**
2324 * @author Christopher Hertel <[email protected] > 2425 *
2526 * @phpstan-import-type PlatformCallData from TraceablePlatform
27+ * @phpstan-import-type ToolCallData from TraceableToolbox
2628 */
2729final class DataCollector extends AbstractDataCollector implements LateDataCollectorInterface
2830{
@@ -37,11 +39,17 @@ final class DataCollector extends AbstractDataCollector implements LateDataColle
3739 private readonly array $ toolboxes ;
3840
3941 /**
40- * @param TraceablePlatform[] $platforms
41- * @param TraceableToolbox[] $toolboxes
42+ * @var list<array{method: string, duration: float, input: mixed, result: mixed, error: ?\Throwable}>
43+ */
44+ private array $ collectedChatCalls = [];
45+
46+ /**
47+ * @param iterable<TraceablePlatform> $platforms
48+ * @param iterable<TraceableToolbox> $toolboxes
4249 */
4350 public function __construct (
4451 iterable $ platforms ,
52+ private readonly ToolboxInterface $ defaultToolBox ,
4553 iterable $ toolboxes ,
4654 ) {
4755 $ this ->platforms = $ platforms instanceof \Traversable ? iterator_to_array ($ platforms ) : $ platforms ;
@@ -50,15 +58,26 @@ public function __construct(
5058
5159 public function collect (Request $ request , Response $ response , ?\Throwable $ exception = null ): void
5260 {
53- $ this ->lateCollect ();
5461 }
5562
5663 public function lateCollect (): void
5764 {
5865 $ this ->data = [
59- 'tools ' => $ this ->getAllTools (),
66+ 'tools ' => $ this ->defaultToolBox -> getTools (),
6067 'platform_calls ' => array_merge (...array_map ($ this ->awaitCallResults (...), $ this ->platforms )),
6168 'tool_calls ' => array_merge (...array_map (fn (TraceableToolbox $ toolbox ) => $ toolbox ->calls , $ this ->toolboxes )),
69+ 'chat_calls ' => $ this ->cloneVar ($ this ->collectedChatCalls ),
70+ ];
71+ }
72+
73+ public function collectChatCall (string $ method , float $ duration , mixed $ input , mixed $ result , ?\Throwable $ error ): void
74+ {
75+ $ this ->collectedChatCalls [] = [
76+ 'method ' => $ method ,
77+ 'duration ' => $ duration ,
78+ 'input ' => $ input ,
79+ 'result ' => $ result ,
80+ 'error ' => $ error ,
6281 ];
6382 }
6483
@@ -84,44 +103,54 @@ public function getTools(): array
84103 }
85104
86105 /**
87- * @return ToolResult []
106+ * @return ToolCallData []
88107 */
89108 public function getToolCalls (): array
90109 {
91110 return $ this ->data ['tool_calls ' ] ?? [];
92111 }
93112
94113 /**
95- * @return Tool[]
114+ * @return list<array{method: string, duration: float, input: mixed, result: mixed, error: ?\Throwable}>
96115 */
97- private function getAllTools (): array
116+ public function getChatCalls (): array
98117 {
99- return array_merge (...array_map (fn (TraceableToolbox $ toolbox ) => $ toolbox ->getTools (), $ this ->toolboxes ));
118+ if (!isset ($ this ->data ['chat_calls ' ])) {
119+ return [];
120+ }
121+
122+ $ chatCalls = $ this ->data ['chat_calls ' ]->getValue (true );
123+
124+ /** @var list<array{method: string, duration: float, input: mixed, result: mixed, error: ?\Throwable}> $chatCalls */
125+ return $ chatCalls ;
126+ }
127+
128+ public function reset (): void
129+ {
130+ $ this ->data = [];
131+ $ this ->collectedChatCalls = [];
100132 }
101133
102134 /**
103135 * @return array{
104- * model: string,
105- * input: array<mixed>|string|object,
106- * options: array<string, mixed>,
107- * result: string|iterable<mixed>|object|null,
108- * metadata: Metadata,
136+ * model: Model,
137+ * input: array<mixed>|string|object,
138+ * options: array<string, mixed>,
139+ * result: string|iterable<mixed>|object|null
109140 * }[]
110141 */
111142 private function awaitCallResults (TraceablePlatform $ platform ): array
112143 {
113144 $ calls = $ platform ->calls ;
114145 foreach ($ calls as $ key => $ call ) {
115- $ result = $ call ['result ' ]-> getResult () ;
146+ $ result = $ call ['result ' ];
116147
117148 if (isset ($ platform ->resultCache [$ result ])) {
118149 $ call ['result ' ] = $ platform ->resultCache [$ result ];
119150 } else {
120- $ call ['result ' ] = $ result ->getContent ();
151+ $ call ['result ' ] = $ result ->asText ();
121152 }
122153
123- $ call ['metadata ' ] = $ result ->getMetadata ();
124-
125154 $ calls [$ key ] = $ call ;
126155 }
127156
0 commit comments