44
55namespace NeuronAI \Observability ;
66
7+ use Inspector \Models \Segment ;
78use NeuronAI \AgentInterface ;
89use NeuronAI \Observability \Events \Deserialized ;
910use NeuronAI \Observability \Events \Deserializing ;
1617
1718trait HandleStructuredEvents
1819{
20+ protected Segment $ schema ;
21+ protected Segment $ extract ;
22+ protected Segment $ deserialize ;
23+ protected Segment $ validate ;
24+
1925 protected function schemaGeneration (AgentInterface $ agent , string $ event , SchemaGeneration $ data ): void
2026 {
2127 if (!$ this ->inspector ->canAddSegments ()) {
2228 return ;
2329 }
2430
25- $ this ->segments [ $ data -> class . ' - schema' ] = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "schema_generate( {$ data ->class } ) " )
31+ $ this ->schema = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "schema_generate( {$ data ->class } ) " )
2632 ->setColor (self ::STANDARD_COLOR );
2733 }
2834
2935 protected function schemaGenerated (AgentInterface $ agent , string $ event , SchemaGenerated $ data ): void
3036 {
31- if (\array_key_exists ( $ data -> class . ' -schema ' , $ this ->segments )) {
32- $ segment = $ this ->segments [ $ data -> class . ' - schema' ] ->end ();
33- $ segment ->addContext ('Schema ' , $ data ->schema );
37+ if (isset ( $ this ->schema )) {
38+ $ this ->schema ->end ();
39+ $ this -> schema ->addContext ('Schema ' , $ data ->schema );
3440 }
3541 }
3642
@@ -40,22 +46,18 @@ protected function extracting(AgentInterface $agent, string $event, Extracting $
4046 return ;
4147 }
4248
43- $ id = $ this ->getMessageId ($ data ->message , 'extract ' );
44-
45- $ this ->segments [$ id ] = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , 'extract_output ' )
49+ $ this ->extract = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , 'extract_output ' )
4650 ->setColor (self ::STANDARD_COLOR );
4751 }
4852
4953 protected function extracted (AgentInterface $ agent , string $ event , Extracted $ data ): void
5054 {
51- $ id = $ this ->getMessageId ($ data ->message , 'extract ' );
52-
53- if (!\array_key_exists ($ id , $ this ->segments )) {
55+ if (!isset ($ this ->extract )) {
5456 return ;
5557 }
5658
57- $ segment = $ this ->segments [ $ id ] ->end ();
58- $ segment ->addContext (
59+ $ this ->extract ->end ();
60+ $ this -> extract ->addContext (
5961 'Data ' ,
6062 [
6163 'response ' => $ data ->message ->jsonSerialize (),
@@ -65,7 +67,6 @@ protected function extracted(AgentInterface $agent, string $event, Extracted $da
6567 'Schema ' ,
6668 $ data ->schema
6769 );
68- unset($ this ->segments [$ id ]);
6970 }
7071
7172 protected function deserializing (AgentInterface $ agent , string $ event , Deserializing $ data ): void
@@ -74,16 +75,14 @@ protected function deserializing(AgentInterface $agent, string $event, Deseriali
7475 return ;
7576 }
7677
77- $ this ->segments [ $ data -> class . ' - deserialize' ] = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "deserialize( {$ data ->class } ) " )
78+ $ this ->deserialize = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "deserialize( {$ data ->class } ) " )
7879 ->setColor (self ::STANDARD_COLOR );
7980 }
8081
8182 protected function deserialized (AgentInterface $ agent , string $ event , Deserialized $ data ): void
8283 {
83- $ id = $ data ->class .'-deserialize ' ;
84-
85- if (\array_key_exists ($ id , $ this ->segments )) {
86- $ this ->segments [$ id ]->end ();
84+ if (isset ($ this ->deserialize )) {
85+ $ this ->deserialize ->end ();
8786 }
8887 }
8988
@@ -93,19 +92,17 @@ protected function validating(AgentInterface $agent, string $event, Validating $
9392 return ;
9493 }
9594
96- $ this ->segments [ $ data -> class . ' - validate' ] = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "validate( {$ data ->class } ) " )
95+ $ this ->validate = $ this ->inspector ->startSegment (self ::SEGMENT_TYPE .'.structured-output ' , "validate( {$ data ->class } ) " )
9796 ->setColor (self ::STANDARD_COLOR );
9897 }
9998
10099 protected function validated (AgentInterface $ agent , string $ event , Validated $ data ): void
101100 {
102- $ id = $ data ->class .'-validate ' ;
103-
104- if (\array_key_exists ($ id , $ this ->segments )) {
105- $ segment = $ this ->segments [$ id ]->end ();
106- $ segment ->addContext ('Json ' , \json_decode ($ data ->json ));
101+ if (isset ($ this ->validate )) {
102+ $ this ->validate ->end ();
103+ $ this ->validate ->addContext ('Json ' , \json_decode ($ data ->json ));
107104 if ($ data ->violations !== []) {
108- $ segment ->addContext ('Violations ' , $ data ->violations );
105+ $ this -> validate ->addContext ('Violations ' , $ data ->violations );
109106 }
110107 }
111108 }
0 commit comments