Skip to content

Commit 85bf0dc

Browse files
committed
monitoring
1 parent 3c9aadd commit 85bf0dc

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/Observability/AgentMonitoring.php

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

238-
public function getMessageId(Message $message): string
238+
public function getMessageId(Message $message, string $prefix = ''): string
239239
{
240240
$content = $message->getContent();
241241

242-
if (!\is_string($content)) {
243-
$content = \json_encode($content, \JSON_UNESCAPED_UNICODE);
242+
if (\is_array($content)) {
243+
$content = \json_encode($content);
244244
}
245245

246-
return \md5($content.$message->getRole());
246+
return $prefix.\md5($content.$message->getRole());
247247
}
248248

249249
protected function getBaseClassName(string $class): string

src/Observability/HandleInferenceEvents.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public function messageSaving(Agent $agent, string $event, MessageSaving $data):
2121

2222
$label = $this->getBaseClassName($data->message::class);
2323

24-
$this->segments[$this->getMessageId($data->message).'-save'] = $this->inspector
24+
$this->segments[$this->getMessageId($data->message, 'save')] = $this->inspector
2525
->startSegment(self::SEGMENT_TYPE.'.chathistory', "save_message( {$label} )")
2626
->setColor(self::STANDARD_COLOR);
2727
}
2828

2929
public function messageSaved(Agent $agent, string $event, MessageSaved $data): void
3030
{
31-
$id = $this->getMessageId($data->message).'-save';
31+
$id = $this->getMessageId($data->message, 'save');
3232

3333
if (!\array_key_exists($id, $this->segments)) {
3434
return;
@@ -45,6 +45,7 @@ public function messageSaved(Agent $agent, string $event, MessageSaved $data): v
4545
] : []
4646
));
4747
$segment->end();
48+
unset($this->segments[$id]);
4849
}
4950

5051
public function inferenceStart(Agent $agent, string $event, InferenceStart $data): void
@@ -55,19 +56,22 @@ public function inferenceStart(Agent $agent, string $event, InferenceStart $data
5556

5657
$label = $this->getBaseClassName($data->message::class);
5758

58-
$this->segments[$this->getMessageId($data->message).'-inference'] = $this->inspector
59+
$this->segments[$this->getMessageId($data->message, 'inference')] = $this->inspector
5960
->startSegment(self::SEGMENT_TYPE.'.inference', "inference( {$label} )")
6061
->setColor(self::STANDARD_COLOR);
6162
}
6263

6364
public function inferenceStop(Agent $agent, string $event, InferenceStop $data): void
6465
{
65-
$id = $this->getMessageId($data->message).'-inference';
66+
$id = $this->getMessageId($data->message, 'inference');
6667

67-
if (\array_key_exists($id, $this->segments)) {
68-
$segment = $this->segments[$id]->end();
69-
$segment->addContext('Message', $data->message)
70-
->addContext('Response', $data->response);
68+
if (!\array_key_exists($id, $this->segments)) {
69+
return;
7170
}
71+
72+
$segment = $this->segments[$id]->end();
73+
$segment->addContext('Message', $data->message)
74+
->addContext('Response', $data->response);
75+
unset($this->segments[$id]);
7276
}
7377
}

src/Observability/HandleStructuredEvents.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,32 @@ protected function extracting(AgentInterface $agent, string $event, Extracting $
4040
return;
4141
}
4242

43-
$id = $this->getMessageId($data->message).'-extract';
43+
$id = $this->getMessageId($data->message, 'extract');
4444

4545
$this->segments[$id] = $this->inspector->startSegment(self::SEGMENT_TYPE.'.structured-output', 'extract_output')
4646
->setColor(self::STANDARD_COLOR);
4747
}
4848

4949
protected function extracted(AgentInterface $agent, string $event, Extracted $data): void
5050
{
51-
$id = $this->getMessageId($data->message).'-extract';
51+
$id = $this->getMessageId($data->message, 'extract');
5252

53-
if (\array_key_exists($id, $this->segments)) {
54-
$segment = $this->segments[$id]->end();
55-
$segment->addContext(
56-
'Data',
57-
[
58-
'response' => $data->message->jsonSerialize(),
59-
'json' => $data->json,
60-
]
61-
)->addContext(
62-
'Schema',
63-
$data->schema
64-
);
65-
unset($this->segments[$id]);
53+
if (!\array_key_exists($id, $this->segments)) {
54+
return;
6655
}
56+
57+
$segment = $this->segments[$id]->end();
58+
$segment->addContext(
59+
'Data',
60+
[
61+
'response' => $data->message->jsonSerialize(),
62+
'json' => $data->json,
63+
]
64+
)->addContext(
65+
'Schema',
66+
$data->schema
67+
);
68+
unset($this->segments[$id]);
6769
}
6870

6971
protected function deserializing(AgentInterface $agent, string $event, Deserializing $data): void

0 commit comments

Comments
 (0)