Skip to content

Commit 50339ed

Browse files
committed
monitoring
1 parent 85bf0dc commit 50339ed

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

src/Observability/AgentMonitoring.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,6 @@ protected function getContext(Agent $agent): array
235235
];
236236
}
237237

238-
public function getMessageId(Message $message, string $prefix = ''): string
239-
{
240-
$content = $message->getContent();
241-
242-
if (\is_array($content)) {
243-
$content = \json_encode($content);
244-
}
245-
246-
return $prefix.\md5($content.$message->getRole());
247-
}
248-
249238
protected function getBaseClassName(string $class): string
250239
{
251240
return \substr(\strrchr($class, '\\'), 1);

src/Observability/HandleInferenceEvents.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NeuronAI\Observability;
66

7+
use Inspector\Models\Segment;
78
use NeuronAI\Agent;
89
use NeuronAI\Chat\Messages\Usage;
910
use NeuronAI\Observability\Events\InferenceStart;
@@ -13,6 +14,9 @@
1314

1415
trait HandleInferenceEvents
1516
{
17+
protected Segment $message;
18+
protected Segment $inference;
19+
1620
public function messageSaving(Agent $agent, string $event, MessageSaving $data): void
1721
{
1822
if (!$this->inspector->canAddSegments()) {
@@ -21,21 +25,19 @@ public function messageSaving(Agent $agent, string $event, MessageSaving $data):
2125

2226
$label = $this->getBaseClassName($data->message::class);
2327

24-
$this->segments[$this->getMessageId($data->message, 'save')] = $this->inspector
28+
$this->message = $this->inspector
2529
->startSegment(self::SEGMENT_TYPE.'.chathistory', "save_message( {$label} )")
2630
->setColor(self::STANDARD_COLOR);
2731
}
2832

2933
public function messageSaved(Agent $agent, string $event, MessageSaved $data): void
3034
{
31-
$id = $this->getMessageId($data->message, 'save');
32-
33-
if (!\array_key_exists($id, $this->segments)) {
35+
if (!isset($this->message)) {
3436
return;
3537
}
3638

37-
$segment = $this->segments[$id];
38-
$segment->addContext('Message', \array_merge(
39+
$this->message->end();
40+
$this->message->addContext('Message', \array_merge(
3941
$data->message->jsonSerialize(),
4042
$data->message->getUsage() instanceof Usage ? [
4143
'usage' => [
@@ -44,8 +46,6 @@ public function messageSaved(Agent $agent, string $event, MessageSaved $data): v
4446
]
4547
] : []
4648
));
47-
$segment->end();
48-
unset($this->segments[$id]);
4949
}
5050

5151
public function inferenceStart(Agent $agent, string $event, InferenceStart $data): void
@@ -56,22 +56,17 @@ public function inferenceStart(Agent $agent, string $event, InferenceStart $data
5656

5757
$label = $this->getBaseClassName($data->message::class);
5858

59-
$this->segments[$this->getMessageId($data->message, 'inference')] = $this->inspector
59+
$this->inference = $this->inspector
6060
->startSegment(self::SEGMENT_TYPE.'.inference', "inference( {$label} )")
6161
->setColor(self::STANDARD_COLOR);
6262
}
6363

6464
public function inferenceStop(Agent $agent, string $event, InferenceStop $data): void
6565
{
66-
$id = $this->getMessageId($data->message, 'inference');
67-
68-
if (!\array_key_exists($id, $this->segments)) {
69-
return;
66+
if (isset($this->inference)) {
67+
$this->inference->end();
68+
$this->inference->addContext('Message', $data->message)
69+
->addContext('Response', $data->response);
7070
}
71-
72-
$segment = $this->segments[$id]->end();
73-
$segment->addContext('Message', $data->message)
74-
->addContext('Response', $data->response);
75-
unset($this->segments[$id]);
7671
}
7772
}

src/Observability/HandleStructuredEvents.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NeuronAI\Observability;
66

7+
use Inspector\Models\Segment;
78
use NeuronAI\AgentInterface;
89
use NeuronAI\Observability\Events\Deserialized;
910
use NeuronAI\Observability\Events\Deserializing;
@@ -16,21 +17,26 @@
1617

1718
trait 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
}

src/Observability/HandleToolEvents.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NeuronAI\Observability;
66

7+
use Inspector\Models\Segment;
78
use NeuronAI\AgentInterface;
89
use NeuronAI\Observability\Events\ToolCalled;
910
use NeuronAI\Observability\Events\ToolCalling;
@@ -13,13 +14,20 @@
1314

1415
trait HandleToolEvents
1516
{
17+
protected Segment $toolBootstrap;
18+
19+
/**
20+
* @var array<Segment>
21+
*/
22+
protected array $toolCalls;
23+
1624
public function toolsBootstrapping(AgentInterface $agent, string $event, mixed $data): void
1725
{
18-
if (!$this->inspector->canAddSegments()) {
26+
if (!$this->inspector->canAddSegments() || $agent->getTools() === []) {
1927
return;
2028
}
2129

22-
$this->segments[$agent::class.'_tools_bootstrap'] = $this->inspector
30+
$this->toolBootstrap = $this->inspector
2331
->startSegment(
2432
self::SEGMENT_TYPE.'.tool',
2533
"tools_bootstrap()"
@@ -29,17 +37,17 @@ public function toolsBootstrapping(AgentInterface $agent, string $event, mixed $
2937

3038
public function toolsBootstrapped(AgentInterface $agent, string $event, ToolsBootstrapped $data): void
3139
{
32-
if (\array_key_exists($agent::class.'_tools_bootstrap', $this->segments) && $data->tools !== []) {
33-
$segment = $this->segments[$agent::class.'_tools_bootstrap']->end();
34-
$segment->addContext('Tools', \array_reduce($data->tools, function (array $carry, ToolInterface|ProviderToolInterface $tool): array {
40+
if (isset($this->toolBootstrap)) {
41+
$this->toolBootstrap->end();
42+
$this->toolBootstrap->addContext('Tools', \array_reduce($data->tools, function (array $carry, ToolInterface|ProviderToolInterface $tool): array {
3543
if ($tool instanceof ProviderToolInterface) {
3644
$carry[$tool->getType()] = $tool->getOptions();
3745
} else {
3846
$carry[$tool->getName()] = $tool->getDescription();
3947
}
4048
return $carry;
4149
}, []));
42-
$segment->addContext('Guidelines', $data->guidelines);
50+
$this->toolBootstrap->addContext('Guidelines', $data->guidelines);
4351
}
4452
}
4553

@@ -49,7 +57,7 @@ public function toolCalling(AgentInterface $agent, string $event, ToolCalling $d
4957
return;
5058
}
5159

52-
$this->segments[$data->tool->getName()] = $this->inspector
60+
$this->toolCalls[$data->tool::class] = $this->inspector
5361
->startSegment(
5462
self::SEGMENT_TYPE.'.tool',
5563
"tool_call( {$data->tool->getName()} )"
@@ -59,12 +67,13 @@ public function toolCalling(AgentInterface $agent, string $event, ToolCalling $d
5967

6068
public function toolCalled(AgentInterface $agent, string $event, ToolCalled $data): void
6169
{
62-
if (\array_key_exists($data->tool->getName(), $this->segments)) {
63-
$this->segments[$data->tool->getName()]
64-
->end()
65-
->addContext('Properties', $data->tool->getProperties())
66-
->addContext('Inputs', $data->tool->getInputs())
67-
->addContext('Output', $data->tool->getResult());
70+
if (!\array_key_exists($data->tool::class, $this->toolCalls)) {
71+
return;
6872
}
73+
74+
$this->toolCalls[$data->tool::class]->end()
75+
->addContext('Properties', $data->tool->getProperties())
76+
->addContext('Inputs', $data->tool->getInputs())
77+
->addContext('Output', $data->tool->getResult());
6978
}
7079
}

0 commit comments

Comments
 (0)